Results 1 to 2 of 2

Thread: Check for Error in connecting to SQL server

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Check for Error in connecting to SQL server

    hi! guyz!!! i have a question here..Currently, i have this code below
    Code:
     
                    string sqlConnStr;
                    sqlConnStr = @"Data Source = DAIMOUS\SQLEXPRESS; Initial Catalog=BCMD; User ID=dbuser; Password=dexxa69++";
                    SqlConnection sqlConn = new SqlConnection(sqlConnStr);
                    sqlConn.Open();
    
                    string qryStr = "select Max(id) as u_id from users";
    
                    SqlCommand sqlCmd = new SqlCommand(qryStr, sqlConn);
    
    
                    SqlDataReader reader = sqlCmd.ExecuteReader();
    
                    while (reader.Read())
                    {
                        MessageBox.Show((string)reader["u_id"].ToString ());
                    }
    
                    reader.Close();
                    sqlConn.Close();
    as you can see i dont have a way to check if there's an error like, Server or the Initial Catalog doesn't exist or the username or password is invalid..So is there a way for me to do that? aside from using exceptions. Thanks in advance!!
    Last edited by daimous; Jul 24th, 2006 at 08:20 AM.

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Check for Error in connecting to SQL server

    Use
    VB Code:
    1. try
    2.             {
    3.                 //code here
    4.             }
    5.             catch (System.Data.SqlClient.SqlException ex)
    6.             {
    7.                 //catch the exception here
    8.             }

    Edit-- I posted VB.NET Try Catch syntax..
    Last edited by Shuja Ali; Jul 24th, 2006 at 08:57 AM.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

Posting Permissions

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



Click Here to Expand Forum to Full Width