[英]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
. 这给了我错误Input string was not in a correct format
。 Please help me out. 请帮帮我。
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. 根据代码和错误,很可能TextBox控件的文本(txtContact或txtAge)之一不是有效的Integer(整数)值。 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. 您应该使用RequiredFieldValidator,以及RangeValidator或RegularExpressionValidator来确保Textbox不为空且包含有效文本。
You might also consider using System.Int32.TryParse() instead of Convert.ToInt32. 您可能还会考虑使用System.Int32.TryParse()而不是Convert.ToInt32。
您正在将非整数值转换为整数值,请检查txtAge.Text
和txtContact.Text
及其值,它们应包含转换为整数值的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.