Click here to Skip to main content
15,922,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I'm trying to run a simple insert query. But it throwing "Data type mismatch in criteria expression" Exception.

C#
public partial class AddVendor : Form
    {
        #region DB Connection

        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\AMS\AMS.accdb");

        #endregion
        public AddVendor()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //Declaration                
                string vendorName, website, email, contactPerson, address, city, state, country;
                double phNo, pCode;

                //Assigning the values with variables
                vendorName = Convert.ToString(textBoxVName.Text);
                website = Convert.ToString(textBoxWebsite.Text);
                email = Convert.ToString(textBoxEmail.Text);
                contactPerson = Convert.ToString(textBoxContactPerson.Text);
                address = Convert.ToString(textBoxAddress.Text);
                city = Convert.ToString(textBoxCity.Text);
                state = Convert.ToString(textBoxState.Text);
                country = Convert.ToString(textBoxCountry.Text);

                phNo = Convert.ToDouble(textBoxPhNo.Text);
                pCode = Convert.ToDouble(textBoxPCode.Text);

                con.Open();
                OleDbCommand cmd = new OleDbCommand("INSERT INTO Vendor(VendorName,PhoneNo,Website,Email,ContactPerson,Address,City,State,PinCode,Country) VALUES(@VendorName,@PhoneNo,@Website,@Email,@ContactPerson,@Address,@City,@State,@PinCode,@Country)", con);

                cmd.Parameters.AddWithValue("@VendorName", vendorName); 
                cmd.Parameters.AddWithValue("@PhoneNo", website);
                cmd.Parameters.AddWithValue("@Website", email);
                cmd.Parameters.AddWithValue("@Email", contactPerson);
                cmd.Parameters.AddWithValue("@ContactPerson", address);
                cmd.Parameters.AddWithValue("@Address", city);
                cmd.Parameters.AddWithValue("@City", state);
                cmd.Parameters.AddWithValue("@State", country);
                cmd.Parameters.AddWithValue("@PinCode", phNo);
                cmd.Parameters.AddWithValue("@Country", pCode);

                int result = cmd.ExecuteNonQuery();

                if (result == 1)
                {
                    MessageBox.Show("VendorDetails Inserted successfully");
                }
                else MessageBox.Show("VendorDetails is not Inserted");
                con.Close();

            }

            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


Please someone help me with this. I tried to check in internet none them helpful.
Thanks in advance.
Posted
Updated 15-May-15 20:38pm
v2
Comments
Member 11151142 16-May-15 3:08am    
Plz check all columns data type in database.Insted of double convert that field to string and check..
Sarath kumar.N 16-May-15 3:43am    
Hi, thanks for your time.
Actually phNo, pCode are 'number' data type in database.

1 solution

Um...did you check your parameters?
C#
cmd.Parameters.AddWithValue("@VendorName", vendorName);
cmd.Parameters.AddWithValue("@PhoneNo", website);
cmd.Parameters.AddWithValue("@Website", email);
cmd.Parameters.AddWithValue("@Email", contactPerson);
cmd.Parameters.AddWithValue("@ContactPerson", address);
cmd.Parameters.AddWithValue("@Address", city);
cmd.Parameters.AddWithValue("@City", state);
cmd.Parameters.AddWithValue("@State", country);
cmd.Parameters.AddWithValue("@PinCode", phNo);
cmd.Parameters.AddWithValue("@Country", pCode);
Aren't you putting the wrong values in the wrong parameters, except for the vendor name?
The phone number is getting the website, the website is getting the email, ...
 
Share this answer
 
Comments
Sarath kumar.N 16-May-15 5:04am    
Hi OriginalGriff,

Thanks for your help. I changed that, but it now throws another exception "Overflow".

Please help me with this.
OriginalGriff 16-May-15 5:09am    
Where does it throw the exception? And what is the exact error message?
Sarath kumar.N 16-May-15 5:20am    
I restarted visual studio, it's working fine thanks.
OriginalGriff 16-May-15 5:26am    
You're welcome!

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