locked
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'pname'. RRS feed

  • Question

  • User1403110688 posted

    I CROSS CHECKED FOR COLUMN SPELLINGS ETC BUT ITS TOTALLY SAME AS I HAVE IN MY TABLE, CAN ANY ONE HELP ME TO FIND ERROR 

    My ASP CODE 

    <asp:TemplateField HeaderText="Product Name">
    <ItemTemplate>
    <asp:Label ID="lbldropname" runat="server" Text='<% # Eval("pname") %>' Visible = "true" />
    <asp:DropDownList ID="droppname" runat="server" ></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>

    AND CODE BEHIND 

    Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If (e.Row.RowType = DataControlRowType.DataRow) Then

    'Find the DropDownList in the Row
    Dim droppname As DropDownList = CType(e.Row.FindControl("droppname"), DropDownList)
    droppname.DataSource = GetData("SELECT DISTINCT pname from product")
    droppname.DataTextField = "pname"
    droppname.DataValueField = "pname"
    droppname.DataBind()

    'Add Default Item in the DropDownList
    droppname.Items.Insert(0, New ListItem("Please select"))

    'Select the Country of Customer in DropDownList
    Dim pname As String = CType(e.Row.FindControl("droppname"), Label).Text
    droppname.Items.FindByValue(pname).Selected = True
    End If
    End Sub

    Private Function GetData(ByVal query As String) As DataSet
    Dim conString As String = ConfigurationManager.ConnectionStrings("cn").ConnectionString
    Dim cmd As New SqlCommand(query)
    Using con As New SqlConnection(conString)
    Using sda As New SqlDataAdapter()
    cmd.Connection = con
    sda.SelectCommand = cmd
    Using ds As New DataSet()
    sda.Fill(ds)
    Return ds
    End Using
    End Using
    End Using
    End Function

    ON PAGE LOAD 

    GridView1.DataSource = GetData("select pname from product")
    GridView1.DataBind()

    Friday, May 12, 2017 6:31 PM

Answers

  • User-1838255255 posted

    Hi iampoojaarora,

    According to your description and code, I know if you use the following code to bind data to gridview, occur this error is right.

    Because in the datatable, doesn't contain a column(pname). I guess after you comment them, gridview through data datasource to bind data.

    Code:

    Public Sub SetInitialRow()
        Dim dt As New DataTable()
        Dim dr As DataRow = Nothing
    
        dt.Columns.Add(New DataColumn("Row", GetType(String)))
        dt.Columns.Add(New DataColumn("Column1", GetType(String)))
        dt.Columns.Add(New DataColumn("Column2", GetType(String)))
        dt.Columns.Add(New DataColumn("Column3", GetType(String)))
        dt.Columns.Add(New DataColumn("Column4", GetType(String)))
        dt.Columns.Add(New DataColumn("Column5", GetType(String)))
        dr = dt.NewRow()
        dt.Rows.Add("1", String.Empty, String.Empty, String.Empty, String.Empty, String.Empty)
        dt.Rows.Add(dr)
        ViewState("CurrentTable") = dt
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

    if you want to use the pname field, you need add a new column(pname) in datatable.

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 15, 2017 7:50 AM
  • User-1716253493 posted

    I Agree with previous post

    You need additional

    dt.Columns.Add(New DataColumn("pname", GetType(String)))

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 15, 2017 8:02 AM

All replies

  • User2103319870 posted

    I CROSS CHECKED FOR COLUMN SPELLINGS ETC BUT ITS TOTALLY SAME AS I HAVE IN MY TABLE, CAN ANY ONE HELP ME TO FIND ERROR 

    Another cause for this issue could be no data is returning from GetData method and you are trying to bind and empty result set. Try adding a try catch block and see if GetData method is having any exceptions

     Private Function GetData(ByVal query As String) As DataSet
            Try
                Dim conString As String = ConfigurationManager.ConnectionStrings("cn").ConnectionString
                Dim cmd As New SqlCommand(query)
                Using con As New SqlConnection(conString)
                    Using sda As New SqlDataAdapter()
                        cmd.Connection = con
                        sda.SelectCommand = cmd
                        Using ds As New DataSet()
                            sda.Fill(ds)
                            Return ds
                        End Using
                    End Using
                End Using
            Catch ex As Exception
                Throw ex.InnerException
            End Try
        End Function

    Friday, May 12, 2017 8:23 PM
  • User-1716253493 posted

    Try remove if any datasourceid value from the grid or dropdown in aspx code

    Saturday, May 13, 2017 4:53 AM
  • User1403110688 posted

    yes thats i did and also placed grid code in another class ..

    but now i found actual problem due to taking dynamic rows because when make it comment its successfully bound .. but when i un comment it the getting same error 

    error code 

    Public Sub SetInitialRow()

    Dim dt As New DataTable()
    Dim dr As DataRow = Nothing


    dt.Columns.Add(New DataColumn("Row", GetType(String)))
    dt.Columns.Add(New DataColumn("Column1", GetType(String)))
    dt.Columns.Add(New DataColumn("Column2", GetType(String)))
    dt.Columns.Add(New DataColumn("Column3", GetType(String)))
    dt.Columns.Add(New DataColumn("Column4", GetType(String)))
    dt.Columns.Add(New DataColumn("Column5", GetType(String)))
    dr = dt.NewRow()
    dr("Row") = 1
    dr("Column1") = String.Empty
    dr("Column2") = String.Empty
    dr("Column3") = String.Empty
    dr("Column4") = String.Empty
    dr("Column5") = String.Empty
    dt.Rows.Add(dr)
    ViewState("CurrentTable") = dt
    Gridview1.DataSource = dt
    GridView1.DataBind()


    End Sub

    help me to find problem in this code ?

    Sunday, May 14, 2017 7:08 PM
  • User-1838255255 posted

    Hi iampoojaarora,

    According to your description and code, I know if you use the following code to bind data to gridview, occur this error is right.

    Because in the datatable, doesn't contain a column(pname). I guess after you comment them, gridview through data datasource to bind data.

    Code:

    Public Sub SetInitialRow()
        Dim dt As New DataTable()
        Dim dr As DataRow = Nothing
    
        dt.Columns.Add(New DataColumn("Row", GetType(String)))
        dt.Columns.Add(New DataColumn("Column1", GetType(String)))
        dt.Columns.Add(New DataColumn("Column2", GetType(String)))
        dt.Columns.Add(New DataColumn("Column3", GetType(String)))
        dt.Columns.Add(New DataColumn("Column4", GetType(String)))
        dt.Columns.Add(New DataColumn("Column5", GetType(String)))
        dr = dt.NewRow()
        dt.Rows.Add("1", String.Empty, String.Empty, String.Empty, String.Empty, String.Empty)
        dt.Rows.Add(dr)
        ViewState("CurrentTable") = dt
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

    if you want to use the pname field, you need add a new column(pname) in datatable.

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 15, 2017 7:50 AM
  • User-1716253493 posted

    I Agree with previous post

    You need additional

    dt.Columns.Add(New DataColumn("pname", GetType(String)))

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 15, 2017 8:02 AM