我正在尝试创建一个简单的应用程序表单,用户可以输入'reservationid', 'bookid', 'EmployeeID' and 'reservedate'
之类的数据。它来自我的程序库系统。 'reservationid' is an auto increment primary key
,其余为BigInt50, NVarChar50 and DateTime10
。所以我遇到了这个错误:Input String was not in a correct format
。 It worked fine a while ago until I modified the 'reservationid' to auto increment
所以我哪里出错了?我附上了我的代码示例。
任何帮助将不胜感激!提前谢谢!
namespace LibraryManagementSystemC4.User
{
public partial class Reserving : System.Web.UI.Page
{
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["LibrarySystemConnectionString"].ConnectionString;
}
//string reservationid
private void ExecuteInsert(string bookid, string EmployeeID, string reservedate)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO BookReservation (reservationid, bookid, EmployeeID, reservedate) VALUES " + " (@reservationid, @bookid, @EmployeeID, @reservedate)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[3];
//param[0] = new SqlParameter("@reeservationid", SqlDbType.Int, 50);
param[0] = new SqlParameter("@bookid", SqlDbType.BigInt, 50);
param[1] = new SqlParameter("@EmployeeID", SqlDbType.NVarChar, 50);
param[2] = new SqlParameter("@reservedate", SqlDbType.DateTime, 10);
//param[0].Value = reservationid;
param[0].Value = bookid;
param[1].Value = EmployeeID;
param[2].Value = reservedate;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert error";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (reservationidTextBox != null)
{
//reservationidTextBox.Text
ExecuteInsert(bookidTextBox.Text, EmployeeIDTextBox.Text, reservationidTextBox.Text);
ClearControls(Page);
}
else
{
Response.Write("Please input ISBN");
bookidTextBox.Focus();
}
{
//get bookid from Book Details and Employee PIN from current logged-in user
bookidTextBox.Text = DetailsView1.SelectedValue.ToString();
EmployeeIDTextBox.Text = HttpContext.Current.User.Identity.ToString();
}
}
public static void ClearControls(Control Parent)
{
if (Parent is TextBox)
{
(Parent as TextBox).Text = string.Empty;
}
else
{
foreach (Control c in Parent.Controls)
ClearControls(c);
}
}
}
}
答案 0 :(得分:4)
如果reservationid
自动递增,则将其从插入查询中删除
string sql = "INSERT INTO BookReservation ( bookid, EmployeeID, reservedate) VALUES (@bookid, @EmployeeID, @reservedate)";
也试试
param[0].Value = Convert.ToInt64(bookid);
param[1].Value = EmployeeID;
param[2].Value = Convert.ToDate(reservedate);
答案 1 :(得分:2)
在你对autoincrement进行预约后,你就不必这样做了
string sql = "INSERT INTO BookReservation (reservationid, bookid, EmployeeID, reservedate) VALUES " + " (@reservationid, @bookid, @EmployeeID, @reservedate)";
删除要插入的reservedid。 喜欢
string sql = "INSERT INTO BookReservation ( bookid, EmployeeID, reservedate) VALUES (@bookid, @EmployeeID, @reservedate)";
答案 2 :(得分:1)
这是因为当reservationid
不是command parameters
时,您没有将Auto Increment
整数值传递给string reservationid
。
我可以从您的代码中看到,您已声明{{1}},但您没有为其分配任何值,其次它应该是整数值。
答案 3 :(得分:0)
我知道这是非常严重的,但是由于Loupi对9Jun11的评论似乎他仍然有问题,我发布了实际答案。 Bala的回答是仍然给他输入类型不正确的格式错误;在值赋值中使用Convert.ToInt64
语句将其绊倒。在分配参数值之前进行变量转换,它就是一个魅力。最可能的罪魁祸首是bookid是某种非零的空字符串表示(空白引号,空格,null,等等)。
编辑:快速简便的单行测试相对防范:
long numAccountNum = Int64.TryParse(AccountNum, out numAccountNum) ? Convert.ToInt64(AccountNum) : 0;