Can any one tell me of how to handle RAISERROR messages raised in my
Stored Procedure in the ASP .NET and display it in my aspx web pages? Any
help is appreciated.
stored procedure is shown below
ALTER PROCEDURE [CuS].[InsertCustomer]
@Cname varchar (50),
@ImageName varchar (50),
@imagepath varchar(50),
@uploadedby varchar(50)
As
BEGIN
DECLARE @count INT
SET @count = (SELECT COUNT(*) FROM UUDetails)
IF @count >= 20
BEGIN
DECLARE @ErrorMessage NVARCHAR(4000);
set @ErrorMessage = 'u canot enter more than 20 records';
RAISERROR( @ErrorMessage,10,1);
return
END
ELSE
INSERT INTO UUDetails(UserName,ImageName,ImagePath,UploadedBy,UploadedDate)values(@Cname,@ImageName,@imagepath,@uploadedby,GETDATE() )
RETURN scope_Identity()
END
and in code behind page is shown below
try
{
SqlConnection con;
SqlCommand cmd;
SqlParameter ret;
// FileUpload1.SaveAs(filepath);
con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
FileUpload1.SaveAs(Server.MapPath("Images/" + filename));
cmd = new SqlCommand("CuS.InsertCustomer", con);
cmd.CommandType = CommandType.StoredProcedure;
ret = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
ret.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(ret);
cmd.Parameters.Add("@Cname", SqlDbType.VarChar, 50).Value = txtcustname.Text;
cmd.Parameters.Add("@ImageName", SqlDbType.VarChar, 50).Value = filename;
// cmd.Parameters.Add("@imagepath", SqlDbType.VarChar).Value = "User/Image" + filename;
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
cmd.Parameters.Add("@Uploadedby", SqlDbType.VarChar, 50).Value = txtuploadedBy.Text;
con.Open();
cmd.ExecuteNonQuery();
int j = Convert.ToInt32(ret.Value);
con.Close();
if (j >= 0)
{
Label1.Text = "<b1>Record sucessfully Inserted";
gvCustomer.DataBind();
}
clear();
}
catch (SqlException ex)
{
Label1.Text = ex.Message.ToString();
}