问题描述
您需要所有帮助.
此存储的proc检查用户是否存在,如果存在,则不会插入,但如果不存在,则插入.但是我想在asp.net标签控件中返回message(@ret),但是标签控件上没有任何显示.
有人帮忙.
这是我的存储过程:
ALTER过程dbo.InsertName
@Cname nchar(10),
@Address nchar(10),
@ret varchar(100)输出
如
如果存在(从mytable中选择*,其中Cname = @ Cname)
开始
选择@ret作为[ret]
设置@ ret =''插入失败.记录已经存在''
结束
其他
开始
插入到mytable值中(@ Cname,@ Address)
选择@ret作为[ret]
设置@ ret =插入成功''
结束
这是我的代码背后:
受保护的void Button1_Click(对象发送者,EventArgs e)
{
string constr =数据源=.\\ SQLEXPRESS; AttachDbFilename = | DataDirectory | \\ Database.mdf;集成安全性= True;用户实例= True";
字符串result =";
SqlConnection conn =新的SqlConnection(constr);
SqlCommand cmd =新的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",结果);
cmd.Connection = conn;
cmd.Parameters ["@ ret"].Direction = ParameterDirection.InputOutput;
conn.Open();
cmd.ExecuteNonQuery();
Label1.Text =结果;
它可以工作,但不会在标签控件中显示该消息.
有人告诉我我做错了什么.
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.
推荐答案
conn.Open();
cmd.ExecuteNonQuery();
result = cmd.Parameters["@ret"].Value as string;
Label1.Text = result;
这篇关于如何在asp.net标签控件中检查用户是否存在并返回消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!