0
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = conn;
command.CommandText = "INSERT INTO myTbl ([username],[password],[firstname],[lastname],[mi],[age],[place],[contact])VALUES (?,?,?,?,?,?,?,?)";
command.Parameters.Add("@user", OleDbType.VarChar).Value = txtUsername.Text;
command.Parameters.Add("@psw", OleDbType.VarChar).Value = txtPassword.Text;
command.Parameters.Add("@nm", OleDbType.VarChar).Value = txtName.Text;
command.Parameters.Add("@Lname", OleDbType.VarChar).Value = txtLastName.Text;
command.Parameters.Add("@mIni", OleDbType.VarChar).Value = txtMI.Text;
command.Parameters.Add("@ag", OleDbType.Integer).Value = Convert.ToInt32(txtAge.Text);
command.Parameters.Add("@plc", OleDbType.VarChar).Value = txtPlace.Text;
command.Parameters.Add("@cntct", OleDbType.Integer).Value = Convert.ToInt32(txtContact.Text); ;
command.ExecuteNonQuery();
lblInfo.Visible = true;
lblInfo.Text = "Success";
conn.Close();

This gives me the error Input string was not in a correct format. Please help me out.

6
  • this code gives me error, input string was not in correct format. So please help me out. Thanks in advance Commented Apr 17, 2012 at 5:03
  • Running it through the debugger, which line gives the error? Commented Apr 17, 2012 at 5:06
  • Also, take a look at Int32.TryParse (msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx) as an alternative to Convert.ToInt32. Commented Apr 17, 2012 at 5:10
  • Can you share those values which are you passed to perform your task and you found error of "Input string was not in a correct format" Commented Apr 17, 2012 at 5:43
  • Debug and check at what line u r getting the error Commented Apr 17, 2012 at 6:28

2 Answers 2

1

Based on the code, and the error, most likely one of the TextBox control's text (txtContact or txtAge) isn't a valid Integer (whole number) value. Ensure that you're validating the input. You should use a RequiredFieldValidator, and either a RangeValidator or RegularExpressionValidator to ensure that the Textbox is not empty and contains valid text.

You might also consider using System.Int32.TryParse() instead of Convert.ToInt32.

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

Comments

1

You are converting non integer value to integer value, please check txtAge.Text and txtContact.Text and their values, they should contain value which convert into integer value.

Comments

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.