文本框搜索Gridview与存储过程ASP.NET

时间:2012-12-21 05:15:37

标签: asp.net gridview

我有存储过程sp_ItemFilter,但问题是当我在文本框中插入数据时,我按btnButton发送数据,未显示在gridview中变空,我该怎么解决这个问题? 使用的代码。

 Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Cmd = New SqlCommand("sp_codefilter", Cnn)
    Cmd.CommandType = CommandType.StoredProcedure
    With Cmd.Parameters
        .Add("@codigo", SqlDbType.VarChar, 6).Value = TextBox1.Text
        Cnn.Open()
        Try
            Dim dv As New DataView
            Da = New SqlDataAdapter(Cmd)
            Ds = New DataSet
            GridView1.DataSource = Ds.Tables()
            GridView1.DataBind()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Cnn.Close()
        End Try
    End With
End Sub
End Class

感谢阅读我的帖子

2 个答案:

答案 0 :(得分:1)

试试这个

Da = New SqlDataAdapter(Cmd)
Ds = New DataSet   //<---- this DataSet is empty and needs filling

Da.Fill(Ds);  //<------- You need to fill the DataSet here

GridView1.DataSource = Ds.Tables(0)
GridView1.DataBind()

答案 1 :(得分:0)

您需要填充数据集,设置select命令,并将特定表绑定到GridView:

Dim Cmd As New SqlCommand("sp_codefilter", Cnn)
Cmd.CommandType = CommandType.StoredProcedure
With Cmd.Parameters
    .Add("@codigo", SqlDbType.VarChar, 6).Value = TextBox1.Text
End With

Cnn.Open()
Try
    Dim dv As New DataView
    Da = New SqlDataAdapter()
    Ds = New DataSet
    Da.SelectCommand = Cmd
    Da.Fill(ds, "MyTable");
    GridView1.DataSource = Ds.Tables("MyTable")
    GridView1.DataBind()
Catch ex As Exception
    MsgBox(ex.Message)
Finally
    Cnn.Close()
End Try