0

I would think this code should be very simple and straight forward but I'm having issues. The sub below is called on the page load. It is nestled between similar functions that load other drop down lists. All the rest of the drop downs are populating but these are not. The other drop downs subs use the same With clause but are binding to a DataSet (of one table) that is populated with a stored procedure call to the database rather than binding to a static DataTable.

VB Code:

   Private Sub LoadEquations()
            Dim dtMain As DataTable
            Dim dtGypsum As DataTable
            Dim dr As DataRow

            dtMain.Columns.Add("Text")
            dtMain.Columns.Add("Value")

            dr = dtMain.NewRow
            dr.Item("Text") = " "
            dr.Item("Value") = "0"
            dtMain.Rows.Add(dr)
            dtMain.AcceptChanges()

            dr = dtMain.NewRow
            dr.Item("Text") = "Build"
            dr.Item("Value") = "B"
            dtMain.Rows.Add(dr)
            dtMain.AcceptChanges()

            dr = dtMain.NewRow
            dr.Item("Text") = "Maintain"
            dr.Item("Value") = "M"
            dtMain.Rows.Add(dr)
            dtMain.AcceptChanges()

            With Me.ddMainEquation
                .DataSource = dtMain
                .DataTextField = "Text"
                .DataValueField = "Value"
                .DataBind()
            End With


            dtGypsum.Columns.Add("Text")
            dtGypsum.Columns.Add("Value")

            dr = dtGypsum.NewRow
            dr.Item("Text") = " "
            dr.Item("Value") = "0"
            dtGypsum.Rows.Add(dr)
            dtGypsum.AcceptChanges()

            dr = dtGypsum.NewRow
            dr.Item("Text") = "pH Base"
            dr.Item("Value") = "P"
            dtGypsum.Rows.Add(dr)
            dtGypsum.AcceptChanges()

            dr = dtGypsum.NewRow
            dr.Item("Text") = "% Sodium Base"
            dr.Item("Value") = "S"
            dtGypsum.Rows.Add(dr)
            dtGypsum.AcceptChanges()

            With Me.ddGypsumEquation
                .DataSource = dtGypsum
                .DataTextField = "Text"
                .DataValueField = "Value"
                .DataBind()
            End With
        End Sub

DNN / ASP Code:

<tr>
    <td>
        <asp:Label ID="lblMainEquation" runat="server" 
            CssClass="FormLabelLeft">Main Equation</asp:Label></td>
    <td>
        <asp:Label ID="lblGypsumEquation" runat="server" 
            CssClass="FormLabelLeft">Gypsum Equation</asp:Label></td>
</tr>
<tr>
    <td>
        <asp:DropDownList ID="ddMainEquation" TabIndex="34" 
            runat="server" CssClass="FormField"></asp:DropDownList></td>
    <td>
        <asp:DropDownList ID="ddGypsumEquation" TabIndex="34" 
            runat="server" CssClass="FormField"></asp:DropDownList></td>
</tr>

What am I missing? I am using an older DNN platform 2.0.

Win
  • 61,100
  • 13
  • 102
  • 181
  • Your code look fine. You need to set a break point inside `LoadEquations()` and ensure that `LoadEquations()` is called. – Win Jul 09 '14 at 22:23
  • Try change **Dim dtMain As DataTable** to **Dim dtMain As _New_ DataTable**. Same for **Dim dtGypsum As New DataTable** – HengChin Jul 10 '14 at 00:59

1 Answers1

1

You should declare your datatable as a New object

Try below:

 Private Sub LoadEquations()
    Dim dtMain As New DataTable
    Dim dtGypsum As New DataTable
    Dim dr As DataRow

    dtMain.Columns.Add("Text")
    dtMain.Columns.Add("Value")

    dr = dtMain.NewRow
    dr.Item("Text") = " "
    dr.Item("Value") = "0"
    dtMain.Rows.Add(dr)

    dr = dtMain.NewRow
    dr.Item("Text") = "Build"
    dr.Item("Value") = "B"
    dtMain.Rows.Add(dr)

    dr = dtMain.NewRow
    dr.Item("Text") = "Maintain"
    dr.Item("Value") = "M"
    dtMain.Rows.Add(dr)

    With ddMainEquation
        .DataSource = dtMain
        .DataTextField = "Text"
        .DataValueField = "Value"
        .DataBind()
    End With

    dtGypsum.Columns.Add("Text")
    dtGypsum.Columns.Add("Value")

    dr = dtGypsum.NewRow
    dr.Item("Text") = " "
    dr.Item("Value") = "0"
    dtGypsum.Rows.Add(dr)

    dr = dtGypsum.NewRow
    dr.Item("Text") = "pH Base"
    dr.Item("Value") = "P"
    dtGypsum.Rows.Add(dr)

    dr = dtGypsum.NewRow
    dr.Item("Text") = "% Sodium Base"
    dr.Item("Value") = "S"
    dtGypsum.Rows.Add(dr)

    With ddGypsumEquation
        .DataSource = dtGypsum
        .DataTextField = "Text"
        .DataValueField = "Value"
        .DataBind()
    End With
End Sub

By the way, AcceptChanges() is not really required for each time after you add in a new row. Calling AcceptChanges after adding new row will actually turn the DataRowState of your newly added DataRow from Added to Unchanged. check this out.

Community
  • 1
  • 1
HengChin
  • 583
  • 5
  • 16