Reputation: 21
private void button1_Click(object sender, EventArgs e) {
try {
Connections.con.Open();
string str = "INSERT INTO `Employee` (`FirstName`, `LastName`, `Age`, `ContactNumber`, `Username`, `Password`) VALUES (?, ?, ?, ?, ?, ?)";
OleDbCommand cmd = new OleDbCommand(str, Connections.con);
cmd.Parameters.AddWithValue("@?", txtFirst.Text.ToString());
cmd.Parameters.AddWithValue("@?", txtLast.Text.ToString());
cmd.Parameters.AddWithValue("@?", txtAge.Text.ToString());
cmd.Parameters.AddWithValue("@?", txtCon.Text.ToString());
cmd.Parameters.AddWithValue("@?", txtUser.Text.ToString());
cmd.Parameters.AddWithValue("@?", txtPass.Text.ToString());
cmd.ExecuteNonQuery();
MessageBox.Show("Registration Successful");
}
catch (Exception ex) {
MessageBox.Show("Error" + ex);
}
finally {
Connections.con.Close();
this.Dispose();
this.Close();
f1.Show();
}
}
This code should save the first name last name age contact number username and password the user will enter but the problem is the OVERFLOW exception error pops up whenever i click the button.. please help I've been searching everywhere just to solve this problem but i can't find an answer
Upvotes: 0
Views: 1280
Reputation: 216283
The AddWithValue method creates a parameter with a DataType equal to the value passed.
You pass strings so the parameter is created with a DataType string.
This will not work well when you try to insert that value in a numeric field.
Change your code to
cmd.Parameters.AddWithValue("@?", txtFirst.Text);
cmd.Parameters.AddWithValue("@?", txtLast.Text);
cmd.Parameters.AddWithValue("@?", Convert.ToInt32(txtAge.Text));
cmd.Parameters.AddWithValue("@?", Convert.ToInt32(txtCon.Text));
cmd.Parameters.AddWithValue("@?", txtUser.Text);
cmd.Parameters.AddWithValue("@?", txtPass.Text);
As a second guess, the contact number field is of type numeric long.
Access allow a number of this kind to be at maximum 2147483647.
In this context it is perfectly possible that you have a telephone number that when is interpreted as a numeric value is bigger that this limit. As a consequence you get the overflow error.
I really suggest to use a string for a contact number with enough chars to accomodate for more than one telephone number (30 chars)
As a side note. I would also store the birth date and not the age otherwise next year you have a problem.
Upvotes: 1