表示成:
protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string chexingid = grid.DataKeys[e.RowIndex].Value.ToString();
string sucheng = ((TextBox)grid.Rows[e.RowIndex].FindControl("suchengTextBox")).Text;
string description = ((TextBox)grid.Rows[e.RowIndex].FindControl("descriptionTextBox")).Text;
string imagefilename = ((TextBox)grid.Rows[e.RowIndex].FindControl("tupianTextBox")).Text;
bool success = CatalogAccess.UpdateCheXing(chexingid, sucheng, description, imagefilename);
grid.EditIndex=-1;
statusLabel.Text=success?"更新成功":"更新失败";
BindGrid();
}
------------------------------------------
业务层(CatalogAccess):
public static bool UpdateCheXing(string chexingid, string sucheng, string description, string imagefilename)
{
SqlCommand comm = GenericDataAccess.CreateCommand();
comm.CommandText = "UpdateCheXing";
SqlParameter param = comm.CreateParameter();
param.ParameterName = "@CheXingID";
param.Value = "chexingid";
param.SqlDbType = SqlDbType.Int;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@SuCheng";
param.Value = "sucheng";
param.SqlDbType = SqlDbType.VarChar;
param.Size = 50;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@Description";
param.Value="description";
param.SqlDbType = SqlDbType.VarChar;
param.Size = 2000;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@ImageFileName";
param.Value = "imagefilename";
param.SqlDbType = SqlDbType.VarChar;
param.Size = 50;
comm.Parameters.Add(param);
int result = -1;
try
{
result = GenericDataAccess.ExecuteNonQuery(comm);
}
catch(Exception ex)
{
Utilities.LogError(ex);
throw ex;
}
return (result != -1);
-----------------------------------------------
存储过程:
ALTER PROCEDURE UpdateCheXing(
@CheXingID int,
@SuCheng varchar(50),
@Description varchar(2000),
@ImageFileName varchar(50))
AS
Update CheXingList
set Description=@Description,SuCheng=@SuCheng,ImageFileName=@ImageFileName
where CheXingID=@CheXingID
--------------------------------------------------
就是点击编辑,更新后 不成功
protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string chexingid = grid.DataKeys[e.RowIndex].Value.ToString();
string sucheng = ((TextBox)grid.Rows[e.RowIndex].FindControl("suchengTextBox")).Text;
string description = ((TextBox)grid.Rows[e.RowIndex].FindControl("descriptionTextBox")).Text;
string imagefilename = ((TextBox)grid.Rows[e.RowIndex].FindControl("tupianTextBox")).Text;
bool success = CatalogAccess.UpdateCheXing(chexingid, sucheng, description, imagefilename);
grid.EditIndex=-1;
statusLabel.Text=success?"更新成功":"更新失败";
BindGrid();
}
------------------------------------------
业务层(CatalogAccess):
public static bool UpdateCheXing(string chexingid, string sucheng, string description, string imagefilename)
{
SqlCommand comm = GenericDataAccess.CreateCommand();
comm.CommandText = "UpdateCheXing";
SqlParameter param = comm.CreateParameter();
param.ParameterName = "@CheXingID";
param.Value = "chexingid";
param.SqlDbType = SqlDbType.Int;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@SuCheng";
param.Value = "sucheng";
param.SqlDbType = SqlDbType.VarChar;
param.Size = 50;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@Description";
param.Value="description";
param.SqlDbType = SqlDbType.VarChar;
param.Size = 2000;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@ImageFileName";
param.Value = "imagefilename";
param.SqlDbType = SqlDbType.VarChar;
param.Size = 50;
comm.Parameters.Add(param);
int result = -1;
try
{
result = GenericDataAccess.ExecuteNonQuery(comm);
}
catch(Exception ex)
{
Utilities.LogError(ex);
throw ex;
}
return (result != -1);
-----------------------------------------------
存储过程:
ALTER PROCEDURE UpdateCheXing(
@CheXingID int,
@SuCheng varchar(50),
@Description varchar(2000),
@ImageFileName varchar(50))
AS
Update CheXingList
set Description=@Description,SuCheng=@SuCheng,ImageFileName=@ImageFileName
where CheXingID=@CheXingID
--------------------------------------------------
就是点击编辑,更新后 不成功
{
Utilities.LogError(ex);
throw ex;
}
此处报错 将参数值从 String 转换到 Int32 失败。
string chexingid = grid.DataKeys[e.RowIndex].Value.ToString(); //放个断点看这得到的是不是能转换成int类型的数
string sucheng = 返回1
string description = 返回1
string imagefilename = 返回 1
这些都没有问题啊
改成
int chexingid = int.Parse(grid.DataKeys[e.RowIndex].Value.ToString()); public static bool UpdateCheXing(string chexingid, string sucheng, string description, string imagefilename) 改成
public static bool UpdateCheXing(int chexingid, string sucheng, string description, string imagefilename)
param.Value = "chexingid"; 多了冒号
改成这样就好了:
param.Value = chexingid; 分能给自己好了
param.SqlDbType = SqlDbType.Int; 把参数转换成你需要的类型