user2797643
user2797643

Reputation: 1

Repeater control search button

My repeater control is showing the database fields on load.

I have a search button outside the repeater control. When the button is pressed, I want the repeater to show only the searched result in the repeater. How can I do that?

This is the code for showing all.On search i don't want to show below details inside repeater.just searched results.

Protected Sub BindRepeater() Dim cmd As New SqlCommand("Select * from abc", con)

    If con.State = ConnectionState.Closed Then

        con.Open()

    End If

    Dim ds As New DataSet()

    Dim adp As New SqlDataAdapter(cmd)

    adp.Fill(ds)

    ' Repeater1.DataSource = ds

    Repeater1.DataBind()

    con.Close()

end sub bELOW IS THE NEW CODE I HAVE ADDED IN SEARCH BUTTON

       cmd = New SqlCommand
    cmd.Connection = con

   cmd.CommandText = "select * from ABC where LicenseID = '" & TextBox16.Text & "'"
      drd = cmd.ExecuteReader
      If drd.HasRows = True Then
    drd.Read()

   End If

     Using con = New SqlConnection("Data Source=ABC-556012RT13\SQLEXPRESS;Initial      Catalog=KABC;Integrated Security=True")
    Using da = New SqlDataAdapter("select * from ABC where LicenseID = @LicenseID", con)
        da.SelectCommand.Parameters.AddWithValue("@LicenseID", TextBox16.Text)
Dim table = New DataTable
  da.Fill(table)
 Repeater1.DataSource = table
        Repeater1.DataBind()
    End Using
End Using       

Upvotes: 0

Views: 702

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460208

Use sql-parameters to prevent sql-injection!

Apart from that, just use the result of the query as datasource of the repeater, for example by filling a DataTable:

Using con = New SqlConnection("connectionString")
    Using da = New SqlDataAdapter("select * from abc where LicenseID = @LicenseID", con)
        da.SelectCommand.Parameters.AddWithValue("@LicenseID", TextBox16.Text)
        Dim table = New DataTable
        da.Fill(table)
        repeater1.DataSource = table
        repeater1.DataBind()
    End Using
End Using

I'm also using the using-statement to ensure that all unmanaged resources(like an open connection) are disposed even on error.

Upvotes: 0

Related Questions