簡體   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