Re: A question about order of execution?

From: John Saunders (johnwsaundersiii_at_notcoldmail.com)
Date: 07/15/04


Date: Thu, 15 Jul 2004 13:58:02 -0400


"Blaxer" <Blaxer@discussions.microsoft.com> wrote in message
news:4BDDBA8A-A129-4F9A-AFA5-C51875991CCD@microsoft.com...
> I have a question about what I think is a problem with order of execution
of some asp.net code. I have searched on this topic and haven't found any
answeres which leads me to believe that I am not looking for the right
thing, that being said if someone could help me with this or just point me
in the right direciton I would appreciate it.
>
> The problem is a bit difficult to explain. I have a few examples of where
this happens but here is a code example that I know, sometimes does not
function the way I would expect it to.
>
>
> Public Class Page
> Public Shared Database_Table_Name as String
>
>
> Public Sub Fill_Datagrid()
>
> Dim SQL_CMD as SQLCommand = New SQLCommand("Select * from " &
Database_Table_Name & " where active = 1"
> Dim DR As SQLDataReader = SQL_CMD.ExecuteDataReader
> DG.Datasource = SQL_CMD
> DG.Databind()
>
> End Sub
>
>
> Public Sub Button1_Click (byval blah blah) Handles Button1.Click
>
> Database_Table_Name = Textbox1.text
> Fill_Datagrid()
>
> End Sub
>
>
>
> End Class
>
> When I execute code LIKE this, I often get a SQL error because the
Database_Table_Name variable is not set "yet". It is, however, set in the
sense that if I now set a label to the variable in the button code it
appears just fine.
>
> Now I know good programming would dictate you should pass the table name
to the function, but this is an example to show my problem, not good
technique. Again I appreciate any help / explanation anyone can provide.

Why are you using a Shared variable? It will be shared amongst all users of
your form, whereas Textbox1.Text will only come from the current request.
Try removing "Shared" and see what happens.

Also, take a look at the following for something else you should change:

"Protecting Against SQL Injection"
(http://www.winnetmag.com/Article/ArticleID/42216/42216.html)

-- 
John Saunders
johnwsaundersiii at hotmail