将数字记录插入 MS 访问从 C# asp.net

本文关键字:asp net 访问 MS 数字 记录 插入 | 更新日期: 2023-09-27 18:31:16

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();

这给了我错误Input string was not in a correct format.请帮帮我。

将数字记录插入 MS 访问从 C# asp.net

根据代码和错误,很可能 TextBox 控件的文本之一(txtContact 或 txtAge)不是有效的整数(整数)值。 确保正在验证输入。 应使用 RequiredFieldValidator 以及 RangeValidator 或 RegularExpressionValidator 来确保文本框不为空且包含有效文本。

您也可以考虑使用 System.Int32.TryParse() 而不是 Convert.ToInt32。

您正在将非整数值转换为整数值,请检查txtAge.TexttxtContact.Text及其值,它们应包含转换为整数值的值。