Click here to Skip to main content
15,886,782 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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 = "";
}
Posted
Updated 4-Jun-18 7:48am

1 solution

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:
C#
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();
 
Share this answer
 
v3
Comments
Prateek gsharma 5-Jun-18 1:05am    
ok
Prateek gsharma 5-Jun-18 1:59am    
can i use cmd.Parameters.Add for insert command?
Prateek gsharma 5-Jun-18 11:23am    
same error is displaying sir
Thomas Daniels 5-Jun-18 11:51am    
Looks like I missed something when writing the answer... ignore what I said initially, I updated my answer and now it should be correct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900