我将此作为存储过程
ALTER procedure [dbo].[editUserInfo]
(@userid int, @firstName varchar(50), @lastName varchar(50), @contact varchar(50), @address varchar(50), @info varchar(100), @username varchar(50), @password varchar(50), @rid int)
as
update dbo.userLogin
set u_firstName=@firstName, u_lastName=@lastName, u_contact=@contact, u_address=@address, u_info=@info, u_username=@username, u_password=@password, r_id=@rid
where u_id=@userid
我有以下代码
在点击事件后点击save
按钮
protected void saveBtn_Click(object sender, EventArgs e)
{
String i = Session["id"].ToString();
userInfo usr = new userInfo();
usr.setFirstName(firstNameTxt.Text);
usr.setLastName(lastNameTxt.Text);
usr.setAddress(addressTxt.Text);
usr.setContact(contactTxt.Text);
usr.setInfo(infoTxt.Text);
usr.setRole(rolebox.SelectedValue);
usr.setUserName(userNameTxt.Text);
usr.setPassword(passwordTxt.Text);
usr.setId(i);
userEditService usredit = new userEditServiceImp();
Boolean ok = usredit.editUserInfo(usr);
if (ok == true)
{
Response.Redirect("viewUser.aspx");
}
else
{
String msg = "error occured";
System.Diagnostics.Debug.WriteLine(msg);
}
}
处理数据库的代码如下
public Boolean editUserInfo(userInfo u)
{
String fn = u.getFirstName();
String ln = u.getLastName();
String add = u.getAddress();
String cont = u.getContact();
String un = u.getUserName();
String pass = u.getPassword();
String info = u.getInfo();
String r = u.getRole();
String i = u.getId();
int rid = Convert.ToInt16(r);
int id = Convert.ToInt16(i);
try
{
dbConnection dbcon = new dbConnection();
con = dbcon.doConnection();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "editUserInfo";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@firstName",fn);
cmd.Parameters.AddWithValue("@lastName",ln);
cmd.Parameters.AddWithValue("@contact",cont);
cmd.Parameters.AddWithValue("@address",add);
cmd.Parameters.AddWithValue("@info",info);
cmd.Parameters.AddWithValue("@username",un);
cmd.Parameters.AddWithValue("@password",pass);
cmd.Parameters.AddWithValue("@rid",rid);
cmd.Parameters.AddWithValue("@userId",id);
if (cmd.ExecuteNonQuery() == 1)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
con.Close();
}
return false;
}
因此cmd.ExecuteNonQuery()
继续捕获异常,它就像这样“Error converting data type nvarchar to int"
。我为桌面应用程序尝试了相同的代码,它工作正常......没有任何错误或异常。
所以我希望你们帮我解决这个问题。 我会非常乐于助人。 也要提前感谢...
答案 0 :(得分:5)
对于OleDbCommand,我认为应该按照它们在存储过程中出现的顺序添加参数。
您可以尝试移动“cmd.Parameters.AddWithValue(”@ userId“,id);”成为第一个参数。