0

I'm trying to do something reasonably simple but for some reason something is going wrong and i can't for the life of me see why.

I have a data grid, on RowDataBound i want to loop through another DataSet and show multiple values within one cell.

So my basic code below is Looping through a dataset within a gridview RowDataBound, I set i = 0 on the loop and then i'm just writing out the value of a field in the dataset and the value of i, the problem is the first line written the value of i = 1 on every row of the grid, therefore its never writing out the first row in the dataset, does anyone know why this is or come across this before?

The variable i is not used anywhere else so can't be grabbing a value from somewhere.

Protected Sub gvReport_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvReport.RowDataBound


Dim cmdAgent As SqlCommand = New SqlCommand()
cmdAgent.Connection = PgsSQL
cmdAgent.CommandText = "spClientEstateAgent"
cmdAgent.CommandType = CommandType.StoredProcedure

Dim pCaseIdb As SqlParameter

pCaseIdb = New SqlParameter()
pCaseIdb.ParameterName = "@CaseId"
pCaseIdb.SqlDbType = SqlDbType.Int
pCaseIdb.Direction = ParameterDirection.Input
pCaseIdb.Value = Trim(CaseId)

cmdAgent.Parameters.Add(pCaseIdb)

'data adapter
Dim daAgent As SqlDataAdapter = New SqlDataAdapter
daAgent.SelectCommand = cmdAgent

'data set
Dim dsAgent As DataSet = New DataSet()
daAgent.Fill(dsAgent, "Agent")

cmdAgent.Dispose()
PgsSQL.Close()

Dim AgentCount As Integer = dsAgent.Tables(0).Rows.Count

        For i As Integer = 0 To AgentCount - 1

            lAgents.Text = dsAgent.Tables("Agent").Rows(i).Item("Name") & " : " & i

        Next

End Sub

Thanks in advance, J.

Jammer
  • 2,330
  • 11
  • 48
  • 77

1 Answers1

0

Not to worry, it was me missing something blatant!

As I was looping I was overwriting the previous value in the label lAgents so it was only ever showing the last value added!

So I created a string variable and concatenated all values to the string and then applied this to the lAgents.text after the loop.

What a plonker, but this might help someone else as stupid as me.

Jammer
  • 2,330
  • 11
  • 48
  • 77