Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all help is needed.

this stored proc check if user exist, if it exist it will not insert but if it doesnt exist it insert. But i want to return message(@ret) in asp.net label control, but nothing is showing on the label control.

somebody help.



This is my stored procedure:


ALTER procedure dbo.InsertName

@Cname nchar(10),
@Address nchar(10),
@ret varchar(100) output
as

if exists (select * from mytable where Cname=@Cname)

begin

select @ret as [ret]

set @ret='insert failed. Record exists already'

end

else

begin

insert into mytable values (@Cname,@Address)

select @ret as [ret]

set @ret='insert successful'

end



Here is my code behind:


protected void Button1_Click(object sender, EventArgs e)
{
string constr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True";
string result="";
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "InsertName";
cmd.Parameters.AddWithValue("@Cname", this.TextBox1.Text);
cmd.Parameters.AddWithValue("@Address", this.TextBox2.Text);
cmd.Parameters.AddWithValue("@ret", result);
cmd.Connection = conn;
cmd.Parameters["@ret"].Direction = ParameterDirection.InputOutput;

conn.Open();
cmd.ExecuteNonQuery();
Label1.Text = result;




It works, but it does not display the message in the label control.

Anybody tell me what i did not do right.
Posted

1 solution

you need

conn.Open();
cmd.ExecuteNonQuery();

result = cmd.Parameters["@ret"].Value as string;

Label1.Text = result;
 
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