5
Answers

Insert data into database error

james james

james james

7y
266
1
I am getting an error while inserting data.
 
The error is "Number of query values and destination fields are not the same".
 
I have read alot about ppl missing some single qoutes but I must be overlooking them.
 
My code :
 
private void btn_Insert_Click(object sender, EventArgs e)
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Table values('" + txt_Date + "','" + txt_Units + "','" + txt_TimeOrd + "','" + txtTimeRec + "','" + txt_Panel + "', '" + txt_Box +"','" + txt_Name + "', '" + txt_Notes +"')";
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record inserted Successfully");
}
}
}
Answers (5)
0
Hemant Kumar

Hemant Kumar

NA 3k 215.1k 13y
If your column type is of number type(such as double), the formating would do the work no matter in edit mode or not.
If your column type is of string type which is by default, you shoud handle the cellFormating event to do a trick work like below:

Code Snippet

private void DgvCellStyle_Load(object sender, EventArgs e)

        {

            DataTable dt = new DataTable();

            dt.Columns.Add("a");

            dt.Rows.Add(155.6565);

            dt.Rows.Add(2342.2);

            this.dataGridView1.DataSource = dt;

 

            this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);

        }

 

        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

        {

            if (e.ColumnIndex == 0 && e.RowIndex != this.dataGridView1.NewRowIndex)

            {

                double d = double.Parse(e.Value.ToString());

                e.Value = d.ToString("N2");

            }

        }