Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 行更新不工作显示输入字符串格式不正确_C#_Gridview - Fatal编程技术网

C# 行更新不工作显示输入字符串格式不正确

C# 行更新不工作显示输入字符串格式不正确,c#,gridview,C#,Gridview,受保护的无效gv_卡行更新(对象发送方,GridViewUpdateEventArgs e) { int结果=0; 信用卡产品=新信用卡(); GridViewRow行=gv_card.Rows[e.RowIndex]; string id=gv_card.DataKeys[e.RowIndex].Value.ToString(); 字符串tid=((文本框)行。单元格[0]。控件[0])。文本; 字符串tnumber=((TextBox)行。单元格[1]。控件[0])。文本; 字符串texpi

受保护的无效gv_卡行更新(对象发送方,GridViewUpdateEventArgs e) {

int结果=0;
信用卡产品=新信用卡();
GridViewRow行=gv_card.Rows[e.RowIndex];
string id=gv_card.DataKeys[e.RowIndex].Value.ToString();
字符串tid=((文本框)行。单元格[0]。控件[0])。文本;
字符串tnumber=((TextBox)行。单元格[1]。控件[0])。文本;
字符串texpirydate=((TextBox)行。单元格[2]。控件[0])。文本;
字符串tcvv=((文本框)行。单元格[3]。控件[0])。文本;
字符串tcardtype=((TextBox)行。单元格[4]。控件[0])。文本;
字符串tholdername=((文本框)行。单元格[5]。控件[0])。文本;
结果=prod.CardUpdate(int.Parse(tid)、tholdername、tnumber、texpirydate、int.Parse(tcvv)、tcardtype);
如果(结果>0)
{
响应。写入(“警报(‘产品更新成功’);”;
}
其他的
{
响应。写入(“警报(‘产品未更新’);”;
}
gv_card.EditIndex=-1;
bind();
}
}


上面是我的代码,但它似乎无法更新我的gridview,该消息可能来自您对int.Parse(string)的调用,该调用要求字符串为有效整数。要处理这个问题,您可以改为使用
int.TryParse(string,out int)
,如果能够解析字符串,它将返回true或false。如果成功,则
out
参数将包含解析后的整数值

因此,首先尝试解析整数字段。如果失败,您可以返回一条错误消息,如果成功,那么您可以在调用
CardUpdate
时直接使用整数:

int tidValue;
int tcvvValue;

if (!int.TryParse(tid, out tidValue))
{
    Response.Write("<script>alert('The value specified for TID is not an integer.');</script>");
}
else if (!int.TryParse(tcvv, out tcvvValue))
{
    Response.Write("<script>alert('The value specified for TCVV is not an integer.');</script>");
}
else
{
    result = prod.CardUpdate(tidValue, tholdername, tnumber, texpirydate, tcvvValue, tcardtype);

    if (result > 0)
    {
        Response.Write("<script>alert('Product updated successfully');</script>");
    }
    else
    {
        Response.Write("<script>alert('Product NOT updated');</script>");
    }
}
int值;
int-tcvv值;
如果(!int.TryParse(tid,out tidValue))
{
Write(“警报(‘为TID指定的值不是整数’);”;
}
如果(!int.TryParse(tcvv,out tcvvValue))
{
Write(“警报(‘为TCVV指定的值不是整数’);”;
}
其他的
{
结果=prod.CardUpdate(tidValue、tholdername、tnumber、texpirydate、tcvvValue、tcardtype);
如果(结果>0)
{
响应。写入(“警报(‘产品更新成功’);”;
}
其他的
{
响应。写入(“警报(‘产品未更新’);”;
}
}

我将猜测
tid
tcvv
的值,由于空值或非数值,无法解析为
int
。如果您仔细阅读
Parse
的文档,调试起来应该非常简单。
int tidValue;
int tcvvValue;

if (!int.TryParse(tid, out tidValue))
{
    Response.Write("<script>alert('The value specified for TID is not an integer.');</script>");
}
else if (!int.TryParse(tcvv, out tcvvValue))
{
    Response.Write("<script>alert('The value specified for TCVV is not an integer.');</script>");
}
else
{
    result = prod.CardUpdate(tidValue, tholdername, tnumber, texpirydate, tcvvValue, tcardtype);

    if (result > 0)
    {
        Response.Write("<script>alert('Product updated successfully');</script>");
    }
    else
    {
        Response.Write("<script>alert('Product NOT updated');</script>");
    }
}