how do i correct error 'name is neither a DataColumn nor a DataRelation for table'?

how do i correct error 'name is neither a DataColumn nor a DataRelation for table'?

i am trying to create a dropdownlist inside a datagrid. the dropdownlist datasource is "States" and the datagrid datasource is "rptPro"

my relevant aspx page is

<ItemTemplate>
<asp:Label id="Label2" Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "rptStuG") %>'> NOTE this work ok
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="edit_State" runat="server" SelectedIndex='<%# GetStateIndex(Container.DataItem("state").tostring()) %>'> NOTE  returns error "state is neither a DataColumn nor a DataRelation for table rptPro"
</asp:DropDownList>
</EditItemTemplate>


my code behind relevent code is

Public States As ArrayList
..
States = New ArrayList
        Dim mySelectQuery As String = "select rptL1 from rptDDL"
        Dim myCommand As SqlCommand = New SqlCommand(mySelectQuery, MyConn)
        MyConn.Open()
        Dim myReader As SqlDataReader
        myReader = myCommand.ExecuteReader
        While myReader.Read
            States.Add(myReader.GetString(0))
        End While
        myReader.Close()
        MyConn.Close()
...
Private Sub MyDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
        If Not (e.Item.ItemType = ListItemType.EditItem) Then
            Return
        End If
        Dim c As Control = e.Item.FindControl("edit_State")
        If c Is Nothing Then
            Return
        End If
        Dim dList As DropDownList = CType(c, DropDownList)
        dList.DataSource = States
        dList.DataBind()
    End Sub

    Public Function GetStateIndex(ByVal stateName As String) As Integer
        Dim myEnumerator As System.Collections.IEnumerator = States.GetEnumerator
        Dim i As Integer = 0
        While myEnumerator.MoveNext
            If myEnumerator.Current.ToString = stateName Then
                Return i
            End If
            System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
        End While
        Return 0

    End Function
...
Sub BindGrid()

        'Dim MyCommand As New SqlDataAdapter("select rptProId, rptPro, case when ASTU5019.Grade = '09' then rptStuG9 when ASTU5019.Grade = '10' then rptStuG10 when ASTU5019.Grade = '11' then rptStuG11 when ASTU5019.Grade = '12' then rptStuG12 End as rptStuG from rptPro p LEFT OUTER JOIN rptStu s ON p.rptProId = s.rptStuPro and s.rptStuLink=" & stuId & " LEFT JOIN  ASTU5019 on ASTU5019.stuLink = " & stuId & " order by p.rptProId", MyConn)
        'Dim MyCommand As New SqlDataAdapter("select rptProId, rptPro, rptStuG = (case when ASTU5019.Grade = '09' Then rptStuG9 else case when ASTU5019.Grade = '10' then rptStuG10 else case when ASTU5019.Grade = '11' then rptStuG11 else case when ASTU5019.Grade = '12' then rptStuG12 end end end end) from rptPro p LEFT OUTER JOIN rptStu s ON p.rptProId = s.rptStuPro and s.rptStuLink=" & stuId & " LEFT JOIN  ASTU5019 on ASTU5019.stuLink = " & stuId & " order by p.rptProId", MyConn)

        'OK to use Comment from junglerover with my add ASTU5019.Grade as Grade  
        Dim MyCommand As New SqlDataAdapter("select rptProId, rptPro, rptStuG = (case when ASTU5019.Grade = '09' Then rptStuG9 else case when ASTU5019.Grade = '10' then rptStuG10 else case when ASTU5019.Grade = '11' then rptStuG11 else case when ASTU5019.Grade = '12' then rptStuG12 end end end end), ASTU5019.Grade as Grade from rptPro p LEFT OUTER JOIN rptStu s ON p.rptProId = s.rptStuPro and s.rptStuLink=" & stuId & " LEFT JOIN  ASTU5019 on ASTU5019.stuLink = " & stuId & " order by p.rptProId", MyConn)

        ds = New DataSet
        MyCommand.Fill(ds, "rptPro")

        myDataGrid.DataSource = ds.Tables("rptPro").DefaultView
        myDataGrid.DataBind()

my aspx page is

<ItemTemplate>
<asp:Label id="Label2" Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "rptStuG") %>'> NOTE this work ok
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="edit_State" runat="server" SelectedIndex='<%# GetStateIndex(Container.DataItem("state").tostring()) %>'> NOTE  returns error
</asp:DropDownList>
</EditItemTemplate>
bianchefAsked:
Who is Participating?
 
the_paabCommented:
I use DropDownList (in datagrid template column) in this way in my projects:

<asp:DropDownList ID="ddlobor" runat="server" datavaluefield="id" datatextfield="zkratka" DataSource="<%#dtobor%>" selectedvalue='<%# DataBinder.Eval(Container, "DataItem.obor") %>'></asp:DropDownList>

What I don't see in your solution are dropdownlist properties:
Datasource
datavaluefield
datatextfield

Dtobor in datasource is datatable filled from db table on first open the page.
0
 
bianchefAuthor Commented:
by the way, i do get the correct dropdownlist values and names when i test only for

States = New ArrayList
        Dim mySelectQuery As String = "select rptL1 from rptDDL"
        Dim myCommand As SqlCommand = New SqlCommand(mySelectQuery, MyConn)
        MyConn.Open()
        Dim myReader As SqlDataReader
        myReader = myCommand.ExecuteReader
        While myReader.Read
            States.Add(myReader.GetString(0))
        End While
        myReader.Close()
        MyConn.Close()
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
bianchefAuthor Commented:
i changed
<asp:DropDownList id="edit_State" runat="server" SelectedIndex='<%# GetStateIndex(Container.DataItem("state").tostring()) %>'>
to
<asp:DropDownList id="edit_State" runat="server" SelectedIndex='<%# GetStateIndex(DataBinder.Eval(Container.DataItem,"state").tostring()) %>'>

with new error

Exception Details: System.Web.HttpException: DataBinder.Eval: 'System.Data.DataRowView' does not contain a property with the name state.
Source Error:
Line 43: <asp:DropDownList id="edit_State" runat="server" SelectedIndex='<%# GetStateIndex(DataBinder.Eval(Container.DataItem,"state").tostring()) %>'>

any ideas what i am doing wrong?
0
 
HappyFunBallCommented:
I don't see a state column being selected in your SELECT statement.  That would be why your getting the 'does not contain property' error.  It appears that you're selecting only rptProId, rptPro, rptStuG, and Grade as columns.  
0
 
bianchefAuthor Commented:
I found  a solution. Although no one provided the solution, both of your suggestions got me started to find my own solution (see below). For your help in giving me some starting questions to solve my code, I am splitting the points.

HappyFunBall

you got me thinking that
it is not the column but the datasource that i needed so i comment out column names and kept only the datasource as

        dList.DataSource = States
        'dList.DataValueField = "rptDDLid"
        'dList.DataTextField = "rptL1"
        dList.DataBind()

the_paab

you made me realized that the only property i needed was the datasource so as i was adding your suggested dropdownlist properties i rewrote my page simply as

<asp:datagrid id="myDataGrid" Runat="server" .... OnItemDataBound="MyDataGrid_ItemDataBound"...> NOTE i added OnItemDataBound
<asp:DropDownList id="edit_State" Runat="server"></asp:DropDownList>
                                                             

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.