问题遇到的现象和发生背景
我在asp.net打算用Insert語句輸入資料進MySql時提示出錯
问题相关代码,请勿粘贴截图
bool IsUpdated = false;
//getting key value, row id
string UserId = GridView1.DataKeys[e.RowIndex].Value.ToString();
//getting row field details
TextBox Pwd = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tb_pw");
TextBox UserIdentity = (TextBox)GridView1.FooterRow.FindControl("tb_newidentity");
TextBox Administrator = (TextBox)GridView1.FooterRow.FindControl("tb_newa");
TextBox LastUpdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tb_LastUpdate");
// overwrite LastUpdate with current date and time
DateTime dt = new DateTime();
dt = DateTime.Now;
LastUpdate.Text = dt.ToLongDateString() + " " + dt.ToLongTimeString();
if (UserId != "")
{
using (MySqlConnection sqlCon = new MySqlConnection(strConnString))
{
using (MySqlCommand cmd = new MySqlCommand())
{
//here i'd added "@" for continuous string in new line
cmd.CommandText = @"UPDATE Table_account SET [USERID] = @UserId, [USERPW] = @Pwd, [SF] = @UserIdentity, [A] = @Administrator, [LASTUPDATE] = @LastUpdate WHERE [USERID] = @UserId";
cmd.Parameters.AddWithValue("@UserId", UserId);
cmd.Parameters.AddWithValue("@Pwd", Pwd.Text);
cmd.Parameters.AddWithValue("@UserIdentity", UserIdentity.Text);
cmd.Parameters.AddWithValue("@Administrator", Administrator.Text);
cmd.Parameters.AddWithValue("@LastUpdate", LastUpdate.Text);
cmd.Connection = sqlCon;
sqlCon.Open();
IsUpdated = cmd.ExecuteNonQuery() > 0;
sqlCon.Close();
}
}
}
if (IsUpdated)
{
lb_msg.Text = "'" + UserId + "' updated successfully!";
lb_msg.ForeColor = System.Drawing.Color.Green;
}
else
{
lb_msg.Text = "Error while updating '" + UserId + "' details";
lb_msg.ForeColor = System.Drawing.Color.Red;
}
GridView1.EditIndex = -1;
ReadDBAndBindGrid();
這是我的MySql
运行结果及报错内容
我的解答思路和尝试过的方法
我在其他頁面也有用Insert語句,但是沒出錯,唯獨2個有要輸入DATETIME類型的Insert就出錯了,也試過上網查輸入datetime轉格式進MySql,但是也不行,感覺很謎
我想要达到的结果
希望成功輸入資料進MySql~