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