Saturday, November 29, 2008

Weird thing with inner queries in SQL SERVER 2005

Few days ago, I found a very strange thing with SQL SERVER 2005 and I strongly feel that THIS IS A BUG in SQL SERVER which actually can create lots of data discrepancy.

To prove my point I have created a sample SQL Script. I want you to run the script in SQL SERVER.

Details of the Script

· Create new database.


· Use this Database.


· Create a table named tblCategory

CREATE TABLE tblCategory
CategoryNAME VARCHAR(50)

· Create another table named tblProduct

CategoryID INT FOREIGN KEY REFERENCES tblCategory(CategoryID),

· Insert 5 rows in tblCategory

INSERT INTO tblCategory VALUES (1,'Category1')
INSERT INTO tblCategory VALUES (2,'Category2')
INSERT INTO tblCategory VALUES (3,'Category3')
INSERT INTO tblCategory VALUES (4,'Category4')
INSERT INTO tblCategory VALUES (5,'Category5')

· Insert 10 rows in tblProduct

INSERT INTO tblProduct VALUES (1,1,0)
INSERT INTO tblProduct VALUES (2,1,0)
INSERT INTO tblProduct VALUES (3,2,0)
INSERT INTO tblProduct VALUES (4,2,0)
INSERT INTO tblProduct VALUES (5,3,0)
INSERT INTO tblProduct VALUES (6,3,0)
INSERT INTO tblProduct VALUES (7,4,0)
INSERT INTO tblProduct VALUES (8,4,0)
INSERT INTO tblProduct VALUES (9,4,0)
INSERT INTO tblProduct VALUES (10,5,0)

· Select statement to confirm the data is inserted or not.

SELECT * FROM tblCategory
SELECT * FROM tblProduct

· Here is a select query which is not correct. The query tells select ProductID from tblCategory table where CategoryID = 1. But the problem here is tblCategory is not having a column named ProductID. So this query throws an error and that's a correct behaviour.

SELECT ProductID FROM tblCategory WHERE CategoryID = 1

· Here is the magic. I have used the above select query which is not correct as a inner query with a update statement. What to do you think? What will happen? Update query should throw an error. Logically it should.I was thinking the same But just execute this update query and you will be shocked.

UPDATE tblProduct SET IsCompleted = 1 WHERE ProductID IN (SELECT ProductID FROM tblCategory WHERE CategoryID = 1)

· Oops, 10 rows affected. Surpised. All the data in IsCompleted field is set to 1 but my inner query (SELECT ProductID FROM tblCategory WHERE CategoryID = 1) is wrong.

· This is just a sample query but When I executed such a similiar statement, 3364 rows in my table was updated.

SELECT * FROM tblCategory
SELECT * FROM tblProduct

I think this is a bug but actaully it's not. Please read comments for more information on this..

Saturday, October 11, 2008

Java script function to get browser name..

We have this requirement in most of the application to know that which browser user is using to view the web site. We can use javascript to know browser name and this function works with all the broswers...

function GetBrowser()
           if (/Firefox[\/\s](\d+\.\d+)/.
                 var ffversion=new Number(RegExp.$1) 
                 if (ffversion>=3)
                  alert("FF 3.x or above")
                 else if (ffversion>=2)
                  alert("FF 2.x")
                 else if (ffversion>=1)
                  alert("FF 1.x")
            else if(/Chrome[\/\s](\d+\.\d+)/.

           else if(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent))
            return false;

 window.onload  = GetBrowser;


Saturday, September 27, 2008

Disabling button at client side before firing server side event

If we want to disable the button when clicked until it's execute the server side code then check out this link....


Firefox 3.0 now supports showModalDialog()

Javascript function window.showModalDialog() is used to create a modal dialog box that displays the specified HTML document. A modal dialog box, is kind of dialog box which retains the focus while open. The user cannot switch back to parent windows until the dialog box is closed.

This works fine in Internet explorer 4.0 and later version of IE but with Firefox 2.0 or below version it was not working. But there is a good news now that Firefox 3.0 supports showModalDialog() function.

Check out this link..

Test this page in Firefox 2 and Firefox 3

Chrome (Google's new browser) also supports showModalDialog().

Virendra Dugar

Saturday, September 20, 2008

Fetch random rows through SQL Query

If you need to fetch random rows from a SQL server table and you have an integer column then using RAND() function goes well. However in some cases there is no number column. In such cases how to fetch random rows? SQL Server provides one more mechanism to access random rows:

FROM <table>
WHERE <criteria>

The key is the use of NEWID() function that returns a GUID.

As for example:


Enjoy Programming....

Monday, June 16, 2008

Text Box and EnableViewState set to false.

Before some days I was just exploring that How EnableviewState works. I noticed one thing that EnableViewState is not working with the textbox. I tried this example

I put a text box with EnableViewState= “false” and a button on the page.

<asp:textbox id="TextBox1" enableviewstate="False" runat="server"/>
<asp:button id="Button1" onclick="Button1_Click" runat="server" text="Button"/>

I entered some value in the text box and When I clicked on the button, the page got refreshed but the text box was maintaining its value.
It was a bit weird thing for me.

After hours of hard work, I came to know this.“Textboxes do not use ViewState to maintain their values across postbacks.These controls simply post their values as normal to the server and then the server simply takes that value and makes it be the default value of the new control generated for the postback. You can see this if you view the source code of the page after the initial form submission, the textbox will have a 'value' attribute set to the data entered into the textbox when it was submitted.”

For more information on this, check out these links..
Microsoft Link
Joteke's Blog


Sunday, March 30, 2008

What's New in ASP.NET 3.5

Developers who are facing ASP.NET 3.5 for the first time are likely to wonder what happened to ASP.NET 2.0 and 3.0. It does not exist. Microsoft used the name .NET Framework to release new technologies- most notably, WPF (Windows Presentation Foundation) a new user interface technology for building rich clients, WCF( Windows Communication foundation) A technology for building message-oriented services and WF(Windows workflow foundation) a technology that allows you to model a complex business process as a series of actions. However, the .NET framework 3.0 doesn’t include a new version of the CLR or ASP.NET. Instead, the next release of ASP.NET was rolled into the .NET Framework 3.5.

(Taken from Scott Mitchell Blog)

Compared to ASP.NET 2.0, ASP.NET 3.5 is a more gradual evolution. Its new features are.

  • Integrated ASP.NET AJAX support,
  • New Controls
  • LINQ
  • ASP.NET Merge Tool
  • New Assemblies


In ASP.NET 2.0, ASP.NET AJAX was used as an extension to it. You had to download the extensions and install it. However in ASP.NET 3.5, ASP.NET AJAX is integrated into the .NET Framework.

New Controls

The ListView and DataPager are new controls added along with a new datasource control called the LinqDataSource.


The ListView control is quiet flexible and contains features of the Gridview, Datagrid, Repeater and similar list controls available in ASP.NET 2.0. It provides the ability to insert, delete, page (using Data Pager), sort and edit data. It looks interesting because it basically allows you much more control over the layout than a DataGrid does while still giving you many of the more advanced features of the data grid.However one feature of the ListView control that stands apart, is that it gives you a great amount of flexibility over the markup generated. So you have a complete control on how the data is to be displayed. You can now render your data without using the

tag. You also get a rich set of templates with the ListView control.


DataPager provides paging support to the ListView control. The best advantage is that you need not have to keep it ‘tied’ with the control on which the paging is being done. You can keep it anywhere on the page.DataPager gives you a consistent way of paging with the controls that support it. Currently only ListView supports it as it implements the IPageableItemContainer. However support is likely to be added to other List controls as well.

Take a look at this example of ListView and DataPager in ASP.NET 3.5.


LINQ (Language Integrated Query) adds native data querying capability to C# and VB.NET along with the compiler and Intellisense support. LINQ is a component of .NET 3.5. LINQ defines operators that allow you to code your query in a consistent manner over databases, objects and XML. The ASP.NET LinqDataSource control allows you to use LINQ to filter, order and group data before binding to the List controls.

ASP.NET Merge Tool

ASP.NET 3.5 includes a new merge tool (aspnet_merge.exe). This tool lets you combine and manage assemblies created by aspnet_compiler.exe. This tool was available earlier as an add-on.

New Assemblies

The new assemblies that would be of use to ASP.NET 3.5 developers are as follows:· System.Core.dll - Includes the implementation for LINQ to Objects·
System.Data.Linq.dll - Includes the implementation for LINQ to SQL·
System.Xml.Linq.dll - Includes the implementation for LINQ to XML· System.Data.DataSetExtensions.dll - Includes the implementation for LINQ to DataSet· System.Web.Extensions.dll: Includes the implementation for ASP.NET AJAX (new enhancements added) and new web controls as explained earlier.

Some Other Important Points
1. ASP.NET 3.5 provides better support to IIS7. IIS7 and ASP.NET 3.5 modules and handlers support unified configuration.
2. You can have multiple versions of ASP.NET on the same machine.
3. For those who are wondering what happened to ASP.NET 3.0, well there isn’t anything called ASP.NET 3.0.
4. VS 2002 worked with ASP.NET 1.0, VS 2003 worked with ASP.NET 1.1, and VS 2005 worked with ASP.NET 2.0. However VS 2008 supports multi-targeting, i.e it works with ASP.NET 2.0, and ASP.NET 3.5.


How to make Web Setup in .NET 2.0

For making WebSetup with .NET 2.0, one need to download webdeployment.msi file from microsoft website. Advantage of webdeployment is, it copies only the design file not the code behind file. If you try to create the web setup without the webdeployment project added code behind file will also go in the setup.

Visual Studio 2005 Web Deployment Projects provide additional functionality to build and deploy Web sites and Web applications in ASP.NET 2.0 and Visual Studio 2005. This add-in includes a tool to merge the assemblies created during ASP.NET 2.0 pre-compilation, and provides a comprehensive UI within Visual Studio 2005 to manage build configurations, merging, and using pre-build and post-build tasks with MSBuild.

For more details about creating the Web Setup using web deployment project take a look at this link.

Sunday, January 20, 2008

Update Panel ,Default Button and FireFox

ASP.Net 2.0 comes up with a new concept called "Default Button" which is one button on the form will work as default button. Whenever we press the enter button default button will be called.

As for example: In a text box after typing you press enter your default button's event will be fired.

However, the one thing that was bothering me with the Default Button is it worked like a charm in IE. In Firefox, it behaved like the weirdly. The problem wasn't that it didn't do a postback. That it did. What it didn't do was hit the server side event (the button click event).
When you are selected in any textbox and you press enter, IE will submit this and hit the button click even. However with FireFox, it will only submit - not call the onclick event.

To solve this add: UseSubmitBehavior="False" to your submit button. Now you can press enter whenever you want in your form and it will do the proper submit regardless of IE or Firefox.

Happy Programming…
back to top