Pages

Sunday, June 3, 2012

Determine the SQL Server Service Pack Installed

It can be difficult to find which Service Pack for SQL Server is installed on a machine. Neither is this information stated under SQL Server nor under Windows > Add Remove Programs. To get this information, use the following query:

SELECT SERVERPROPERTY ('productlevel')

You will get the following result (depending on the SP installed):

SP[1,2,3...]

I hope this helps. Stay tunned for more...

Monday, May 21, 2012

ASP.NET application not browsing on localhost

Recently I came across a problem where we hosted a new ASP.NET website on a freshly installed IIS (Windows 7). We were able to browse the application through the IP address. But when we tried to browse the application using the hostname or localhost, it didn't work (we reached the homepage but afterwards, all functions resulted in an asp.net error). We had the following scenario:

1. http://hostmachine/somesite (No)
2. http://localhost/somesite (No)
3. http://1.1.1.1/somesite (Yes)

After doing lots of googling (I mean binging :-), it turned out to be a potential bug where the DNS resolution didn't work for the loopback address. Microsoft claims that 127.0.0.1 is automatically resolved. But this is not the case and needs a fix. To fix this issue, go to C:\Windows\System32\drivers\etc and open the file hosts. Uncomment the following line:


# 127.0.0.1 localhost (remove #)


Now you can browse your application using localhost or hostname without any issue. I hope you find this article useful. Stay tuned for more.

Wednesday, February 1, 2012

SQL Server Indexing

In this blog, I will talk about SQL Server indexing. More to follow shortly...

Nested Master Pages

In this blog, we talk about nested master pages. Master Pages are a great way of providing a consistent look and feel throughout a asp.net website. More to follow shortly....

Tuesday, May 31, 2011

Information System

In this blog entry, I will talk about what are Information Systems? what are the different types of Information Systems and what benefits do they provide? More to follow shortly. Stay tuned for more...

Sunday, March 13, 2011

SQL Server EXCEPT Clause

A few days back I had performance issues with a query in SQL Server 2005. The query was a nested query similar to the following (details omitted for brevity):


SELECT
   EmployeeID, DateAttended
FROM
   CourseDetails
WHERE
   EmployeeID NOT IN
   (
      SELECT
         EmployeeID
      FROM
         CourseDetails
      WHERE
         isActive = 0
   )


The above query would return all data rows where [Courses] are 'Active'. Due to the slow query, the web application was timing out. After doing some research, I finally came across the wonder EXCEPT clause in SQL Server 2005.

The EXCEPT clause returns all rows from the first query which are not present in the second query. It's more like a LEFT JOIN where rows from the left tables are returned. It is important to note that both the first and second query must have same data columns with similar data types.

Using the EXCEPT clause, we can now rewrite the above query as following:


SELECT
   EmployeeID, DateAttended
FROM
   CourseDetails
EXCEPT
   SELECT
      EmployeeID
   FROM
      CourseDetails
   WHERE
      isActive = 0


To my joy, the performance of the query was lightening fast. It really worked like a charm. So my advice would be to use the EXCEPT clause wherever possible and avoid nested queries.

I hope you found this blog use. Stay tuned for more.

Monday, February 28, 2011

Using Surrogate Key in Databases

I prefer using surrogate key (a non business key used as an identifier e.g. Instead of using a StudentID as an identifier, we may use a separate RowID column to identify each student record) whenever I can. Different people have different opinions about using surrogate keys in databases. Some will always use a surrogate key while others won’t. It all comes down to the personal preference of a database developer.

However, based on my experience working with surrogate keys, I am off the opinion that surrogate keys are both good and bad. Don’t get me wrong when I call them bad. They don’t do any harm rather add to the working load on behalf of the developer. Following are some of the pros and cons of using a surrogate key:



PROS

1. Primary keys (or natural keys) are hard to change. In programming, there are times when we may need to change the primary key e.g. we can change a Student-ID from type char (6) to char (8) to accommodate more students. If Student-ID is defined as a primary key, it must have links in several tables. This means, the change must be reflected in all the associated tables (a nightmare for a developer). However, if a separate surrogate key is defined, Student-ID type can be changed without affecting multiple tables.

2. A surrogate key can assist developers in programming e.g. suppose a table has a surrogate key defined while the unique key is defined by the combination of three columns. If the data in this table is displayed in a gridview in asp.net, the developer can easily write code to Update and Delete records based on the primary key. In this case, he doesn’t have to worry about all three keys which define the unique key.

3. There is no locking contention since a surrogate key is generated by the database and cached making them highly scalable.



CONS

1. Data integrity becomes the responsibility of the developer. Whenever a record is inserted or updated, the developer must check if a similar natural key already exists to avoid duplication. If the natural key was defined as the primary key, the database would take care of data integrity.

2. Excessive joins are needed since joins are dependent on keys with business value and not a surrogate key e.g. to get details of a student from different tables, a developer would prefer to use his Registration-ID and not a surrogate key.

3. An separate index must be defined on the natural key.

4. From a programming point of view, surrogate keys cannot be used as a search key.


I hope you find this blog as useful. Stay tuned for more...

I start writing again - finally!

I have been away from blogging for quite some time. This can be attributed to my busy schedule (and some what laziness :-). However, I now plan to write on regular basis. I have some interesting topics in mind to start with. I also plan to finish my incomplete 'LINQ Explained' series. Honestly, LINQ is one topic which requires thorough and deep understanding of concepts before talking about it. In coming days, I also plan to blog about SharePoint Server, Enterprise Concepts and ASP.NET. So stay tuned for more...

Saturday, June 19, 2010

Creating SQL Server Limited User Account

Sometimes you need to create a limited user account in SQL Server 2005. This is a trivial task but I find many new DB developers struggling to do so. However, if you understand the underlying concept, it indeed is trivial.

This is how I find new DB developers creating a limited user account:

1. Open up SQL Server Management Studio
2. Expand the desired DB > Security
3. Right click on Users and select New User…
4. In the Database User – New dialog, enter in a User name following by a Login name. Usually both are the same
5. Bang. This is where you get the error “Error 15007 when trying to add a new user”

So what went wrong in the above steps? The answer is that you cannot create a User Role without first creating a Login. A Login connects to an SQL Server instance while a User Role defines the database access level. In other words:

Login – SQL Server Level
User – Database Level

Now to create a limited user account, you following steps have to be followed:

1. Open up SQL Server Management Studio
2. Expand Security. You will see a Logins node. Right click on Logins and click on New Login…
3. In the Login – New dialog, enter in a Login name
4. Next click on SQL Server Authentication radio button. Enter and confirm Password.
5. From Default database, select the desired database. Now you are done creating a Login. The next step is to create his specific role
6. Under the SQL Server instance node, Expand Databases > [Database] > Security. You will see a Users node.
7. Right click on Users and select New User…
8. In the Database User – New dialog, under General page, enter in User name
9. In front of Login name, click on the browse (…) button. You will see a Select Login dialog
10. Click on Browse button and check the Login you created above and click OK. Close the Select Login dialog by click on OK
11. Next under Database User – New dialog, click on Securables page. Click on Add button. This will open up Add Objects dialog.
12. Select Specific objects and click OK. This will open up Select Objects dialog.
13. Click on Object Types button. This will open up Select Object Types dialog.
14. Check the desired object type (Tables, Views, Stored Procedures etc) which Login will have access to. Click OK
15. Under Select Objects dialog, click on Browse button. This will open up Browse for Objects dialog. Select the desired objects which Login will have access to. Click OK.
16. Click on OK to close Select Objects dialog.
17. Under Database User - New, you can select each object in Securables list and specify permission level on each object in the Explicit permissions for list
18. Click on OK to close the Database User - New dialog. You have now set permissions on the specific user.

I hope you have found this post to be useful. Please do provide your feedback and stay tuned for more…

Understanding HTML, XHTML and DHTML

As programmers, we come across the terms HTML, XHTML and DHTML on a daily basis. The difference between these is subtle but important to understand. This post will elaborate these concepts in further details. I would suggest reading my other post HTML Document Structure before proceeding.

HTML

Hyper Text Markup Language or HTML is a markup language used for creating web documents. It has the following characteristics:

1. HTML is a markup language and not a programming language.
2. HTML is an application of SGML (Standard Generalized Markup Language). SGML is a system for defining markup languages.
3. HTML markup consists of elements where each element has a start and end tag. The content of the element is contained between the two tags.
4. HTML also includes character reference and symbols such as ‘&lt;’ is used to represent the ‘<’ sign.
5. An HTML document allows comments.
6. HTML documents are validated by Document Type Definition or DTD.


XHTML

Extensible Hyper Text Markup Language or XHTML is the reevaluation of HTML 4. Both have sharp resembles but with subtle differences. However, the most critical difference between the two is that XHTML is an application of XML . This means that XHTML follows the XML syntax rules for validating web documents. These rules include the following:

1. XHTML is case-sensitive.
2. The entire document has only one root element.
3. Elements must be nested in the correct order.
4. Every element must have a closing tag leading to a well-formed document. Elements without content such ‘br’ must have a self-closing tag.
5. The element tags and attributes are in lowercase.
6. Attributes must be in double or single quotes.
7. Each attribute must have a corresponding value. There is no notion of default values.
8. If there is a syntax error in the XHTML document, the entire document is aborted from loading.
9. Comments are limited in XHTML.
10. The id attribute is used instead of name attribute.

Since XHTML complies with the above rules, it is portable across different platforms including web browsers, mobile phones, palm devices or any reduced browser. HTML, on the other hand, can ignore these rules altogether leading to document with syntax errors.

XHTML also deals with CSS differently. This includes the following characteristics:

1. Element Selectors are case sensitive.
2. In HTML certain properties (background, overflow) of the BODY element applies to the root HTML element as well. This is not true for XHTML.
3. In HTML, even if we omit some tags, elements still exist in the DOM and hence the CSS properties apply to them. This is not true for XHTML. The CSS will only apply to elements with proper markup.
4. MIME types (Content-Type header specified in HTML/XHTML document) are very important when using style-sheets in XHTML document. An XHTML document can work with application/xhtml+xml, application/xml and text/xml MIME types. An XHMTL document using text/xml MIME type is parsed as HTML. However, a style sheet written specifically for XHTML document may not work with text/xml MIME type (since its interpreted as HTML).

XHTML also deals with JavaScript differently. Some of these characteristics include the following:

1. XHTML does not support the .innerHTML property.
2. XHTML does not support document.write () otherwise it confuses the browser which is unable to tell whether a document is well formed or not. For example suppose we have a tag </MyTag> somewhere in the document. Obviously this is not a well formed tag. However, if somewhere above JavaScript uses the statement ‘document.write (“<MyTag>”);’ it will make it a valid tag. This means that unless the document is fully served, the browser cannot if it is a well formed document.
3. DOM methods are replaced by respective namespace-based methods.


DHTML

Unlike HTML and XHTML, Dynamic HTML or DHTML is not an industry standard and is not supported by W3C, IEEE or ISO. The term DHTM was coined by Microsoft. DHTML represents a set of several technologies/standards including HTML/XHTML, DOM, CSS and JavaScript. The merger of these technologies leads to development of Rich Client Applications. HTML/XHTML and CSS are used to create the static but rich visual appearance while JavaScript and DOM are used to make the web applications dynamic.

With this we come to the end of this post. I hope this post has given you a good idea about the difference between HTML, XHTML and DHTML. Do provide your feedback and stay tuned for more…