使用存储过程将数据插入数据库时遇到问题? [英] getting problem while inserting data into database using stored procedure?
问题描述
protected void btnregister_Click(object sender,EventArgs e)
{
if(Page.IsValid)
{
string cs = ConfigurationManager.ConnectionStrings [SampleConnectionString]。ConnectionString;
using(SqlConnection con = new SqlConnection(cs) ))
使用(SqlCommand cmd = new SqlCommand(spRegisterUser,con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ Username,txtusername.Text);
cmd.Parameters.AddWithValue(@ Password,txtpassword.Text);
cmd.Parameters.AddWithValue(@ Email,txtemail.Text);
con.Open();
int count =(int) cmd.ExecuteScalar();
if(count == -1)
{
Label1.Text =用户ID不可用,请选择另一个; < br $>
}
其他
{
Response.Redirect(〜/ Login .aspx);
}
}
和存储过程是
protected void btnregister_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string cs = ConfigurationManager.ConnectionStrings["SampleConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
using(SqlCommand cmd = new SqlCommand("spRegisterUser", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", txtusername.Text);
cmd.Parameters.AddWithValue("@Password", txtpassword.Text);
cmd.Parameters.AddWithValue("@Email", txtemail.Text);
con.Open();
int count = (int)cmd.ExecuteScalar();
if ( count == -1)
{
Label1.Text = "User id is not avalilable please choose another one";
}
else
{
Response.Redirect("~/Login.aspx");
}
}
and Stored procedure is
CREATE Procedure spRegisterUser
@Username nvarchar(50),
@Password nvarchar(50),
@Email nvarchar(50)
As
Begin
Declare @Count int
Declare @Returncount int
Select @Count = COUNT(Username)
from tblUsers Where Username=@Username
if @Count > 0
Begin
Set @Returncount = -1
End
Else
Begin
set @Returncount = 1
Insert into tblUsers values(@Username,@Password,@Email)
End
Select @Returncount as ReturnValue
End
i不知道我没有得到任何异常和价值没有得到存储。请帮帮我这个代码有什么问题
i don't know iam not getting any exception and the values not getting stored. please help me is there anything wrong with that code
推荐答案
给出正确的存储过程名称并将CommandType设置为StoredProcedure
Give correct Stored procedure name and set CommandType as StoredProcedure
SqlCommand cmd = new SqlCommand("spRegisterUser", con);
cmd.CommandType = CommandType.StoredProcedure;
代码:
Code:
using (SqlConnection con = new SqlConnection(cs))
using(SqlCommand cmd = new SqlCommand("spRegisterUser", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", txtusername.Text);
cmd.Parameters.AddWithValue("@Password", txtpassword.Text);
cmd.Parameters.AddWithValue("@Email", txtemail.Text);
con.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar());
}
我做了这个要求..尝试这个任务..我的意思是检查存储过程和逻辑肯定会得到结果......
http: //www.stuffuser.in/2014/04/grid-view-crud-operations-using.html [ ^ ]
I did the requirement.. Try this task..I mean Check stored procedure and logic definitely you will get the result...
http://www.stuffuser.in/2014/04/grid-view-crud-operations-using.html[^]
这篇关于使用存储过程将数据插入数据库时遇到问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!