column count does not much value count at row 1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Muca Mega
    New Member
    • Mar 2012
    • 1

    column count does not much value count at row 1

    Hi guys,
    am having trouble with this error code while trying to insert certain values to the database. Can somebody help?

    Code:
    if (e.ColumnIndex == 4)
                    {
    
                        try{
                        
                    string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                    string item = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                    string quantity = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                    string UnitCost = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
    
                    int quant = int.Parse(quantity);
    
                    double unicost = double.Parse(UnitCost);
                    double tot=quant*unicost;
    
                        try
                        {
                            MySqlConnection connection = new MySqlConnection(Generals.ConnectionString);
                            connection.Open();
                            string sql = "INSERT INTO sales VALUES ('" + id + "','" + item + "','" + quant + "','" + unicost + "','" + DateTime.Now.ToString() + "','"+tot+"')";
                            MySqlCommand cmd = new MySqlCommand(sql, connection);
                            cmd.ExecuteNonQuery();
                            connection.Close();
                            MessageBox.Show("New Sales is added successfully", "Hotel Management", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Refresh();
    Last edited by PsychoCoder; Mar 30 '12, 11:30 PM. Reason: Code tags added
  • RhysW
    New Member
    • Mar 2012
    • 70

    #2
    If ID is set to be an auto incrementing number in the database that could be your problem as it needs the first parameter in the INSERT INTO to be an empty set of quotes '' instead of you manually entering the id, though this is only true if it is an auto-incrementing id.

    looking at your code it could well be that you have the database set up to have an auto-incrementing number but youre ignoring it for the use of your own one taken from the table,

    perhaps a better explanation of what i am trying to say can be found here

    Documentation

    Comment

    Working...