0

i have a class called querys , and inside it i have function like code below

public void insertdata(int x1, string x2, string x3, int x4, string x5)
   { 
        try
        {
            string insert = string.Format("insert into [customer] ([cust_id],[cust_arabic_name],[cust_english_name],[cust_mobile],[cust_address]) values ('{0}','{1}','{2}','{3}','{4}')",x1, x2, x3, x4, x5);
            OleDbCommand cmd = new OleDbCommand(insert,mf.con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("new data added");
        }
        catch (System.InvalidOperationException ex) 
        {
           MessageBox.Show(ex.Message);
        } 
    }

and on my form have the following code inside the button

            query.insertdata(int.Parse( Id_TBox.Text + ""), A_Name_TBox.Text + "", E_Name_TBox.Text + "", int.Parse(Mobile_TBox.Text + ""), Address_TBox.Text + "");

i changed the Id_TBox max length to be (9) and the Mobile_TBox max length to be (10).

now when i am trying to insert data an error shows (value was either too large or too small for int32).

i change it to decimal but the error is come again.

Any help please

4
  • Just a Question: int.Parse( Id_TBox.Text + ""), Why +""? Commented Nov 18, 2014 at 14:52
  • to convert the Id_TBox inupt to integer and the "" , is if the data entry bush space button by mistake it'll be ignored Commented Nov 18, 2014 at 14:55
  • Is [cust_mobile] a phone number? If so, can it ever be greater than 2147483647? That would explain why it won't fit into Int32. You should be storing phone numbers in the database as string, not int. Commented Nov 18, 2014 at 16:02
  • when i change the column in the database from number to string it works, but how can i obligate the user to enter just numbers, because if he entered a letter in the cust_mobile by mistake it wont make sense Commented Nov 18, 2014 at 18:07

1 Answer 1

1

Try to change the Mobile Phone number data type to string in your function and to varchar(10) in your database. It is not good idea to set it as numerical variable. Then try to remove "" from your Id column, while data entry. After doing these steps, it may work fine

Sign up to request clarification or add additional context in comments.

2 Comments

when i change the column in the database from number to string it works, but how can i obligate the user to enter just numbers, because if he entered a letter in the cust_mobile by mistake it wont make sense
if you are developing web application with asp.net you can use fieldvalidators or you can use javascript to insure the input type. other wise you can monitor the user input using TextBox, KeyPress event to ignore the non numerical inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.