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.
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:
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
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: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>