繁体   English   中英

将数字记录插入到c#asp.net的ms访问中

[英]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();

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

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

您可能还会考虑使用System.Int32.TryParse()而不是Convert.ToInt32。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM