Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

Am trying to load gridView according to selected string in my dropdownlist.Check the code below. But weird thing, it gives me an error that says:
MSIL
A field or property with the name 'name' was not found on the selected data source.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: A field or property with the name 'name' was not found on the selected data source.

Source Error:


Line 121:            grdAvailableStuff.DataSource = dt;
Line 122:
Line 123:            grdAvailableStuff.DataBind();
Line 124:        }
Line 125:



And I dont have that name in my data source and now am lost, wher should i look for this?



Below is the Stored procedure:

SQL
ALTER PROCEDURE [dbo].[procGetSelectedStaffMemberAvailableTimeSlot]
@Date nVarChar(20),@EmpRecNum nVarChar(50)
AS

SELECT   Employee.EmpName+' '+Employee.EmpSurname AS FullName,  AppointmentSlots.AvailabilityTimes, Employee.EmpRecNumber
FROM         AppointmentSlots INNER JOIN
                      Employee ON AppointmentSlots.EmpRecNumber = Employee.EmpRecNumber
WHERE     Employee.EmpRecNumber=@EmpRecNum AND AppointmentSlots.Date=@Date

================================================================

Code from My Class
================================================================
public DataTable grdLoadSpecificEmployee(string date, string id)
        {
            using (SqlConnection con = new SqlConnection(ConnString))
            {
                SqlCommand cmd = new SqlCommand("procGetSelectedStaffMemberAvailableTimeSlot", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.NVarChar));
                cmd.Parameters["@Date"].Value = date;
                cmd.Parameters.Add(new SqlParameter("@EmpRecNum", SqlDbType.NVarChar));
                cmd.Parameters["@EmpRecNum"].Value = id;
                DataTable dTable = new DataTable("dTable");
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dTable);
                return dTable;

            }

        }

===============================================================


Code behind the dropdownlist
================================================================
C#
protected void ddlStaff_SelectedIndexChanged(object sender, EventArgs e)
  {
      string var = ddlStaff.SelectedValue.ToString();
      if (ddlStaff.SelectedValue == var)
      {
          MessageBox.Show(var.ToString());

          DataTable dt = new DataTable();
          systemBusinessLayer = new BusinessLayer();

          dt = systemBusinessLayer.grdLoadSpecificEmployee(DatePicker.SelectedDate.ToShortDateString(), var);
          grdAvailableStuff.DataSource = dt;

          grdAvailableStuff.DataBind();
      }




  }

===============================================================


Below is the code for loading the dropdownlis

Code from Businesslayer
===============================================================
public ArrayList ddlAvailableStaffPerDay(string date)
       {
           using (SqlConnection con = new SqlConnection(ConnString))
           {
               SqlCommand cmd = new SqlCommand("procGetEmployeesForSpecificDay", con);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.NVarChar));
               cmd.Parameters["@Date"].Value = date;
               ArrayList myList = new ArrayList();
               try
               {
                   con.Open();
                   SqlDataReader read = cmd.ExecuteReader();
                   while (read.Read())
                   {
                       ClsEmployees employees = new ClsEmployees(Convert.ToInt32(read["EmpRecNumber"]), Convert.ToString(read["EmpName"]), Convert.ToString(read["EmpName"]));
                       myList.Add(employees);
                   }
                   read.Close();
               }
               catch (Exception exp)
               {
                   throw new ApplicationException(exp.Message);
               }
               return myList;
           }
       }

============================================================

Below is how i load the dropdown
============================================================
public void populateDDLStaff()
    {
        ArrayList staffList = new ArrayList();
        systemBusinessLayer = new BusinessLayer();
        staffList = systemBusinessLayer.ddlAvailableStaffPerDay(DatePicker.SelectedDate.ToShortDateString());
     
        ddlStaff.DataSource = staffList;
        ddlStaff.DataTextField = "EmployeeName1";
        ddlStaff.DataValueField = "EmpRecNumber1";
        ddlStaff.DataBind();   
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        
        populateDDLStaff();
       
        DataTable dt = new DataTable();
        systemBusinessLayer = new BusinessLayer();
        dt = systemBusinessLayer.grid(DatePicker.SelectedDate.ToShortDateString());
        grdAvailableStuff.DataSource = dt;
        grdAvailableStuff.DataBind();   
    }


Hope this is enough information
Posted
Updated 17-Jun-11 8:54am
v2

1 solution

It's seems, Datasource column name and grid view column name is being mismatch as per you Exception message. So make sure your data source field name and Gridview field name should be match.

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900