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!!
Re: Check for Error in connecting to SQL server
Use
VB Code:
try
{
//code here
}
catch (System.Data.SqlClient.SqlException ex)
{
//catch the exception here
}
Edit-- I posted VB.NET Try Catch syntax.. :bigyello: