My database column is of type bigint and has values that are phone numbers. It shows error on the button click event. The error is in the line:
_bal.phone = Convert.ToInt32(txtphone.Text);
Why is this happening?
protected void btnsave_Click(object sender, EventArgs e)
{
_bal.name = txtname.Text;
_bal.age = Convert.ToInt32(txtage.Text);
_bal.email = txtemail.Text;
_bal.password = txtpassword.Text;
_bal.phone = Convert.ToInt32(txtphone.Text);
if (btnsave.Text == "SAVE")
{
_bal.emp_insert();
}
else if (btnsave.Text == "UPDATE")
{
_bal.Lid = int.Parse(ViewState["Loginid"].ToString());
_bal.emp_update();
}
fill_grd();
}
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
_bal.Lid = int.Parse(e.CommandArgument.ToString());
if (e.CommandName == "editing")
{
DataSet ds = new DataSet();
ds = _bal.emp_edit();
if (ds.Tables[0].Rows.Count > 0)
{
txtname.Text = ds.Tables[0].Rows[0]["name"].ToString();
txtage.Text = ds.Tables[0].Rows[0]["age"].ToString();
txtemail.Text = ds.Tables[0].Rows[0]["email"].ToString();
txtpassword.Text = ds.Tables[0].Rows[0]["password"].ToString();
txtphone.Text=ds.Tables[0].Rows[0]["phone"].ToString();
}
btnsave.Text = "UPDATE";
ViewState["Loginid"] = e.CommandArgument.ToString();
}
else if (e.CommandName == "deleting")
{
_bal.emp_delete();
fill_grd();
}
}
Int32 can accept values between –2147483648 and 2147483647. The phone number you are inputting could be crossing this range and that's why you are getting this error.
Suggestion: As you mentioned that you are using nchar in your database, please use string data type to represent phone number field. OR if you are stick to use integer then use Int64 datatype. :)