我正在尝试使用一些TextBoxes和DropDowns来更新表中的数据。
在更新表之前,我在GridView
中显示预览,但在更新数据时,特定列的值会从传递的值(即主键)更改为表中的Name
。
Class.cs
public int Update()
{
try
{
int result = 0;
cmd.Parameters.AddWithValue("@Param1", 12354);
cmd.Parameters.AddWithValue("@Param2", 1473);
cmd.Parameters.AddWithValue("@Param3", "some_text");
cmd.Parameters.AddWithValue("@Param4", 28);//Id from DDL
cmd.Parameters.AddWithValue("@Param5", 25);//Id from DDL
using (StringWriter sw = new StringWriter())
{
_TransItem.TableName = "TransItems";
_TransItem.WriteXml(sw);
XML = sw.ToString();
}
cmd.Parameters.AddWithValue("@TransItems", XML);
con.Open();
result = cmd.ExecuteNonQuery();
return result;
}
catch (Exception ex)
{
cls_WriteErrorLog.Write_Error_To_File(ex.ToString());
return -1;
}
finally
{
cmd.Parameters.Clear();
con.Close();
}
}
我在GridView
中有一个按钮来更新记录。
Form.aspx.cs
protected void GdMaster_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.ToString() == "CmdEdit")
{
int i = Convert.ToInt32(e.CommandArgument) - 1;
//some code
ddl1.SelectedValue = ddlSaakh2.SelectedValue = ((Label)GdMaster.Rows[i].FindControl("GdDdlId1")).Text == "0" || ((Label)GdMaster.Rows[i].FindControl("GdDdlId1")).Text == "Select" ? "" : ((Label)GdMaster.Rows[i].FindControl("GdDdlId1")).Text;
//remaining code
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
dt_GridItems = new DataTable();
dt_GridItems.Columns.Add("Field1");
dt_GridItems.Columns.Add("Field2");
dt_GridItems.Columns.Add("Field3");
dt_GridItems.Columns.Add("Field4");
dt_GridItems.Columns.Add("Field5");
for (int i = 0; i < GdMaster.Rows.Count; i++)
{
dt_GridItems.Rows[i]["Field1"] = ((Label)GdMaster.Rows[i].FindControl("GdField1")).Text;
dt_GridItems.Rows[i]["Field2"] = ((Label)GdMaster.Rows[i].FindControl("GdField2")).Text;
dt_GridItems.Rows[i]["Field3"] = ((Label)GdMaster.Rows[i].FindControl("GdField3")).Text;
dt_GridItems.Rows[i]["Field4"] = ((Label)GdMaster.Rows[i].FindControl("GdDdlId1")).Text;
dt_GridItems.Rows[i]["Field5"] = ((Label)GdMaster.Rows[i].FindControl("GdDdlId2")).Text;
}
Class class = new Class();
int i = class.Update();
}
}
当它到达Update()
Parameters
时,如上面的代码所示。但是输入时间数据时,传递给@Param4
的值是使用StringWriter
的表中主键的名称值。
如何输入主键代替名称?