Closed Thread
Page 2 of 2 FirstFirst 1 2
Results 11 to 20 of 20

Thread: C# database question

  1. #11
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    reply

    that seems to work, just like my original code, but I continue to get this in the upper left hand corner of the page:

    database connecteddatabase not connectedThread was being aborted.

    Quote Originally Posted by jmurrayhead View Post
    Alright, now give this a shot:
    Code:
    String set = ConfigurationManager.ConnectionStrings["peebman2000"].ConnectionString;
     
    datacommand.Connection = new SqlConnection(set);
    

  2. #12
    Barn Frequenter lewy is on a distinguished road lewy's Avatar
    Join Date
    Mar 2008
    Posts
    108
    Rep Power
    4

    Your connection string should look like this:
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["peebman2000"].ToString());

  3. #13
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    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?

  4. #14
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    reply

    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.

    Quote Originally Posted by Wolffy View Post
    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?

  5. #15
    Barn Frequenter lewy is on a distinguished road lewy's Avatar
    Join Date
    Mar 2008
    Posts
    108
    Rep Power
    4

    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:
    Code:
    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);
    }
    
    This code will ensure that your connections are closed properly and that any exceptions are caught

  6. #16
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    reply

    Okay Lewy that worked, i tried several times and when I hit the button I get:
    database connected
    from the response.write.

    Thanks for the help, but one question. What does this snippit of code do?
    Code:
    SqlConnection.ClearPool(datacommand.Connection);
    
    I don't understand what this is doing.

    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);
                 }
            } 
        }
    



    Quote Originally Posted by lewy View Post
    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:
    Code:
    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);
    }
    
    This code will ensure that your connections are closed properly and that any exceptions are caught

  7. #17
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    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.


  8. #18
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    reply

    Okay thanks for the explaination jmurray. I'm done practicing C# for today. You guys may see me again tommorrow.

    thanks.

    Quote Originally Posted by jmurrayhead View Post
    SqlConnection.ClearPool(datacommand.Connection); is used for cleanup. Essentially, ClearPool clears the connection pool that is associated with the connection.

  9. #19
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    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.

  10. #20
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Quote Originally Posted by Wolffy View Post
    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.
    It is my opinion that Response.End is not needed, particularly when using Try...Catch blocks. My primary reason being that you can do everything you wanted using Response.End within the Catch portion of Try...Catch
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


Closed Thread
Page 2 of 2 FirstFirst 1 2

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO