Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying to insert values into acess database by parameterised method


private void button1_Click(object sender, EventArgs e)
           {
               if (validationcontrol())
               {


                   MessageBox.Show(cmbjobcode.SelectedValue.ToString());
                   OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                   oleDbConnection1.Open();
                   OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO quotationmastertable (quotationcode ,jobcode , jobpk , sillabordercharges , battabordercharges , driverpayment , rent , extra , total , discount , remark ,amount ) Values (?,?,?,?,?,?,?,?,?,?,?,?) ", oleDbConnection1);
                   oleDbCommand1.Parameters.Add(txtquotationno.Text);
                   oleDbCommand1.Parameters.Add(cmbjobcode.Text);
                   oleDbCommand1.Parameters.Add(cmbjobcode.SelectedValue);
                   oleDbCommand1.Parameters.Add(int.Parse(txtsilabordercharges.Text));
                   oleDbCommand1.Parameters.Add(int.Parse(txtbattacharges.Text));
                   oleDbCommand1.Parameters.Add(int.Parse(txtdriverpayment.Text));
                   oleDbCommand1.Parameters.Add(int.Parse(txtrent.Text));
                   oleDbCommand1.Parameters.Add(int.Parse(txtextra.Text));
                   oleDbCommand1.Parameters.Add(int.Parse(txttotal.Text));
                   oleDbCommand1.Parameters.Add(int.Parse(txtdiscount.Text));
                   oleDbCommand1.Parameters.Add(txtremark.Text);
                   oleDbCommand1.Parameters.Add(int.Parse(txtamount.Text));
                   oleDbCommand1.CommandType = CommandType.Text;
                   oleDbCommand1.ExecuteNonQuery();
                   oleDbConnection1.Close();
                   MessageBox.Show(txtquotationno.Text);

               }
           }


but I am getting exception at the first line itself


oleDbCommand1.Parameters.Add(txtquotationno.Text);
exception is

The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not String objects.

I am new to programming. Can anyone help by pointing out my mistakes?
Posted
Updated 22-Feb-12 10:28am
v2

Of course. The exception message is quite informative, you should just follow it.

You need to understand that a parameter is not a parameter value. It is essentially a meta-data description of the parameter which helps the ADO.NET to process actual parameter value when you substitute the value in the parametrized query.

In this way, the method System.Data.OleDb.OleDbCommand.Parameters.Add expect the parameter of the type System.Data.OleDb.OleDbParameter. Please see:
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx[^],
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx[^].

You probably need to understand how it all works in principle. Please read:
http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx[^].

—SA
 
Share this answer
 
Hi there..
Try this.

C#
private void button1_Click(object sender, EventArgs e)
            {
                if (validationcontrol())
                {
    
    
                    MessageBox.Show(cmbjobcode.SelectedValue.ToString());
                    OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                    oleDbConnection1.Open();
                    OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO quotationmastertable (quotationcode ,jobcode , jobpk , sillabordercharges , battabordercharges , driverpayment , rent , extra , total , discount , remark ,amount ) Values(@quotationcode ,@jobcode , @jobpk , @sillabordercharges , @battabordercharges , @driverpayment , @rent , @extra , @total , @discount , @remark ,@amount ) ", oleDbConnection1);
                    oleDbCommand1.Parameters.Add("@quotationcode", txtquotationno.Text);
                    oleDbCommand1.Parameters.Add("@jobcode", cmbjobcode.Text);
                    oleDbCommand1.Parameters.Add("@jobpk", cmbjobcode.SelectedValue);
                    oleDbCommand1.Parameters.Add("@sillabordercharges", int.Parse(txtsilabordercharges.Text));
                    oleDbCommand1.Parameters.Add("@battabordercharges", int.Parse(txtbattacharges.Text));
                    oleDbCommand1.Parameters.Add("@driverpayment", int.Parse(txtdriverpayment.Text));
                    oleDbCommand1.Parameters.Add("@rent", int.Parse(txtrent.Text));
                    oleDbCommand1.Parameters.Add("@extra", int.Parse(txtextra.Text));
                    oleDbCommand1.Parameters.Add("@total", int.Parse(txttotal.Text));
                    oleDbCommand1.Parameters.Add("@discount", int.Parse(txtdiscount.Text));
                    oleDbCommand1.Parameters.Add("@remark", txtremark.Text);
                    oleDbCommand1.Parameters.Add("@amount", int.Parse(txtamount.Text));
                    oleDbCommand1.CommandType = CommandType.Text;
                    oleDbCommand1.ExecuteNonQuery();
                    oleDbConnection1.Close();
                    MessageBox.Show(txtquotationno.Text);
    
                }
            }
 
Share this answer
 

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