Sql Exception未被用户代码处理.... [英] Sql Exception was unhandled by user code....

查看:71
本文介绍了Sql Exception未被用户代码处理....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误就是这样......

Sql异常未被用户代码处理....

过程或函数sp_userinformation指定的参数太多。 />






我写了这样的存储过程



创建proc sp_userinformation



@username varchar(20),

@password varchar(20 ),

@firstname varchar(20),

@lastname varchar(20),

@email varchar(20),
@phoneno varchar(20),

@location varchar(15),

@created_by varchar(20))

as

插入用户信息(用户名,密码,名字,姓氏,电子邮件,phoneno,location,created_by)值(@ username,@ password,@ firstname,@ lastname,@ email,@ phoneno,@ location,@ created_by)





我写了这样的代码隐藏文件





protected void btnsubmit_Click(object sender,EventArgs e)

{

if(txtpwd.Text == txtcnmpwd.Text)

{

string UserName = txtuser.Text;

string Password = txtpwd.Text;

string ConfirmPassword = txtcnmpwd.Text;

string FirstName = txtfname.Text;

string LastName = txtlname.Text;

string Email = txtEmail.Text;

string Phoneno = txtphone.Text;

string Location = txtlocation.Text ;

string Created_By = txtuser.Text;

int count = 0;

SqlConnection con = new SqlConnection(Data Source =(local) ; Initial Catalog = sample; Integrated Security = True);

con.Open();

SqlCommand cmd = new SqlCommand(sp_userinformation,con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue(@ UserName,UserName);

cmd.Parameters.AddWithValue( @Password,密码);

cmd.Parameters.AddWithValue(@ FirstName,FirstName);

cmd.Parameters.AddWithValue(@ Las tName,LastName);

cmd.Parameters.AddWithValue(@ Email,电子邮件);

cmd.Parameters.AddWithValue(@ PhoneNo,Phoneno);

cmd.Parameters.AddWithValue(@ Location,Location);

cmd.Parameters.AddWithValue(@ Created_By,Created_By);

cmd.Parameters.Add(@ ERROR,SqlDbType.Char,500);

cmd.Parameters [@ ERROR]。Direction = ParameterDirection.Output;

count = cmd.ExecuteNonQuery();

message =(string)cmd.Parameters [@ ERROR]。值;



if(count> 0)

{

txtuser.Text =;

txtpwd.Text =;

txtcnmpwd.Text =;

txtfname.Text =;

txtlname.Text =;

txtEmail .Text =;

txtphone.Text =;

txtlocation.Text =;



}



con.Close();



}

否则

{

Page.RegisterStartupScript(UserMsg,< Script language ='' javascript''>提醒(''+密码不匹配+'');< / script>);

}

// Page。 RegisterStartupScript(UserMsg,< Script language =''javascript''> window.alert(''+ message +'');< / script>);

lblErrorMsg.Text = message;

}



执行此操作时,

错误就像这样...... 。

Sql异常未被用户代码处理....

过程或函数sp_userinformation指定的参数太多。





我是初学者....请指导我解决这个问题...



提前致谢。 ..

Error has coming like this...
Sql Exception was unhandled by user code....
Procedure or function sp_userinformation has too many arguments specified.



I have written stored procedure like this

create proc sp_userinformation
(
@username varchar(20),
@password varchar(20),
@firstname varchar(20),
@lastname varchar(20),
@email varchar(20),
@phoneno varchar(20),
@location varchar(15),
@created_by varchar(20))
as
insert into userinformation(username,password,firstname,lastname,email,phoneno,location,created_by)values (@username,@password,@firstname,@lastname,@email,@phoneno,@location,@created_by)


I have written codebehind file like this


protected void btnsubmit_Click(object sender, EventArgs e)
{
if (txtpwd.Text == txtcnmpwd.Text)
{
string UserName = txtuser.Text;
string Password = txtpwd.Text;
string ConfirmPassword = txtcnmpwd.Text;
string FirstName = txtfname.Text;
string LastName = txtlname.Text;
string Email = txtEmail.Text;
string Phoneno = txtphone.Text;
string Location = txtlocation.Text;
string Created_By = txtuser.Text;
int count = 0;
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=sample;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("sp_userinformation", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", UserName);
cmd.Parameters.AddWithValue("@Password", Password);
cmd.Parameters.AddWithValue("@FirstName", FirstName);
cmd.Parameters.AddWithValue("@LastName", LastName);
cmd.Parameters.AddWithValue("@Email", Email);
cmd.Parameters.AddWithValue("@PhoneNo", Phoneno);
cmd.Parameters.AddWithValue("@Location", Location);
cmd.Parameters.AddWithValue("@Created_By", Created_By);
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
count = cmd.ExecuteNonQuery();
message = (string) cmd.Parameters["@ERROR"].Value;

if (count>0)
{
txtuser.Text = "";
txtpwd.Text = "";
txtcnmpwd.Text = "";
txtfname.Text = "";
txtlname.Text = "";
txtEmail.Text = "";
txtphone.Text = "";
txtlocation.Text = "";

}

con.Close();

}
else
{
Page.RegisterStartupScript("UserMsg", "<Script language=''javascript''>alert(''" + "Password mismatch" + "'');</script>");
}
//Page.RegisterStartupScript("UserMsg", "<Script language=''javascript''>window.alert(''" + message + "'');</script>");
lblErrorMsg.Text = message;
}

while executing this,
Error has coming like this...
Sql Exception was unhandled by user code....
Procedure or function sp_userinformation has too many arguments specified.


I am the beginner.... Kindly guide me to solve this problem...

Thanks in advance...

推荐答案

创建proc sp_userinformation



@username varchar(20),

@password varchar(20),

@firstname varchar(20),

@lastname varchar(20),

@ email varchar(20),

@phoneno varchar(20),

@location varchar(15),

@created_by varchar(20),

@ERROR varchar(500)输出 - 输出参数



as

开始

开始尝试

开始交易trTest

插入用户信息(用户名,密码,名字,姓氏,电子邮件,phoneno,location,created_by)值(@ username,@ password,@ firstname,@ lastname,@ email,@ phoneno,@ location,@ created_by)

提交事务trTest

结束尝试

开始捕获

如果@@ trancount> 0

回滚事务trTest

选择@ERROR = ERROR_MESSAGE()

结束捕获

结束
create proc sp_userinformation
(
@username varchar(20),
@password varchar(20),
@firstname varchar(20),
@lastname varchar(20),
@email varchar(20),
@phoneno varchar(20),
@location varchar(15),
@created_by varchar(20),
@ERROR varchar(500)output -- out put parameter
)
as
begin
begin try
begin transaction trTest
insert into userinformation(username,password,firstname,lastname,email,phoneno,location,created_by)values (@username,@password,@firstname,@lastname,@email,@phoneno,@location,@created_by)
commit transaction trTest
end try
begin catch
if @@trancount >0
rollback transaction trTest
select @ERROR =ERROR_MESSAGE()
end catch
end


通常当你发生这个错误传递的参数多于所需的参数。

因此,首先验证每个参数。以下代码是从sp获取输出参数。



Generally this error occurs when you are passing more parameters than the required.
So,Verify each and every parameter first.And the following code is to get the output parameter from sp.

create proc sp_userinformation
(
@username varchar(20),
@password varchar(20),
@firstname varchar(20),
@lastname varchar(20),
@email varchar(20),
@phoneno varchar(20),
@location varchar(15),
@created_by varchar(20)),
@ERROR char(500) output--add this in your sp
as
insert into userinformation(username,password,firstname,lastname,email,phoneno,location,created_by)values (@username,@password,@firstname,@lastname,@email,@phoneno,@location,@created_by) 

set @ERROR='Sucessfully Inserted' --(or your message)



和codebehind.cs




and in codebehind.cs

SqlParameter error_msg=New SqlParameter("@ERROR",sqldbtype.char,500);
error_msg.Direction=ParameterDirection.Output;
cmd.parameters.add(error_msg);



执行后



你可以得到输出参数的值




after the execution

you can get the value of output parameter

string message=error_msg.value.ToString();


我想你.cs文件代码你正在使用输出参数

cmd.Parameters [@ ERROR]。Direction = ParameterDirection.Output;

但你还没有声明存储过程中的输出参数。

所以你必须在存储过程中声明输出参数。
i think u in .cs file code u are using output parameter
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
but u have not declared output parameter in your stored procedure .
so u must declare output parameter in your stored procedure .


这篇关于Sql Exception未被用户代码处理....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆