简体   繁体   中英

Data Mismatch Type Error in c# windows Form application

So, I am trying to insert data in a table called prices but an Exception of mismatch data is thrown. This is the code i did to insert

foreach (DataGridViewRow row in dgvMultipleItems.SelectedRows)
        {
            #region Insert Data Into The Database

            cmd.Parameters.AddWithValue("@ID", OleDbType.Numeric);
            cmd.Parameters["@ID"].Value = row.Cells[0].Value.ToString();
            cmd.Parameters.AddWithValue("@Company", OleDbType.VarChar);
            cmd.Parameters["@Company"].Value = row.Cells[1].Value.ToString();
            cmd.Parameters.AddWithValue("@PriceStructure", OleDbType.VarChar);
            cmd.Parameters["@PriceStructure"].Value = row.Cells[2].Value.ToString();
            cmd.Parameters.AddWithValue("@ItemNumber", OleDbType.VarChar);
            cmd.Parameters["@ItemNumber"].Value = row.Cells[3].Value.ToString();
            cmd.Parameters.AddWithValue("@Description", OleDbType.VarWChar);
            cmd.Parameters["@Description"].Value = row.Cells[4].Value.ToString();

            cmd.Parameters.AddWithValue("@Unit", OleDbType.VarChar);
            cmd.Parameters["@Unit"].Value = row.Cells[5].Value.ToString();
            cmd.Parameters.AddWithValue("@Price", OleDbType.Numeric);
            cmd.Parameters["@Price"].Value = row.Cells[6].Value.ToString();
            cmd.Parameters.AddWithValue("@Discount", OleDbType.Double);
            cmd.Parameters["@Discount"].Value = row.Cells[7].Value.ToString();
            cmd.Parameters.AddWithValue("@FluidType", OleDbType.VarChar);
            cmd.Parameters["@FluidType"].Value = row.Cells[8].Value.ToString();
            cmd.Parameters.AddWithValue("@SubContractor", OleDbType.VarChar);
            cmd.Parameters["@SubContractor"].Value = row.Cells[9].Value.ToString();
            cmd.Parameters.AddWithValue("@Discount_SubContract", OleDbType.Double);
            cmd.Parameters["@Discount_SubContract"].Value = row.Cells[10].Value.ToString();
            cmd.Parameters.AddWithValue("@Price_SubContract", OleDbType.Numeric);
            cmd.Parameters["@Price_SubContract"].Value = row.Cells[11].Value.ToString();
            try
            {
                conn.Open();
                i = cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }

the database is access and the types are as following: ID= Number, Company= Short Text, PriceStructure = Short Text, ItemNumber = Short Text, Description = Long Text, Unit = Short Text, Price= Number, Discount = Number, FluidType = Short Text, SubContractor = Short Text, Discount_SubContract = Number, and Price_SubContract= Number. Can someone help me tracing the error?

You are adding the parameters the wrong way. Dont use AddWithValue . Use Add instead:

        cmd.Parameters.Add("@ID", OleDbType.Numeric);
        cmd.Parameters["@ID"].Value = Convert.ToInt32(row.Cells[0].Value);
        cmd.Parameters.Add("@Company", OleDbType.VarChar);
        cmd.Parameters["@Company"].Value = row.Cells[1].Value.ToString();
        //... and so on...

AddWithValue is used to add parameters without specifying the data type, and since you are already specifying, the most accurate method will be Add , where you do have to specify. A correct use of AddWithValue would be:

cmd.Parameters.AddWithValue("@ID", row.Cells[0].Value.ToString());

Anyway, IMHO AddWithValue shouldn´t be used since it can lead you to have problems with data types conversions:

http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/

EDIT: You also seem to be having a lot of problem with casting the values as what is expected from the database. You should cast to a similar data type of what is expected. For example:

        //THIS IS WRONG!
        cmd.Parameters.AddWithValue("@Price", OleDbType.Numeric);
        cmd.Parameters["@Price"].Value = row.Cells[6].Value.ToString();

        //THIS IS OK!
        cmd.Parameters.AddWithValue("@Price", OleDbType.Numeric);
        cmd.Parameters["@Price"].Value = Convert.ToInt32(row.Cells[6].Value);         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM