Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I'm trying to display rows in a GridView that match the ID which is cast into a Label. I get an error "Input string was not in a correct format" when I run this query.

VB
Private Sub BindGrid()
        Dim constring As String = ConfigurationManager.ConnectionStrings("connstr").ConnectionString
        Using con As New SqlConnection(constring)
            Using cmd As New SqlCommand("SELECT * from [table] WHERE ID=@Label6")
                Using sda As New SqlDataAdapter()
                    cmd.Connection = con
                    cmd.Parameters.Add("@Label6", Label.Text)
                    sda.SelectCommand = cmd
                    Using dt As New DataTable()
                        sda.Fill(dt)
                        GridView2.DataSource = dt
                        GridView2.DataBind()
                    End Using
                End Using
        End Using
    End Using
End Sub


Here is the Code for Label:

VB
 Public Sub LinkButton1_Click(sender As Object, e As EventArgs)
    Dim Lnk As LinkButton = DirectCast(sender, LinkButton)
    Label6.Text = Lnk.Text
    LinkButton1_ModalPopupExtender.Show()
End Sub


I've used the following
VB
cmd.Parameters.Add("@Label6", SqlDbType.Int)
                   cmd.Parameters.Add("Label6", SqlDbType.Int).Value = Label6.Text
                   cmd.Parameters.Add("@Label6", SqlDbType.Int).Value = Convert.ToInt32(Label6.Text)
                   cmd.Parameters.AddWithValue("@Label6", Convert.ToInt16(Label6.Text))
                   cmd.Parameters.AddWithValue("@Label6", Label6.Text)


Here is the .aspx

ASP.NET
<asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton>
   <cc1:ModalPopupExtender ID="LinkButton1_ModalPopupExtender" runat="server" Enabled="True" TargetControlID="lnkDummy" PopupControlID="Panel1"> </cc1:ModalPopupExtender>
   <asp:Panel ID="Panel1" runat="server" Height="164px" Width="284px" BackColor="SlateGray" ><br /><br />
         <center><asp:Label ID="Label6" runat="server" Text="Label"></asp:Label><br />
             <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false"
       PageSize="2" AllowPaging="true">
                 <Columns>

               <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
               <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
               <asp:BoundField DataField="Processed_By" HeaderText="Processed_By" SortExpression="Processed_By" />

           </Columns>
             </asp:GridView>
         <asp:Button ID="Button3" runat="server" CssClass="btn btn-default btn-md" Text="Close"/></center>
   </asp:Panel>
Posted
Updated 14-Jun-15 22:01pm
v2
Comments
Andy Lanng 15-Jun-15 4:09am    
hmm. And you got the same error from all the variants of parameter you tried?

Just as a side-note, I would use an int variable which I would assign the parsed int of the label. That way you can check that it is a valid int before you get to the query.

Try doing that and debug to see what value it's trying to parse. It might be that the label has not been populated by the time your query runs
ZurdoDev 15-Jun-15 10:15am    
Please be clear on which line of code the error is happening.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900