Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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();

}
Posted
Updated 4-Jul-12 3:33am
v2
Comments
[no name] 4-Jul-12 9:24am    
"i have used this catch block its not working" It's not working is not an acceptable description of any kind of problem. Do you take your car to a mechanic and tell him "it's broke" and expect him to know exactly what the problem is?

"Also microsoft link is in VB.NET"
So what?
[no name] 4-Jul-12 9:35am    
"Can any one tell me" Sure, simple. Read the answers to your previous exact same question.
shashavali 4-Jul-12 10:25am    
thanks for remembering me i got it

1 solution

change error severioty to 18or above.....


RAISERROR( @ErrorMessage,10,1);
RAISERROR( @ErrorMessage,18,1);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900