Hi guys, I having problem display out the value at the gridview. At the database it store the gender field in integer. For example 1 = Male and etc.
I need to display out the text at gridview column. I getting error input string was not correct. Please help

Protected Sub gvCustomer_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCustomer.RowDataBound
             If e.Row.RowType = DataControlRowType.DataRow Then

            Dim gender As Integer = Convert.ToInt32(e.Row.Cells(5).Text)

                If gender = 1 Then
                    e.Row.Cells(5).Text = "MALE"
                ElseIf gender = 2 Then
                    e.Row.Cells(5).Text = "FEMALE"
                End If

          End If
    End Sub

Line No 4 is causing error i guess.

 Dim gender As Integer = Convert.ToInt32(e.Row.Cells(5).Text)

Thats because e.Row.Cells(5).Text will have null value as it is not binded.

You can do it in two ways :
1.Insert direct inline code in aspx upon the condition like this :

<ItemTemplate>
     <%# (Convert.ToInt32(Eval("gender"))=="1"?"Male": "Female") %>
 </ItemTemplate>

2.Store gender value in hiddenfield like this :

In Aspx :

<ItemTemplate>
    <asp:Label ID="lblGender" runat="server"></asp:Label>
    <asp:HiddenField runat="server" ID="hdnGender" Value='<%# Eval("gender")%>' />
</ItemTemplate>

In Backend :

If e.Row.RowType = DataControlRowType.DataRow Then
        Label lblGender = (Label)e.Row.FindControl("lblGender")
        HiddenField hdnGender = (HiddenField)e.Row.FindControl("hdnGender")
        If hdnGender.value = 1 Then
            lblGender.Text = "MALE"
        ElseIf hdnGender.value = 2 Then
            lblGender.Text = "FEMALE"
        End If
End If

Code above might contain C# format and have to be changed in VB.

Hope it solves the issue.

The error could be occured in the following line.

Dim gender As Integer = Convert.ToInt32(e.Row.Cells(5).Text*)

Put a breakpoint and see if the value of e.Row.Cells(5).Text is a number or empty string. It seems the index of the data cells(GridView column) may not be 5 or returns a non numeric value for some rows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.