使用标签查询数据库时出错

时间:2015-06-15 07:52:21

标签: sql-server vb.net

我正在尝试在GridView中显示与投射到Label中的ID匹配的行。我运行此查询时收到错误“输入字符串格式不正确”。

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

以下是标签代码:

 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

我做错了什么? ID是int。

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>

1 个答案:

答案 0 :(得分:2)

Add没有带参数名称和值的重载。 改变这一行:

cmd.Parameters.Add("@Label6", Label.Text)

到此:

cmd.Parameters.Add("@Label6", SqlDbType.int).value = Label.Text