Parameterized query to insert values

I tried to insert values ​​into an Access database using a parameterized query:

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 get an exception in the first line:

oleDbCommand1.Parameters.Add(txtquotationno.Text);

The exception is

OleDbParameterCollection accepts only nonzero objects of type OleDbParameter, not String objects.

I am new to programming; can anyone help in pointing out my mistakes?

+1
source share
2 answers

The only parameter for the object Addis the OleDBParameter object. You just pass rows and data.

A simple solution would be to use a method AddWithValue:

oleDbCommand1.Parameters.AddWithValue("?", txtquotationno.Text);
oleDbCommand1.Parameters.AddWithValue("?", cmbjobcode.Text);

OleDB , , . do , , .

+4

. ( OleDbType.VarChar, 50 db.

oleDbCommand1.Parameters.Add("@quot", OleDbType.VarChar, 50).Value =  txtquotationno.Text;

msdn: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx

0

All Articles