Input string was not in a correct format

B

Bart

Hi,

i defined a linkbutton in a gridview. In code-behind, i want to perform the
delete query when the user clicks on the linkbutton, but this code gives
following error:
"Input string was not in a correct format." at line: r =
Convert.ToInt16(e.CommandArgument)

Thanks for help
Bart


aspx file:
--------
<asp:GridView ID="GridView1" runat="server" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb1" runat="server" CommandName="Delete"
Text="delete"
OnClientClick="return confirm('Cancel?');" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
....

code-behind:
-------------
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
GridView1.RowCommand
Dim r As Int16
Select Case e.CommandName
Case "Delete"
r = Convert.ToInt16(e.CommandArgument)
Me.GridView1.DeleteRow(r)
End Select
End Sub
 
G

gnewsgroup

Hi,

i defined a linkbutton in a gridview. In code-behind, i want to perform the
delete query when the user clicks on the linkbutton, but this code gives
following error:
"Input string was not in a correct format." at line:  r =
Convert.ToInt16(e.CommandArgument)

Thanks for help
Bart

aspx file:
--------
<asp:GridView ID="GridView1" runat="server" />
           <Columns>
           <asp:TemplateField>
               <ItemTemplate>
               <asp:LinkButton ID="lb1" runat="server" CommandName="Delete"
Text="delete"
                OnClientClick="return confirm('Cancel?');" >
               </asp:LinkButton>
               </ItemTemplate>
           </asp:TemplateField>
...

code-behind:
-------------
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
GridView1.RowCommand
        Dim r As Int16
        Select Case e.CommandName
            Case "Delete"
                r = Convert.ToInt16(e.CommandArgument)
                Me.GridView1.DeleteRow(r)
        End Select
End Sub

Because you did not assign any values to CommandArgument, and by
default it's String.Empty, which cannot be converted to an Int16.
 
B

Bart

Thanks for replying, but

I have no idea which value to assign to CommandArgument. I guess it needs
the index of the selected row ...


"gnewsgroup" <[email protected]> schreef in bericht
Hi,

i defined a linkbutton in a gridview. In code-behind, i want to perform
the
delete query when the user clicks on the linkbutton, but this code gives
following error:
"Input string was not in a correct format." at line: r =
Convert.ToInt16(e.CommandArgument)

Thanks for help
Bart

aspx file:
--------
<asp:GridView ID="GridView1" runat="server" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb1" runat="server" CommandName="Delete"
Text="delete"
OnClientClick="return confirm('Cancel?');" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
...

code-behind:
-------------
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
GridView1.RowCommand
Dim r As Int16
Select Case e.CommandName
Case "Delete"
r = Convert.ToInt16(e.CommandArgument)
Me.GridView1.DeleteRow(r)
End Select
End Sub

Because you did not assign any values to CommandArgument, and by
default it's String.Empty, which cannot be converted to an Int16.
 
G

gnewsgroup

Thanks for replying, but

I have no idea which value to assign  to CommandArgument. I guess it needs
the index of the selected row ...

"gnewsgroup" <[email protected]> schreef in bericht





Because you did not assign any values to CommandArgument, and by
default it's String.Empty, which cannot be converted to an Int16.- Hide quoted text -

- Show quoted text -

So, you would like to have the row index? Can you do the following?

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>

It should work for a LinkButton, too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top