Exception Handling in Ado.net

Posted by Chilverupavan under C# on 5/25/2010 | Views : 2484 | Status : [Member] | Replies : 1
hi,,

how to handle exceptionhnadling for the below code..........any one can plz help me...............

String cs31 = ("server=ABCD;database=abc;uid=sa;pwd=abc");
SqlConnection con31 = new SqlConnection(cs31);
con31.Open();
SqlCommand cmd131 = new SqlCommand("select count(*) from course where coursename='" + textcname.Text + "'", con31);
int y = (int)cmd131.ExecuteScalar();
if (y == 1)
{
label3.Text = "CourseName Already Exits...";
return;
}
con31.Close();

these code shoul handle in exception...




Responses

Posted by: Vuyiswamb on: 5/25/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Change your Code and it should look like this

String cs31 = ("server=ABCD;database=abc;uid=sa;pwd=abc"); 

SqlConnection con31 = new SqlConnection(cs31);
SqlCommand cmd131 = new SqlCommand("select count(*) from course where coursename='" +
textcname.Text + "'", con31);
try
{

con31.Open();
int y = (int)cmd131.ExecuteScalar();

if (y == 1)
{
label3.Text = "CourseName Already Exits...";
return;
}

}
catch(SQlException ex)
{

label3.Text = ex.Message(); //show your Exception is there was any
}
Finally
{
con31.Close(); //Clear the Connection
}

Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Chilverupavan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response