从sqlreader加载数据表时出现映射错误

时间:2016-05-01 01:16:58

标签: c# sql-server

从sqlreader加载数据表时出错,在下面一行

empPrivDataTable.Load(sqlReader);

以下是完整的方法:

 private void LoadEmployeePrivilegeGrid()
    {
        using (SqlConnection sqlConnection = new SqlConnection(connectionString))
        {
            SqlCommand sqlCmd = new SqlCommand("SELECT Id as Id, (select Name from Room r where r.id = ap.RoomId) as Room, cast(ap.DateFrom as date) as Date,  CONVERT(varchar(15), cast(ap.DateFrom as time), 100) as TimeFrom,  CONVERT(varchar(15), cast(ap.DateTo as time), 100) as TimeTo FROM Access_Privilege ap where EmployeeId = @EmployeeId", sqlConnection);
            //SqlCommand sqlCmd = new SqlCommand("SELECT Id as Id from Access_Privilege", sqlConnection);
            sqlCmd.Parameters.AddWithValue("@EmployeeID", empNmCmbBx.SelectedValue.ToString());
            sqlConnection.Open();
            SqlDataReader sqlReader = sqlCmd.ExecuteReader();
            if (sqlReader.HasRows)
            {
                currPrvDtGrdView.Visible = true;
                empPrivDataTable.Load(sqlReader);
                currPrvDtGrdView.AutoGenerateColumns = false;
                currPrvDtGrdView.Columns[0].DataPropertyName = "Id";
                currPrvDtGrdView.Columns[1].DataPropertyName = "Room";
                currPrvDtGrdView.Columns[2].DataPropertyName = "Date";
                currPrvDtGrdView.Columns[3].DataPropertyName = "TimeFrom";
                currPrvDtGrdView.Columns[4].DataPropertyName = "TimeTo";
                currPrvDtGrdView.Columns[0].Visible = false;
                currPrvDtGrdView.DataSource = empPrivDataTable;
            }
            sqlReader.Close();
        }
}

这是错误:

    Object reference not set to an instance of an object.
   at SGFinalProjectRoomAllocationSystem.ManageAccessForm.LoadEmployeePrivilegeGrid() in c:\Projects\SGFinalProjectRoomAllocationSystem\SGFinalProjectRoomAllocationSystem\ManageAccessForm.cs:line 110 at SGFinalProjectRoomAllocationSystem.ManageAccessForm.empNmCmbBx_SelectedIndexChanged(Object sender, EventArgs e) in c:\Projects\SGFinalProjectRoomAllocationSystem\SGFinalProjectRoomAllocationSystem\ManageAccessForm.cs:line 221
       at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
       at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
       at System.Windows.Forms.ComboBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)   

0 个答案:

没有答案