System.Web.HttpException: 'DataBinding: 'System.Data.DataRowView' does not contain a property with the name

Anonymous
2021-06-30T16:10:07.033+00:00

Hello, I'm using DataBind for show a column of SQL DB in a DropDownList.
The program works but when I change the case of DropDownList, this issue appear.

void ddlTurno()
        {
            ddlTablas.DataSource = null;
            ddlTablas.DataBind();
            SqlConnection con = new SqlConnection(connectionString);
            string com = "Select DISTINCT Turno from ee";
            SqlDataAdapter adpt = new SqlDataAdapter(com, con);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            ddlTablas.DataSource = dt;
            ddlTablas.DataBind();
            ddlTablas.DataTextField = "Turno";
            ddlTablas.DataValueField = "Turno";
            ddlTablas.DataBind();
            PopulateGridview();
        }

void ddlNivel()
        {
            ddlTablas.DataSource = null;
            ddlTablas.DataBind();
            SqlConnection con = new SqlConnection(connectionString);
            string com = "Select DISTINCT Nivel from ee";
            SqlDataAdapter adpt = new SqlDataAdapter(com, con);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            ddlTablas.DataSource = dt;
            ddlTablas.DataBind();
            ddlTablas.DataTextField = "Nivel";
            ddlTablas.DataValueField = "Nivel";
            ddlTablas.DataBind();
            PopulateGridview();
        }

protected void ddlCondicion_SelectedIndexChanged(object sender, EventArgs e)
        {
if (ddlCondicion.SelectedValue == "Turno")
            {
                ddlTurno();
            }
            if (ddlCondicion.SelectedValue == "Nivel")
            {
                ddlNivel();
            }
}

If I select first "Turno" in DDL and then "Nivel" the error message is:

'System.Data.DataRowView' does not contain a property with the name 'Turno'
But if first I select "Nivel", then "Turno" the message is:
'System.Data.DataRowView' does not contain a property with the name 'Nivel'

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2021-06-30T18:01:36.403+00:00

    Try removing the ddlTablas.DataBind() lines which are executed prematurely, before defining DataTextField and DataValueField.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.