cmd.executenonquery中没有给出一个或多个必需参数错误的值 [英] No value given for one or more required parameters error at cmd.executenonquery

查看:105
本文介绍了cmd.executenonquery中没有给出一个或多个必需参数错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

插入操作时出错



此外,如果我删除con1.open();我收到数据类型不匹配表达式标准错误。





我也不删除con1.open();然后我得到连接没有关闭



我尝试过:



private void button1_Click(object sender,EventArgs e)

{

使用(OleDbConnection con1 = new OleDbConnection(con))

{

OleDbCommand cmd = new OleDbCommand();



string query1 =INSERT INTO Spldetails([Date],[EmpId],[EmpName] ,[指定],[记录],[Splwrkhrs],[团队],[Splwrk],[Splwrkdetails],[Allottedby])VALUES(@ d1,@ v1,@ v2,@ dg,@ rd,@ shr,@ tm,@ sw,@ sd,@ ab);

con1.Open();

OleDbDataAdapter da = new OleDbDataAdapter(cmd);

da.SelectCommand.Parameters.Add(@ v1,OleDbType.Variant,30).Value = this.textBox1.Text;

da.SelectCommand.Parameters.Add(@ v2 ,OleDbType.Variant,100).Value = this.textBox2.Text;

da.Sele ctCommand.Parameters.Add(@ dg,OleDbType.Variant,50).Value = this.textBox3.Text;

da.SelectCommand.Parameters.Add(@ rd,OleDbType.Variant ,15).Value = Convert.ToDouble(this.textBox4.Text);

da.SelectCommand.Parameters.Add(@ shr,OleDbType.Variant,15).Value = Convert.ToDouble (this.textBox5.Text);

da.SelectCommand.Parameters.Add(@ tm,OleDbType.Variant,100).Value = this.textBox8.Text;

da.SelectCommand.Parameters.Add(@ d1,OleDbType.Date).Value = this.dateTimePicker1.Text;

da.SelectCommand.Parameters.Add(@ ab,OleDbType .Variant,100)。Value = this.comboBox1.Text;

da.SelectCommand.Parameters.Add(@ sw,OleDbType.Variant,100).Value = this.comboBox2.Text;

da.SelectCommand.Parameters.Add(@ sd,OleDbType.Variant,300).Value = this.textBox6.Text;



cmd =新的OleDbC ommand(query1,con1);





cmd.ExecuteNonQuery();



MessageBox.Show(祝贺....你的详细信息已成功提交);

textBox4.Text =;

textBox5.Text = ;

textBox6.Text =;

comboBox1.Text =;

comboBox2.Text =;

}

解决方案

您将参数放在 OleDbDataAdapter 中,但您无法使用 da 传递参数(另外,你不应该使用SelectCommand,而是插入命令);看起来你实际上并不需要OleDbDataAdapter所以你可以放弃 da 并使用 cmd.Parameters :

  string  query1 =   INSERT INTO Spldetails([Date],[EmpId],[EmpName],[Designation],[Records],[Splwrkhrs],[Team],[Splwrk],[Splwrkdetails], [Allottedby])VALUES(@ d1,@ v1,@ v2,@ dg,@ rd,@ shr,@ tm,@ sw,@ sd,@ ab); 
con1.Open();
OleDbCommand cmd = new OleDbCommand(query1,con1);
cmd.Parameters.Add( @ v1,OleDbType.Variant, 30 )。Value = this .textBox1.Text;
// 其他参数
cmd.ExecuteNonQuery();


getting error while insert operation

moreover if i remove the con1.open(); i am getting data type mismatch expression criteria error.


also i dont remove con1.open(); then i am getting connection was not closed

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
using (OleDbConnection con1 = new OleDbConnection(con))
{
OleDbCommand cmd = new OleDbCommand();

string query1 = "INSERT INTO Spldetails([Date],[EmpId],[EmpName],[Designation],[Records],[Splwrkhrs],[Team],[Splwrk],[Splwrkdetails],[Allottedby]) VALUES(@d1,@v1,@v2,@dg,@rd,@shr,@tm,@sw,@sd,@ab)";
con1.Open();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.SelectCommand.Parameters.Add("@v1", OleDbType.Variant, 30).Value = this.textBox1.Text;
da.SelectCommand.Parameters.Add("@v2", OleDbType.Variant, 100).Value = this.textBox2.Text;
da.SelectCommand.Parameters.Add("@dg", OleDbType.Variant, 50).Value = this.textBox3.Text;
da.SelectCommand.Parameters.Add("@rd", OleDbType.Variant, 15).Value = Convert.ToDouble(this.textBox4.Text);
da.SelectCommand.Parameters.Add("@shr", OleDbType.Variant, 15).Value = Convert.ToDouble(this.textBox5.Text);
da.SelectCommand.Parameters.Add("@tm", OleDbType.Variant, 100).Value = this.textBox8.Text;
da.SelectCommand.Parameters.Add("@d1", OleDbType.Date).Value = this.dateTimePicker1.Text;
da.SelectCommand.Parameters.Add("@ab", OleDbType.Variant, 100).Value = this.comboBox1.Text;
da.SelectCommand.Parameters.Add("@sw", OleDbType.Variant, 100).Value = this.comboBox2.Text;
da.SelectCommand.Parameters.Add("@sd", OleDbType.Variant, 300).Value = this.textBox6.Text;

cmd = new OleDbCommand(query1, con1);


cmd.ExecuteNonQuery();

MessageBox.Show("Congratulations....Your Details have been submitted successfully");
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";
}

解决方案

You put your parameters in an OleDbDataAdapter but you are nowhere using da to pass the parameters (also, you shouldn't use SelectCommand anyway, but InsertCommand); it looks like you don't actually need the OleDbDataAdapter so you can drop da and just set the parameters using cmd.Parameters:

string query1 = "INSERT INTO Spldetails([Date],[EmpId],[EmpName],[Designation],[Records],[Splwrkhrs],[Team],[Splwrk],[Splwrkdetails],[Allottedby]) VALUES(@d1,@v1,@v2,@dg,@rd,@shr,@tm,@sw,@sd,@ab)";
con1.Open();
OleDbCommand cmd = new OleDbCommand(query1, con1);
cmd.Parameters.Add("@v1", OleDbType.Variant, 30).Value = this.textBox1.Text;
// other parameters too
cmd.ExecuteNonQuery();


这篇关于cmd.executenonquery中没有给出一个或多个必需参数错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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