views:

503

answers:

2
Try
    Dim ds As DataSet = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings("connstr").ConnectionString, CommandType.StoredProcedure, "Get_All", New SqlParameter("@userid", Session("userid")))


    rptBundles.DataSource = ds.Tables(0)

Catch ex As Exception
    showerror(ex, Me)
End Try

That is the code, I want to be able to parse through it and find certain rows that have a certain boolean set to 1 and then edit other variables in that row accordingly, how would I do this, I tried making a For Each row nested in a For Each table but when I tested it the repeater never populates with data...

    For Each ds_table As DataTable In ds.Tables
        For Each ds_row As DataRow In ds_table.Rows
            If ds_row("isScorm") = 1 Then
                ds_row("totalLessons") = 100
                ds_row("TotalQuestions") = 100
                ds_row("lessonscompleted") = 50
                ds_row("TotalCorrect") = 50
            End If
        Next
    Next

Only when I remove that code does the repeater populate as expected, but I got no errors!

+1  A: 

If you're using a Repeater, or whatever datasource bound control, I would use the ItemDataBound event and set those values to your controls.

If this was your basic HTML

<html>
  <asp:Repeater id="repeater" runat="server" OnItemDataBound="repeater_ItemDatabound">
     <ItemTemplate>
         <span><%# DataBinder.Eval(Container.DataItem, "isScorm") %></span>
         <span id="totalLessonsSpan" runat="server"><%# DataBinder.Eval(Container.DataItem, "totalLessons") %></span>
     </ItemTemplate>
  </asp:Repeater>
</html>

I would have this in the code behind

protected void repeater_ItemDatabound(object sender, RepeaterItemEventArgs e)
{
    DataRow row = e.Item.DataItem as DataRow;

    if (row == null) { }
    else
    {
        int isScorm = 0;
        int.TryParse(Convert.ToString(row["isScorm"]), out isScorm);
        if (isScorm > 0)
        {
            HtmlGenericControl totalLessonsSpan = e.Item.FindControl("totalLessonsSpan") as HtmlGenericControl;
            totalLessonsSpan.Text = "100";
        }
    }
}

You probably don't want to loop through the data and swap it there, then bind when you can do it during the bind.

Alternately, something I hate that DB's do because of my need for data integrity, is change it in your SQL select with case statements.

hunter
+1  A: 

Does adding rptBundles.DataBind() after setting DataSource fix the problem?

Also, you might want to check out the DataTable.Select method to only select (and then modify) rows where isScorm = 1.

Si