How to catch a stored procedures exceptions in a code behind using C#.Net?

Posted by Rajendra.prasad under C# on 4/18/2010 | Views : 4523 | Status : [Member] | Replies : 2
Hi All,
How to catch a stored procedures exceptions in a code behind using C#.Net? how to display a stored procedures exceptions in code behind using C#.Net.

Regards,
Rajendra




Responses

Posted by: Vuyiswamb on: 4/19/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
lets say you want to Execute a StoredProcedure in your DAL, you will have something like this


public voidDisable_Grid_Contents()
{
strcon = GetConnectionString(DB);

con = new SqlConnection(strcon);

cmdselect = new SqlCommand();

cmdselect.CommandText = "dbo.Get_Pattern";

cmdselect.CommandTimeout = 0;

cmdselect.CommandType = CommandType.StoredProcedure;

cmdselect.Connection = con;


try
{
con.Open();

cmdselect.ExecuteNonQuery();
}
catch (SqlException ex)
{
lblErrorMessage.Text = ex.Message; //Your Error willl be display in a Label
}
finally
{
if (con != null)
{
con.Close();
}

}
}


Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Muhilan on: 4/20/2010 [Member] Starter

Up
0
Down
use SQLExceptions to catch a stored procedures exceptions

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

Login to post response