Search code examples
c#winformsms-accessoledb

OverFlow Exception in OLEDB


I am getting an error while i am trying to insert a user id of my client in the ms access database.

The error i am getting is Overflow.

when i am trying to insert its getting the above error.

I am using these code.

        OleDbCommand cmd = new OleDbCommand("insert into UserInfo " + "([firstname], [lastname], [gender], [occupation], [expirydate], [UserId], [phoneno]) " + " values('" + txt_FirstName.Text + "','" + txt_LastName.Text + "','" + cmb_Gender.Text + "','" + cmb_Occupation.Text + "','" + txt_expiryDate.Text + "','" + txt_HardDiskId.Text + "','" + txt_PhoneNo.Text +  "');", con);
        OleDbCommand cmd1 = new OleDbCommand("select * from UserInfo where (HardDiskId='" + txt_HardDiskId.Text + "')", con);
        int temp = 0;
        try
        {
            con.Open();
            string count = (string)cmd1.ExecuteScalar();
            if ((count == "") || (count == null))
            {
                temp = cmd.ExecuteNonQuery();
                if (temp > 0)
                {
                    MessageBox.Show("User ID of " + txt_FirstName.Text + " " + txt_LastName.Text + " has been added");
                }
                else
                {
                    MessageBox.Show("Record not added");
                }
            }
            else
            {
                MessageBox.Show("User ID of " + txt_FirstName.Text + " already exists. Try another user ID.");
            }
        }

Solution

  • Aside from being open to SQL Injection (which you should parameterize your OleDbCommand), first thought on a problem is what you are trying to store the data. Do any of the text fields have special characters or apostrophe in name which would otherwise pre-terminate your embedded .... '" + nextField + "' ..." entries and throw the balance off.

    Another... don't know if the parser is picky or not... but a space after values, before open paren.... " values(" to " values (".

    Third, and more probable the issue is the expiration date. If its a date field, and you are trying to put in as text, it might be failing on the data type conversion.