Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using datalist to display search results, for example i'm retrieving this sample of fields in this image

I'm retrieving the name and description using [Text='<%# Eval("field") %>'] in their corresponding labels, and retrieving the category and the user using code behind

This work fine for the first item only as you see, but the rest of the item shows blanks. The reason am using this approach that i'm storing the values of user and categories as foreign keys (ID'S) to different tables other than the one i bind the datalist to it, and i want to display texts instead of the id numbers in the search results. How can i make those two labels (category and user) get populated in similar way like the ones with the Eval.

P.S. All the four labels are in one <itemtemplate>

What I have tried:

VB
For row = 0 To rowcount - 1
    idd = dt1.Rows(row)(0)



    DetailsAdapter2 = New SqlDataAdapter(" select Category.subcategory, Users.username from [Category]  JOIN [LostItem] ON (Category.CategoryID = LostItem.CategoryID) JOIN [Users] ON (LostItem.[User] = Users.[userid] ) WHERE (LostItem.LostId=" & idd & "    ) ", con)
    Dim dt2 As New DataTable

    DetailsAdapter2.Fill(dt2)

 Dim rowcount1 As Integer = dt2.Rows.Count
 For row1 = 0 To rowcount1 - 1


    Dim CategoryLabel As Label = DirectCast(DataList1.Items(0).FindControl("CategoryLabel"), Label)
    CategoryLabel.Text = DirectCast(dt2.Rows(row1)(0), String)
    Dim UserLabel As Label = DirectCast(DataList1.Items(0).FindControl("UserLabel"), Label)
    UserLabel.Text = DirectCast(dt2.Rows(row1)(1), String)
 Next
Next

Full Code:

VB
< Dim command As SqlDataAdapter
        command = New SqlDataAdapter("select * FROM Lostitem WHERE city=@city AND datelost=@datelost AND (name Like '%' + @name + '%' OR name Like '%' + '""' + '%') ", con)
        Dim ide2 As Integer = ddlCities.SelectedValue
        command.SelectCommand.Parameters.AddWithValue("@City", ide2)
        command.SelectCommand.Parameters.AddWithValue("@datelost", TextBox1.Text)
        command.SelectCommand.Parameters.AddWithValue("@name", TextBox2.Text)
        Dim DetailsAdapter2 As SqlDataAdapter

        Dim dt1 As New DataTable
       
        command.Fill(dt1)
        DataList1.DataSource = dt1

        DataList1.DataBind()
        If Not Me.Page.User.Identity.IsAuthenticated Then
            Dim ClaimButton As Button = DirectCast(DataList1.Items(0).FindControl("ClaimButton"), Button)
            ClaimButton.Visible = False
        End If
        Dim rowcount As Integer = dt1.Rows.Count
        Dim idd As Integer
        For row = 0 To rowcount - 1
            idd = dt1.Rows(row)(0)



            DetailsAdapter2 = New SqlDataAdapter(" select Category.subcategory, Users.username from [Category]  JOIN [LostItem] ON (Category.CategoryID = LostItem.CategoryID) JOIN [Users] ON (LostItem.[User] = Users.[userid] ) WHERE (LostItem.LostId=" & idd & "    ) ", con)
            Dim dt2 As New DataTable

            DetailsAdapter2.Fill(dt2)

            Dim rowcount1 As Integer = dt2.Rows.Count
            For row1 = 0 To rowcount1 - 1


                Dim CategoryLabel As Label = DirectCast(DataList1.Items(0).FindControl("CategoryLabel"), Label)
                CategoryLabel.Text = DirectCast(dt2.Rows(row1)(0), String)
                Dim UserLabel As Label = DirectCast(DataList1.Items(0).FindControl("UserLabel"), Label)
                UserLabel.Text = DirectCast(dt2.Rows(row1)(1), String)
            Next
        Next
Posted
Updated 5-Aug-19 6:05am
v3

1 solution

First off, appending the values from your input to your SQL statement is a big NO NO, as it can lead you to SQL injection attack. Read: Protect Your Data: Prevent SQL Injection[^]

Second, please use the debugger to figure out what went wrong with your code logic. You simply need to set a break point and step into your code to see what's going on there.

Third, I'm not a VB person but looking at your code, it seems like you are always overwriting the first items of your DataList. Can you try doing something like this instead:

VB
Dim CategoryLabel As Label = DirectCast(DataList1.Items(row1).FindControl("CategoryLabel"), Label)
CategoryLabel.Text = DirectCast(dt2.Rows(row1)(0), String)
Dim UserLabel As Label = DirectCast(DataList1.Items(row1).FindControl("UserLabel"), Label)
UserLabel.Text = DirectCast(dt2.Rows(row1)(1), String)



What I changed there is just update the DataList Item index value to row1 instead of 0.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900