I am working on a project, its purpose is to search a database and display the results in a data list so they can be used. I have the search working correctly but it only outputs to a data grid. Help please!

Protected Sub Button2_OnClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim objCommand As OleDbCommand
        Dim objAdapter As OleDbDataAdapter
        Dim objDataSet As DataSet
        Dim strSearch As String

        Dim objConnection As String = ("Provider=Microsoft.ACE.OLEDB.12.0;" _
            & "Data Source=" & Server.MapPath("DB\ScrumManagementSystem.accdb") & ";")

        Dim cn As New OleDbConnection(objConnection)
        Dim strSQLQuery As String

        strSearch = TextBox1.Text

        If Len(Trim(strSearch)) > 0 Then

            strSQLQuery = "SELECT FName & SName FROM tblUser WHERE FName LIKE '%" & Replace(strSearch, "'", "''") & "%' ORDER BY FName;"

            cn.Open()
            Dim cmd As New OleDbCommand(strSQLQuery, cn)

            objCommand = New OleDbCommand(strSQLQuery, cn)

            objAdapter = New OleDbDataAdapter(objCommand)
            objDataSet = New DataSet()
            objAdapter.Fill(objDataSet)

            dgPaging1.DataSource = objDataSet
            dgPaging1.DataBind()

            cn.Close()
        Else
            TextBox1.Text = "Enter search criteria!"
        End If
    End Sub

Dani AI

Generated

Short answer: yes — a DataList can be bound programmatically the same way a GridView can. As suggested, define an ItemTemplate (and any AlternatingItemTemplate/Header/Footer you need) and then set the DataList's DataSource to your DataTable/DataView and call DataBind(). The DataList renders whatever you put in the templates, so you must create the markup that shows the fields and the buttons/links you want to use. (learn.microsoft.com)

Example ItemTemplate (place this in the .aspx where the DataList lives):

<asp:DataList ID="dlResults" runat="server">
  <ItemTemplate>
    <asp:Label ID="lblName" runat="server" Text='<%# Eval("FullName") %>' />
    <asp:LinkButton ID="lnkUse" runat="server"
      CommandName="Use" CommandArgument='<%# Eval("UserID") %>' Text="Use" />
  </ItemTemplate>
</asp:DataList>

Data-binding expressions inside templates use <%# Eval("FieldName") %> (one-way/read-only) or Bind(...) for two-way binding; those expressions are evaluated when you call DataBind() on the control or page. Make sure the field names/aliases in your SELECT match the names used in Eval. (learn.microsoft.com)

A safe, minimal pattern for the search click handler (VB) is to use parameterized SQL and Using blocks so connections/commands are disposed correctly, then bind the resulting DataTable to the DataList:

Dim sql As String = "SELECT FName & ' ' & SName AS FullName, UserID FROM tblUser WHERE FName LIKE ? ORDER BY FName"
Dim dt As New DataTable()
Using cn As New OleDb.OleDbConnection(connString)
  Using cmd As New OleDb.OleDbCommand(sql, cn)
    cmd.Parameters.Add("?", OleDb.OleDbType.VarChar, 100).Value = "%" & search & "%"
    Using da As New OleDb.OleDbDataAdapter(cmd)
      da.Fill(dt)
    End Using
  End Using
End Using

dlResults.DataSource = dt
dlResults.DataBind()

For OleDb remember parameter placeholders are question marks and parameters are applied in order. Use Using/End Using to ensure disposal. (learn.microsoft.com)

Quick troubleshooting checklist: verify the SELECT uses an alias (e.g., FullName) that matches Eval, check dt.Rows.Count to confirm data was returned, ensure DataBind() is called after assigning DataSource, and remember DataList does not offer automatic sorting/paging/updates the way GridView does — those need to be implemented in code (for example, sort the DataTable/DataView or re-query with different ORDER BY). (learn.microsoft.com)

Recommended Answers

All 2 Replies

You have to configure ItemTemplate and other templates of . Take a look at tutorial - http://www.asp.net/Learn/data-access/tutorial-29-vb.aspx

Not sure what to make of that? When using DataList, is it not possible to use data bind like i would have done with a typical gridview? Also the data i am reaning in is coming from a database (2007) after a search criteria has been applied, so selecting a data source wont work. Help please

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.