Your connection string should look like this:SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["peebman2000"].ToString());
Since you are getting both messages, I think it's possible that your Response.End() may be throwing an exception -- you Catch block will catch every exception. (Tho why the second Response.End() doesn't throw another exception, I don't know).
Have you tried stepping through this in the debugger and seeing what happens?
Thanks wolffy, i'm not real good at using the debugger in visual studio 05, I usually use response.write's to debug my code, even though in this case i'm not using response.write's to debug the code. I just want to know if the code is allowing to connect to the database or not.
I'll try puting a break point and see if I can understand some of the things the debugger throughs at me.
Since you're learning, it's important to learn how to handle connections an such the right way:
Make sure that you use try ... catch blocks for your db related access as shown below:This code will ensure that your connections are closed properly and that any exceptions are caughtCode:SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["peebman2000"].ToString()); try { connection.Open(); SqlCommand command = new SqlCommand(YourSQLStatement, connection); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { Response.Write(reader["YourDbField"].ToString() + "<br>"); } } else Response.Write("There are no matching Records"); } catch (SqlException ex) { Response.Write(ex.TargetSite.ToString() + "<br />ERROR MESSAGE <br /> " + ex.Message.ToString() + "<br />STACK TRACE <br />" + ex.StackTrace.ToString()); } finally { connection.Close(); SqlConnection.ClearPool(connection); }
Okay Lewy that worked, i tried several times and when I hit the button I get:
from the response.write.database connected
Thanks for the help, but one question. What does this snippit of code do?
I don't understand what this is doing.Code:SqlConnection.ClearPool(datacommand.Connection);
Let me know and thanks all the help guys again.
Here is my updated code.
Code:protected void Button1_Click(object sender, EventArgs e) { { SqlDataAdapter dataadapter; SqlCommand datacommand; dataadapter = new SqlDataAdapter(); datacommand = new SqlCommand();//ConnectionStringSettings String set = ConfigurationManager.ConnectionStrings["peebman2000"].ToString(); datacommand.Connection = new SqlConnection(set); try { datacommand.Connection.Open(); Response.Write("database connected"); Response.End(); } catch (SqlException ex) { Response.Write("database not connected" + ex.Message); } finally { datacommand.Connection.Close(); SqlConnection.ClearPool(datacommand.Connection); } } }
SqlConnection.ClearPool(datacommand.Connection); is used for cleanup. Essentially, ClearPool clears the connection pool that is associated with the connection.
jmurrayhead
If you agree, give me rep.
If you like it here...throw us a few bones to help support us.
In some quick surfing on Reponse.End(), there does some to be some anecdotal evidence that it causes problems in ASP.NET, especially in Try..Catch blocks. Having never used it in .NET, I'm going to do some more research.
jmurrayhead
If you agree, give me rep.
If you like it here...throw us a few bones to help support us.
Bookmarks