Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know this a very newbie question... so lets see:

I define global variables:

CSS
string strConn;
     SqlCommand cmd;
     SqlConnection conn;



and then on page_load:

MSIL
strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
       conn = new System.Data.SqlClient.SqlConnection(strConn);
        conn.Open();


The problem is with public static functions I cant do:

cmd = new SqlCommand("sql query..", conn);


because I get: at (cmd and conn)

An object reference is required for the nonstatic field, method, or property

how can I solve this problem ?

SOLVED:

Yes I know it was a newbie question (altough I need more than one hour to know how to do it).

C#
public static SqlCommand cmd;
     public static SqlConnection conn;
Posted
Updated 3-Jan-11 10:33am
v6

1 solution

Still stumbling around I see :rolleyes:

If you had picked up a book, any book that has a chapter on ADO.NET, you would see how to do this. I'll give you a hint; it doesn't involve global variables and does make use of the using method.
 
Share this answer
 
v2
Comments
Maxdd 7 3-Jan-11 16:39pm    
Yes, but I think I'm now stumbling very less than before :)

I picked a book already. Altough I had to read it in time record because I have a deadline to what. For now I will finish what i'm doing (i'm in debugging and optimizing now phases now), next time I'll for sure remember to pick up a book previously.

For now, I'm doing the best I can with the time I have.
Maxdd 7 3-Jan-11 16:41pm    
By the way, I suppose your solution involve a class and/or a connectDB.cs file to import :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900