2012-04-17 33 views
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(); 

这给了我错误Input string was not in a correct format。请帮助我。插入数字记录到ms访问从c#asp.net

+0

此代码给我错误,输入字符串格式不正确。所以请帮助我。在此先感谢 – user1337815 2012-04-17 05:03:05

+0

运行它通过调试器,哪一行给出错误? – 2012-04-17 05:06:41

+0

另外,请查看'Int32.TryParse'(http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx)作为'Convert.ToInt32'的替代方法。 – 2012-04-17 05:10:59

回答

1

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

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

1

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