Thursday, August 30, 2007

Avoid Cross Threaded Calls..

I was developing a TCP/IP based Chat Application.

When i was sending the data from the server to the client using send method of Socket Class Object and on the client side i was receiving the data using the BeginReceive andEndReceive method of socket class.
Till here it's functioning properly but when i assigned the content sent by the server to the textbox .NET throws an exception
*Cross Threaded call are not allowed. ThisText box is defined in some other thread.*

I searched a lot on this to find the solution.

Finally I got the solution.

The solution is :-

The solution is to write
Control.CheckForIllegalCrossThreadCalls =false;
in Page Load
CheckForIllegalCrossThreadCalls is a static propertyin the load event handler of the main form.

(Don't worry if this property doesn't appear in the intellisense menu )

This problem happens because a thread other than the one who created a control is attempting to modify its properties.

Enjoy...

Difference Between RegisterClientScript & RegisterStartupScript

The main difference is that the RegisterStartupScript method places the JavaScript at the bottom of the ASP.NET page right before the closing /form element. The RegisterClientScriptBlock method places the JavaScript directly after the opening <form> element in the page.

Both of these methods take two strings as input. The second parameter, script , is the client-side script-including the opening and closing script tags. The first parameter, key , serves as a unique identifier for the inserted client-side script.


The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object's <form runat="server"> element.

The code cannot access any of the form's elements because, at that time, the elements haven't been instantiated yet. This explains why hdnView variable had a null value in my case. The RegisterStartupScript method inserts the specified client-side script just before the closing tag of the Page object's <form runat="server"> element.

The code can access any of the form's elements because, at that time, the elements have been instantiated. The choice of which method to use really depends on the "order" in which you want your script to be run by the browser when rendering the page.

As for example if you have declared a hidden variable and want to use in your java script by any of these method then java script method registred with RegisterClientScriptBlock method won't be able to find the hidden variable as the form elements are not instantiated yet.

Enjoy..



Wednesday, August 29, 2007

Retrieve Table Name from all the Procedures of any database

In Sql Server 2000 or later versions,

Suppose you have deleted one of the column of any table and now you have to change all your procedures where this column is used.

1. One way is to open every procedure and look for the table and the cloumn name.

2. Second solution is the

Select * from information_Schema.Routines where Routine_Definition like '%table1%'

This Query will allow to find out whether table name specified in where conditoin exists in which Procedures.

INFORMATION_SCHEMA.ROUTINES view is used to retrieve information about stored procedures. This view contains one row for each stored procedure accessible to the current user in the current database.

The INFORMATION_SCHEMA.ROUTINES view was introduced in SQL Server 2000. This view is based on the sysobjects, syscomments and other system tables.

Enjoy..

Tuesday, August 28, 2007

Java Script For IE and FireFox

Currently many browsers are available in the market that is making life hell for programmers.

If syntax A is supported by Browser A then this syntax is not supported by Browser B.

The one very interesting I encountered when I was working on one of my project that is how java script supported by browsers.

There is a slight difference how the HTML tags are rendered by IE and FireFox.

Look at this scenario


<parent>
<Child>Contnet of Child</Child>
</parent>

This is interpreted in different ways by FireFox and IE.

How IE interprets

IE considers <parent>has a single child node called <child>

How FireFox interprets

On every new line, FireFox inserts empty text node. In the above case FireFox considers there are 3 child nodes.

This is how these tags will be rendered in FireFox

<parent>
<Empty Text node 1>
<Child>Contnet of Child</Child>
<Empty Text node 2>
</parent>

So, you see, first Child is the for IE, but an Empty text Node for FireFox.

The solutions may be:-

1. Put Everything in a single line:- <parent> <Child>Contnet of Child</Child> </parent>

But it is very impractical solution.

2. Change the ways you access the nodes.

First of all determine the browser used by the user.


function checkBrowser()
{
var browser=navigator.appName;
if (browser == "Microsoft Internet Explorer" )
{
return 0;
}
else if (browser == "Netscape")
{
return 1;
}
}

Once the browser is determined then return respectively value of the index variable from the function.

Now this index variable will be used to access the control in Java Script.

function checkValue()
{
var index;
var rowNo = 1;
index = checkBrowser();
var o = document.getElementById('<%=dgSystemList.ClientID%>');
var chk1= o.rows[rowNo].cells[0].childNodes[index];
}

Note:-This function is just for sample.

One thing to notice there are two ways to access child nodes.

1. children
2. childNodes

The Problem with the first method is that it is not compatible with firefox. It is working fine for IE.

Second method works fine for both the browsers.

To make your code compatible with both the browsers use childNodes method.
Hope this helps…



back to top