Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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# GridView行命令错误_C#_Asp.net_Gridview - Fatal编程技术网

C# GridView行命令错误

C# GridView行命令错误,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个GridView设置,实体框架有两个TemplateFields,每个字段调用一个弹出窗口,只显示数据库中的文本。这两个模板字段都设置为图像按钮 <asp:GridView ID="grvResults" runat="server" DataKeyNames="Soln_SolutionId" OnRowCommand="grvResults_RowCommand" CssClass="dgstyle" /> <Columns> <asp:Templat

我有一个
GridView
设置,实体框架有两个
TemplateFields
,每个字段调用一个弹出窗口,只显示数据库中的文本。这两个
模板字段
都设置为
图像按钮

<asp:GridView ID="grvResults" runat="server" DataKeyNames="Soln_SolutionId"  OnRowCommand="grvResults_RowCommand" CssClass="dgstyle" />
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
   <asp:ImageButton ID="imgPreview" runat="server" CommandName="viewDoc" CommandArgument='<%#Eval("Link")%>' ImageUrl="~/images/viewpage.gif" ImageAlign="Left" Width="25" Height="25" ToolTip="View attachment" AlternateText='<%#Eval("Link")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
   <asp:ImageButton ID="imgDetail" runat="server" CommandName="viewDetail" CommandArgument='<%#Eval("Soln_SolutionId")%>' ImageUrl="~/images/docview.gif" ImageAlign="Left" Width="25" Height="25" ToolTip="View details about the page" AlternateText="View details" /></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
如果我单击“viewDetails”图像,它将非常有效。 如果单击“viewDoc”图像,它将失败,出现以下异常:

输入字符串的格式不正确。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.FormatException:输入字符串不在 格式正确

源错误:

第151行:int DetailID= 转换.ToInt32(如CommandArgument.ToString())

现在有人知道为什么会发生这种情况,以及如何解决它吗

异常详细信息:System.FormatException:输入字符串的格式不正确


此错误表示该值为null或非numeric。附加调试器并检查您正在接收的值

您应该像下面那样更改代码

int DetailID;
int.TryParse(e.CommandArgument.ToString(), out DetailID);
有关更多信息,请参阅此链接:
这是我的Row命令的逻辑错误。实体模型不喜欢在Gridview行命令中使用共享变量的整个想法。我还在读这方面的书

但这是有效的代码

if (e.CommandName == "viewDetail")
            {
                int DetailID = Convert.ToInt32(e.CommandArgument.ToString());
                using (VIPCRMEntities dbc = new VIPCRMEntities())
                {
                    Solution selDetail = (from c in dbc.Solutions
                                          where c.Soln_SolutionId == DetailID
                                          select c).First();
                    InfoModal.Show();
                    lblInfoText.Text = selDetail.Soln_SolutionDetails;
                }
            }
            else if (e.CommandName == "viewDoc")
            {
                int DetailID = Convert.ToInt32(e.CommandArgument.ToString());
                using (VIPCRMEntities dbc = new VIPCRMEntities())
                {
                    Solution selDetail = (from c in dbc.Solutions
                                          where c.Soln_SolutionId == DetailID
                                          select c).First();
                    InfoModal.Show();
                    lblInfoText.Text = selDetail.soln_link;
                }               
            }

您应该
debug
您的代码并检查
e.CommandArgument
的值似乎
e.CommandArgument
的值是无法转换为
int
的字符串(即,它是普通字符串而不是数字)。我已经这样做了。e.CommandArgument为null,但它适用于另一个命令。我需要知道为什么?您在第一个
ImageButton
中使用了CommandArgument
Link
,在第二个
ImageButton
中使用了
Soln\u SolutionId
。。。。。似乎您应该在第一个ImageButton中使用
Soln\u SolutionId
,也可以作为commandArguments,我也尝试过,它仍然返回相同的异常。我从一开始就有同样的论点。没有效果。我这样做了,它返回一个空值。问题是,一个命令运行良好,而另一个命令运行不正常。我需要知道为什么它会这样做,因为它几乎是相同的代码,但具有不同的命令参数。源数据(绑定到网格)是否包含空值?数据库中有一些空值,但Soln_SolutionId是该表的主键,永远不会为空。它甚至不调用select命令。它在DetailIDBut的声明中给出了一个错误,这是一个好的实践,我将在开始生产之前把它放进去。谢谢你的帮忙。
if (e.CommandName == "viewDetail")
            {
                int DetailID = Convert.ToInt32(e.CommandArgument.ToString());
                using (VIPCRMEntities dbc = new VIPCRMEntities())
                {
                    Solution selDetail = (from c in dbc.Solutions
                                          where c.Soln_SolutionId == DetailID
                                          select c).First();
                    InfoModal.Show();
                    lblInfoText.Text = selDetail.Soln_SolutionDetails;
                }
            }
            else if (e.CommandName == "viewDoc")
            {
                int DetailID = Convert.ToInt32(e.CommandArgument.ToString());
                using (VIPCRMEntities dbc = new VIPCRMEntities())
                {
                    Solution selDetail = (from c in dbc.Solutions
                                          where c.Soln_SolutionId == DetailID
                                          select c).First();
                    InfoModal.Show();
                    lblInfoText.Text = selDetail.soln_link;
                }               
            }