|
-
Jul 24th, 2006, 08:16 AM
#1
Thread Starter
Fanatic Member
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.
-
Jul 24th, 2006, 08:48 AM
#2
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..
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|