2

当我尝试更新 a 中的记录时GridView,以下代码不会更新记录,但也不会引发任何错误。

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string index = GridView1.Rows[e.RowIndex].Cells[1].Text; //DwgRegID

        SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True");

        using (SqlCommand cmd = new SqlCommand())
        {
            string sql = "UPDATE dbo.Dwg_Register SET Ref = @Ref, Dwg_Ref = @Dwg_Ref,Title = @Title, Dwg_Received_Date = @Dwg_Received_Date, Rev = @Rev, Trade = @Trade, type1=@type1,Produced_Date=@Produced_Date,Produced_By=@Produced_By,Submittal_Ref=@Submittal_Ref,Issued_To=@Issued_To,Date_Issued = Date_Issued, Purpose = @Purpose, status1=@status1,Action_Date=@Action_Date where DwgRegID=N'" + index + "'";

            cmd.Connection = con;
            con.Open();
            cmd.CommandText = sql;

            cmd.Parameters.Add(new SqlParameter("@Ref", txtRef.Text));
            cmd.Parameters.Add(new SqlParameter("@Dwg_Ref", txtDwgRef.Text));
            cmd.Parameters.Add(new SqlParameter("@Title", txtTitle.Text));
            cmd.Parameters.Add(new SqlParameter("@Dwg_Received_Date", txtDwgReceivedDate.Text == "" ? DBNull.Value : (object)txtDwgReceivedDate.Text));
            cmd.Parameters.Add(new SqlParameter("@Rev", txtRev.Text));
            cmd.Parameters.Add(new SqlParameter("@Trade", ddlTrade.Text));
            cmd.Parameters.Add(new SqlParameter("@type1", ddlType.Text));
            cmd.Parameters.Add(new SqlParameter("@Produced_Date", txtProducedDate.Text == "" ? DBNull.Value : (object)txtProducedDate.Text));
            cmd.Parameters.Add(new SqlParameter("@Produced_By", ddlProducedBy.Text));
            cmd.Parameters.Add(new SqlParameter("@Submittal_Ref", txtSubmittalRef.Text));
            cmd.Parameters.Add(new SqlParameter("@Issued_To", ddlIssuedTo.Text));
            cmd.Parameters.Add(new SqlParameter("@Date_Issued", txtDateIssued.Text == "" ? DBNull.Value : (object)txtDateIssued.Text));
            cmd.Parameters.Add(new SqlParameter("@Purpose", ddlPurpose.Text));
            cmd.Parameters.Add(new SqlParameter("@status1", ddlStatus.Text));
            cmd.Parameters.Add(new SqlParameter("@Action_Date", txtActionDate.Text == "" ? DBNull.Value : (object)txtActionDate.Text));
            //cmd.Parameters.Add(new SqlParameter("@DwgRegID", index));


            cmd.ExecuteNonQuery();
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            GridView1.DataBind();
        }

            SqlConnection con1 = new SqlConnection("Data Source= MEHDI-PC\\SQLExpress; Initial Catalog=PIMS; Integrated Security=True");
            string sql1 = "SELECT DwgRegID,Ref,Dwg_Ref,Title,Dwg_Received_Date,Rev,Trade,type1,Produced_Date,Produced_By,Submittal_Ref,Issued_To,Date_Issued,Purpose,status1,Action_Date from dbo.Dwg_Register";
            con1.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql1,con1);
            DataSet ds1 = new DataSet();
            da.Fill(ds1);
            GridView1.DataSource = ds1; 
            GridView1.DataBind();
            if (con1.State == ConnectionState.Open)
            {
                con1.Close();
            }


            GridView1.EditIndex = -1;

            GridView1.DataBind();

            MultiView1.SetActiveView(ViewGrid);

            lblUpdate.Text = "Record updated sucessfully.";


    }

当我在 SSMS 中运行以下 SQL 查询时:

UPDATE dbo.Dwg_Register
SET Ref=4
from dbo.Dwg_Register
where DwgRegID=23

此命令确实将值 4 存储到 dbo.Dwg_Register 中。但是此命令不会将值从 GridView 存储到 sql 表中。

我的 EditRow 函数代码如下:

            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        MultiView1.SetActiveView(ViewGrid);

        GridView1.EditIndex = e.NewEditIndex;

        GridView1.DataBind(); 

}

RowEditing功能更改RowStateGridView编辑模式。但是当我在中编辑单元格值Gridview并按更新按钮将新值存储到 Sql 表时,更新函数不会这样做。此代码既不会引发任何错误。请帮助我确定我可能出错的地方。谢谢。

4

1 回答 1

2

在插入数据之前进行错误检查。

该错误告诉您txtRef.Text没有值,但需要一个值。

完成对值的错误检查后Ref,您还应该继续检查所有其他值。

您不想在数据库中造成混乱。

更新:

确保您GridView只更新正确的行类型。

我不太喜欢使用该AddWithValue工具,因为它似乎会阻止人们理解他们的数据库。你的Add(new SqlParameter(技术基本上就是这样做的。

例如,这两行产生相同的结果:

cmd.Parameters.AddWithValue("@Ref", txtRef.Text);

cmd.Parameters.Add(new SqlParameter("@Ref", txtRef.Text));

@Ref的数据库里有什么?Integer? varchar(50)? 显然,数据库如何处理这两者会有很大的不同。

如果它是一个integer,它真的应该写成:

cmd.Parameters.Add("@Ref", SqlDbType.Int).Value = Convert.ToInt32(txtRef.Text.Trim());

如果它是 a varchar(50),它真的应该写成:

cmd.Parameters.Add("@Ref", SqlDbType.VarChar, 50).Value = txtRef.Text.Trim();

由于我对您的数据库一无所知,所以我一直在使用AddWithValue,但我强烈建议您查看数据库表的设计,然后将您的数据库表与SqlParameters它们的设计方式相匹配。

我还建议使用constSQL 连接字符串,因为您的数据库在您的程序使用它时不会更改:

private static string m_sqlConn = "Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True";

我还创建了一个static DbSafe方法来在数据插入之前“发送消息”。专用于此的方法使您可以轻松地引导所有数据以确保其安全。此外,稍后修改该方法以添加更多功能很容易,并立即转换为您使用它的所有代码。

private static object DbSafe(object value) {
  if ((value != null) || (value != DBNull.Value)) {
    string strVal = value.ToString();
    if (!String.IsNullOrEmpty(strVal)) {
      return strVal.Trim();
    }
  }
  return DBNull.Value;
}

您也没有理由制作两 (2) 个单独的 SQL 对象 (SqlCommandSqlDataAdapter) 来执行您的任务。

这是我编写修改后的版本的方式:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
  if (GridView1.Rows[e.RowIndex].RowType == DataControlRowType.DataRow) {
    string index = GridView1.Rows[e.RowIndex].Cells[1].Text; //DwgRegID
    string sqlUpdate = "UPDATE dbo.Dwg_Register SET " +
      "Ref=@Ref, Dwg_Ref=@Dwg_Ref, Title=@Title, Dwg_Received_Date=@Dwg_Received_Date, " +
      "Rev=@Rev, Trade=@Trade, type1=@type1, Produced_Date=@Produced_Date, " +
      "Produced_By=@Produced_By, Submittal_Ref=@Submittal_Ref, Issued_To=@Issued_To, " +
      "Date_Issued=Date_Issued, Purpose=@Purpose, status1=@status1, Action_Date=@Action_Date " +
      "WHERE DwgRegID=N'" + index + "'";
    string sqlSelect = "SELECT DwgRegID, Ref, Dwg_Ref, Title, Dwg_Received_Date, Rev, Trade, type1, Produced_Date, Produced_By, Submittal_Ref, " +
      "Issued_To, Date_Issued, Purpose, status1, Action_Date " +
      "from dbo.Dwg_Register";
    var table = new DataTable();
    using (var cmd = new SqlCommand(sqlUpdate, new SqlConnection(m_sqlConn))) {
      cmd.Parameters.AddWithValue("@Ref", DbSafe(txtRef.Text));
      cmd.Parameters.AddWithValue("@Dwg_Ref", DbSafe(txtDwgRef.Text));
      cmd.Parameters.AddWithValue("@Title", DbSafe(txtTitle.Text));
      cmd.Parameters.AddWithValue("@Dwg_Received_Date", DbSafe(txtDwgReceivedDate.Text));
      cmd.Parameters.AddWithValue("@Rev", DbSafe(txtRev.Text));
      cmd.Parameters.AddWithValue("@Trade", DbSafe(ddlTrade.Text));
      cmd.Parameters.AddWithValue("@type1", DbSafe(ddlType.Text));
      cmd.Parameters.AddWithValue("@Produced_Date", DbSafe(txtProducedDate.Text));
      cmd.Parameters.AddWithValue("@Produced_By", DbSafe(ddlProducedBy.Text));
      cmd.Parameters.AddWithValue("@Submittal_Ref", DbSafe(txtSubmittalRef.Text));
      cmd.Parameters.AddWithValue("@Issued_To", DbSafe(ddlIssuedTo.Text));
      cmd.Parameters.AddWithValue("@Date_Issued", DbSafe(txtDateIssued.Text));
      cmd.Parameters.AddWithValue("@Purpose", DbSafe(ddlPurpose.Text));
      cmd.Parameters.AddWithValue("@status1", DbSafe(ddlStatus.Text));
      cmd.Parameters.AddWithValue("@Action_Date", DbSafe(txtActionDate.Text));
      //cmd.Parameters.Add(new SqlParameter("@DwgRegID", index));
      try {
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.CommandText = sqlSelect;
        cmd.Parameters.Clear();
        table.Load(cmd.ExecuteReader());
      } catch (SqlException err) {
        Response.Write(err.Message);
      } finally {
        cmd.Connection.Close();
      }
    }
    GridView1.DataSource = table;
    GridView1.DataBind();
    MultiView1.SetActiveView(ViewGrid);
    lblUpdate.Text = "Record updated sucessfully.";
  }

}

此外,您会注意到上面我添加了一个try/catch/finally仅捕获SqlException错误的块,然后它立即关闭数据库连接。

我不确定你GridViewEditEventArgs在做什么和/或它是否会伤害任何东西,所以我不理会它。

我希望这会有所帮助。解释的时间太长了。

于 2013-07-11T18:42:55.023 回答