显示此错误的原因参数化查询'(@ id int,@ name nvarchar(4000),@ age nvarchar(4000),@ phoneno nvarc'需要参数'@gender',这是未提供的。 [英] why this error shown The parameterized query '(@id int,@name nvarchar(4000),@age nvarchar(4000),@phoneno nvarc' expects the parameter '@gender', which was not supplied.
问题描述
我写了一个用于更新数据库中数据的代码
我的班级代码stafag是
class staffAG
{
string _name;
int _id;
string _age;
string _phone;
string _address;
string _post;
string _gender;
public string name
{
get
{
return _name;
}
set
{
_name = value ;
}
}
public int id
{
get
{
return _id;
}
set
{
_id = value ;
}
}
public string age
{
get
{
return _age;
}
set
{
_age = value ;
}
}
public string phone
{
get
{
return _phone;
}
set
{
_phone = value ;
}
}
public string 地址
{
get
{
return _address;
}
set
{
_address = value ;
}
}
public string post
{
get
{
return _post;
}
set
{
_post = value ;
}
}
公开 字符串性别
{
get
{
return _gender;
}
set
{
_gender = value ;
}
}
}
}
和我班级员工的编码是
public void updatestaff(staffAG j)
{
cn.Open();
cmd = new SqlCommand( 更新员工set name = @ name,age = @ age,phoneno = @ phoneno,address = @ address,post = @ post,gender = @gender where id = @id,cn);
cmd.Parameters.AddWithValue( @ id,j.id);
cmd.Parameters.AddWithValue( @ name,j.name);
cmd.Parameters.AddWithValue( @ age,j.age);
cmd.Parameters.AddWithValue( @ phoneno,j.phone);
cmd.Parameters.AddWithValue( @ address,j.address);
cmd.Parameters.AddWithValue( @ post,j.post);
cmd.Parameters.AddWithValue( @ gender,j.gender);
cmd.ExecuteNonQuery();
cn.Close();
{
MessageBox.Show( 已添加);
}
}
更新按钮的编码是
private void button1_Click( object sender,EventArgs e)
{
Staff s = new Staff();
staffAG j = new staffAG();
j.id = comboBox2.SelectedIndex;
j.name = textBox2.Text;
j.age = textBox3.Text;
j.phone = textBox4.Text;
j.address = textBox5.Text;
j.post = comboBox1.Text;
s.updatestaff(j);
MessageBox.Show( 记录更新);
}
但是当我按下更新按钮时会显示此错误
引用:参数化查询'(@ id int,@ name nvarchar(4000),@ age nvarchar(4000),@ phoneno nvarc'需要参数' @gender',没有提供。
请帮助我..
您没有设置性别
属性!
员工s = new Staff();
staffAG j = new staffAG();
j.id = comboBox2.SelectedIndex;
j.name = textBox2.Text;
j.age = textBox3.Text;
j.phone = textBox4.Text;
j.address = textBox5.Text;
j.post = comboBox1.Text;
j.gender = ???
s.updatestaff(j);
public void updatestaff(staffAG j)
你试图通过将参数@gender设置为 j.gender来传递参数
但是在你的函数private void button1_Click(object sender,EventArgs e)
你还没有将j.gender设置为任何值。
仅仅因为从未设置过性别的价值:
private void button1_Click( object sender,EventArgs e)
{
Staff s = new Staff();
staffAG j = new staffAG();
j.id = comboBox2.SelectedIndex;
j.name = textBox2.Text;
j.age = textBox3.Text;
j.phone = textBox4.Text;
j.address = textBox5.Text;
j.post = comboBox1.Text;
s.updatestaff(j);
MessageBox.Show( 记录更新);
}
您设置了staffAG实例的其他属性,但由于您没有设置性别,因此它保持为空。
Null作为参数传递给不能为null的列,将抛出异常。
BTW:C#中的命名标准是初始大写属性的特征 - 坚持使用这些标准是个好主意。
I write a coding for updating data in database
coding of my class stafag's is
class staffAG
{
string _name;
int _id;
string _age;
string _phone;
string _address;
string _post;
string _gender;
public string name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public int id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string age
{
get
{
return _age;
}
set
{
_age = value;
}
}
public string phone
{
get
{
return _phone;
}
set
{
_phone = value;
}
}
public string address
{
get
{
return _address;
}
set
{
_address = value;
}
}
public string post
{
get
{
return _post;
}
set
{
_post = value;
}
}
public string gender
{
get
{
return _gender;
}
set
{
_gender = value;
}
}
}
}
and coding of my class staff is
public void updatestaff(staffAG j)
{
cn.Open();
cmd = new SqlCommand("update staff set name = @name,age = @age,phoneno = @phoneno,address = @address,post = @post,gender = @gender where id = @id", cn);
cmd.Parameters.AddWithValue("@id", j.id);
cmd.Parameters.AddWithValue("@name",j.name);
cmd.Parameters.AddWithValue("@age",j.age);
cmd.Parameters.AddWithValue("@phoneno",j.phone);
cmd.Parameters.AddWithValue("@address",j.address);
cmd.Parameters.AddWithValue("@post",j.post);
cmd.Parameters.AddWithValue("@gender",j.gender);
cmd.ExecuteNonQuery();
cn.Close();
{
MessageBox.Show("Added");
}
}
coding of my update button is
private void button1_Click(object sender, EventArgs e)
{
Staff s = new Staff();
staffAG j = new staffAG();
j.id = comboBox2.SelectedIndex;
j.name = textBox2.Text;
j.age = textBox3.Text;
j.phone = textBox4.Text;
j.address = textBox5.Text;
j.post = comboBox1.Text;
s.updatestaff(j);
MessageBox.Show("Record updated");
}
but when i press the update button this error is displayed
Quote:The parameterized query '(@id int,@name nvarchar(4000),@age nvarchar(4000),@phoneno nvarc' expects the parameter '@gender', which was not supplied.
please help me..
You did not set upgender
property!
Staff s = new Staff(); staffAG j = new staffAG(); j.id = comboBox2.SelectedIndex; j.name = textBox2.Text; j.age = textBox3.Text; j.phone = textBox4.Text; j.address = textBox5.Text; j.post = comboBox1.Text; j.gender = ??? s.updatestaff(j);
In your functionpublic void updatestaff(staffAG j)
you are attempting to pass in the parameter @gender by setting it toj.gender
but in yourfunction private void button1_Click(object sender, EventArgs e)
you have not set j.gender to any value.
Simply because the value of gender is never set:
private void button1_Click(object sender, EventArgs e) { Staff s = new Staff(); staffAG j = new staffAG(); j.id = comboBox2.SelectedIndex; j.name = textBox2.Text; j.age = textBox3.Text; j.phone = textBox4.Text; j.address = textBox5.Text; j.post = comboBox1.Text; s.updatestaff(j); MessageBox.Show("Record updated"); }
You set the other properties of the staffAG instance, but since you don't set gender, it remains null.
Null passed as a parameter to a column that can't be null, will throw an exception.
BTW: The standards for naming in C# is for an initial uppercase character for properties - it's a good idea to stick with such standards.
这篇关于显示此错误的原因参数化查询'(@ id int,@ name nvarchar(4000),@ age nvarchar(4000),@ phoneno nvarc'需要参数'@gender',这是未提供的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!