user1337815
user1337815

Reputation: 1

insert numeric records into ms access from c# asp.net

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.

Upvotes: 0

Views: 1458

Answers (2)

Saurabh
Saurabh

Reputation: 5727

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.

Upvotes: 1

David
David

Reputation: 73604

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.

Upvotes: 1

Related Questions