I have a simple stored procedure which inserts a users record in a table and should give out its userid.
我有一个简单的存储过程,它将用户记录插入表中,并应该给出其用户ID。
Here is the stored procedure:
这是存储过程:
Alter proc spRegistration
@UserId nvarchar(10) output,
@Name nvarchar(20),
@EmailAdd nvarchar(20)
as
begin
Declare @Count int;
Declare @ReturnCode int
Select @Count = Count(EmailAdd)
from tblAllUsers
where EmailAdd = @EmailAdd
if @Count > 0
begin
Set @ReturnCode = -1
end
else
begin
Set @ReturnCode = 1
Insert into tblAllUsers(Name, EmailAdd)
values(@Name, @EmailAdd)
end
Select @UserId = UserId
from tblAllUsers
where EmailAdd = @EmailAdd
Select @ReturnCode as ReturnCode
end
I try to get the userid
into a textbox like below :
我尝试将用户标识放入如下文本框中:
string CS = ConfigurationManager.ConnectionStrings["hh"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("spRegistration", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@EmailAdd", txtEmailAddress.Text);
var UserID = cmd.Parameters.AddWithValue("@UserId", SqlDbType.NVarChar);
UserID.Direction = ParameterDirection.Output;
int ReturnCode = (int)cmd.ExecuteScalar();
if (ReturnCode == -1)
{
lblRegMessage.Text = "This email has already been registered with us.";
}
else
{
lblRegMessage.Text = "You were registered successfully.";
txtUserId.Text=(String)UserID.Value;
}
The table as well as the stored procedure is far too complex,I have simplified it for better understanding of problem.UserId is Alphanumeric.Dont worry about that.
表格和存储过程太复杂了,为了更好地理解问题,我简化了它.UserId是Alphanumeric.Dont担心。
Putting a break point shows a null against var UserID Where am i going wrong?
设置一个断点显示对null的用户ID我在哪里出错?
1 个解决方案
#1
1
You need to Use .ToString()
on UserID
您需要在UserID上使用.ToString()
txtUserId.Text= UserID.Value.ToString();
EDIT:
var UserID = cmd.Parameters.AddWithValue("@UserId", SqlDbType.NVarChar,50);
#1
1
You need to Use .ToString()
on UserID
您需要在UserID上使用.ToString()
txtUserId.Text= UserID.Value.ToString();
EDIT:
var UserID = cmd.Parameters.AddWithValue("@UserId", SqlDbType.NVarChar,50);