Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code
C#
protected void Button2_Click (objectsender, EventArgs e)
 {
     connection();
     query = "Emplogin";   //stored procedure Name
     SqlCommand com = new  SqlCommand(query, con);
     com.CommandType= CommandType.StoredProcedure;
     com.Parameters.AddWithValue("@Usename", TextBox1.Text.ToString());   //for username 
     com.Parameters.AddWithValue("@Password", TextBox2.Text.ToString());  //for password
 
     int usercount = (Int32)com.ExecuteScalar();// for taking single value
 
     if (usercount == 1)  // comparing users from table 
     {
         Response.Redirect("Welcome.aspx");  //for sucsseful login
     }
     else
     {
         con.Close();
         Label1.Text = "Invalid User Name or Password";  //for invalid login
     }
}


i want to create project like single time only use con.open() and con.close() entire project, because of each and every query should have the connection open and close, so the connection open close using as function, please give the code of the execute function ?
that is possible ??

Regards
AR
Posted
Updated 29-Jul-21 2:14am
v4
Comments
Ron Beyer 1-Jan-14 0:51am    
Why? How would you then open the connection? Why would you want to skip these these things?
An@nd Rajan10 1-Jan-14 1:03am    
sorry, update my question you can solve it...

No you can not skip con.open() method , because it is the way that your application can communicate with the database.

And if you should use it try following code.
C#
if(con.state==connectionstate.close)
       con.open();



The .Net Framework mantains a connection pool for a reason. Trust it! :) You don't have to write so much code just to connect to the database and release the connection.

You can just use the 'using' statement and rest assured that 'IDBConnection.Release()' will close the connection for you.

C#
using (SqlConnection conn = new SqlConnection (...))
{
    // Whatever happens in here, the connection is 
    // disposed of (closed) at the end.
}


In this code you dont need to close connection anymore.
 
Share this answer
 
v3
Comments
An@nd Rajan10 1-Jan-14 1:07am    
sorry update my question, please see !
Dave Kreskowiak 1-Jan-14 1:11am    
That's the wrong way to check to see if the connection needs to be opened. There are more states to the connection other than Open and Closed. There's also Broken, Connecting, Fetching and Executing.
Are you asking if you can open a connection at the start of your application and leave it open for the life of the application?? VERY bad idea! The connection requires a connection license to the SQL Server and those don't come cheap. Hogging a connection and doing nothing with it will get you some very nasty messages from your SQL Server people.

Best practice is to use have the connection open only for as long as you need it. This allows a customer to use fewer connection licenses (and save money!) and allows for much better scaling of the load imposed by the application.

Open as late as possible, run your query and close as early as possible.
 
Share this answer
 
v2
Comments
An@nd Rajan10 1-Jan-14 1:19am    
please give you worked code, thats may be helpful me !!
Dave Kreskowiak 1-Jan-14 1:24am    
There are literally hundreds of thousands of examples of this all over the web. Learn to do research instead of asking to be spoon-fed copy-n-paste code. You'll last longer at this job if you learn to do research.

Google for "C# SqlConnection open".
Just to add, putting all this code in your presentation layer is a bad idea. It seems likely that only one button will ever perform a login, but what if you want to TEST your login code ? How do you do that, if it's inside an event handler in your presentation layer ? What if you do this with a proc that you then want to call in three places ? What if that proc gets an extra parameter ? Suddenly you're searching everywhere for all the places that call the proc, instead of only having one.
 
Share this answer
 
Comments
An@nd Rajan10 1-Jan-14 2:38am    
please give sample code for your way !!!
Christian Graus 1-Jan-14 2:42am    
I'm afraid that's impossible. What I've told you requires you to think for yourself, instead of just copying and pasting.

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