locked
Error - DataBinding: 'System.Data.DataRowView' does not contain a property with the name '[No_]'. RRS feed

  • Question

  • User-1499457942 posted

    Hi

      On below code i get above error

    using (SqlConnection con = new SqlConnection(CommonFunction.cnn_Live))
                    {
                        string strQuery = "Select [No_] from [xyz].[dbo].[ABC$Employee] order by [No_]";
                        SqlDataAdapter sdaEmployee = new SqlDataAdapter(strQuery, con);
                        DataTable dtEmployee = new DataTable();
                        sdaEmployee.Fill(dtEmployee);
                        if (dtEmployee.Rows.Count > 0)
                        {
                            ddl_Employee1.DataSource = dtEmployee;
                            ddl_Employee1.DataTextField = "[No_]";
                            ddl_Employee1.DataValueField = "[No_]";
                            ddl_Employee1.DataBind();
                        }
                    }

    Thanks

    Thursday, October 11, 2018 5:56 PM

All replies

  • User2103319870 posted

    DataBinding: 'System.Data.DataRowView' does not contain a property with the name '[No_]'.

    Try applying alias name to your query and use that value while populating dropdownlist

    using (SqlConnection con = new SqlConnection(CommonFunction.cnn_Live))
                {
                    string strQuery = "Select [No_] as NumberValue from [xyz].[dbo].[ABC$Employee] order by [No_]";
                    SqlDataAdapter sdaEmployee = new SqlDataAdapter(strQuery, con);
                    DataTable dtEmployee = new DataTable();
                    sdaEmployee.Fill(dtEmployee);
                    if (dtEmployee.Rows.Count > 0)
                    {
                        ddl_Employee1.DataSource = dtEmployee;
                        ddl_Employee1.DataTextField = "NumberValue";
                        ddl_Employee1.DataValueField = "NumberValue";
                        ddl_Employee1.DataBind();
                    }
                }

    Thursday, October 11, 2018 6:08 PM
  • User-1038772411 posted

    string connectionString = ""; //Add Connection String 
    using (SqlConnection con = new SqlConnection(connectionString)) //Connection
    {
        string strQuery = "Select [No_] from Xyz order by [No_]"; // Query
        SqlDataAdapter sdaEmployee = new SqlDataAdapter(strQuery, connectionString); //Pass Command to database
       DataTable dtEmployee = new DataTable(); //Datatable
       sdaEmployee.Fill(dtEmployee); //Fill datatable 
      if (dtEmployee.Rows.Count > 0) // Bind Dropdown
       {
           ddl_Employee1.DataSource = dtEmployee;
          ddl_Employee1.DataTextField = "No_";  // do not use the []
          ddl_Employee1.DataValueField = "No_";  // do not use the []
          ddl_Employee1.DataBind();
    }
    }

    Friday, October 12, 2018 6:36 AM