?meo? 2021-12-05 19:46 采纳率: 0%
浏览 7

當我在asp.net輸入資料進MySql時提示錯誤

问题遇到的现象和发生背景

我在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

img

运行结果及报错内容

img

我的解答思路和尝试过的方法

我在其他頁面也有用Insert語句,但是沒出錯,唯獨2個有要輸入DATETIME類型的Insert就出錯了,也試過上網查輸入datetime轉格式進MySql,但是也不行,感覺很謎

我想要达到的结果

希望成功輸入資料進MySql~

  • 写回答

2条回答 默认 最新

  • oldmao_2000 人工智能领域新星创作者 2021-12-05 21:03
    关注

    先把最后那个字段去掉看是否能够成功update,然后手工设置一下值试试能否更新,看你好像是字符型的

    评论

报告相同问题?

问题事件

  • 创建了问题 12月5日

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿