当我尝试更新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表时,Update函数不会执行此操作。此代码既不会引发任何错误。请帮我确定可能出错的地方。
感谢。
答案 0 :(得分: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());
如果它是varchar(50)
,那么它应该写成:
cmd.Parameters.Add("@Ref", SqlDbType.VarChar, 50).Value = txtRef.Text.Trim();
由于我对您的数据库一无所知,所以我使用了AddWithValue
,但我强烈建议您查看数据库表的设计,然后将SqlParameters
与它们的匹配方式相匹配设计的。
我还建议使用const
SQL连接字符串,因为在您的程序使用它时数据库不会改变:
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对象(SqlCommand
和SqlDataAdapter
)来执行您的任务。
以下是我编写修改版本的方法:
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
正在做什么和/或它是否会伤害任何东西,所以我不管它。
我希望有所帮助。解释时间太长了。