-1

我开发了一个 asp.net 应用程序,其中输入将通过 Excel 表给出。此应用程序在具有 WINDOWS XP 和 MS Office 2008 的系统中运行良好。如果我尝试在具有 WINDOWS 7 和 MS Office 2010 的系统中运行相同的应用程序,我将收到参数空异常。

代码:

foreach (var dr in data)
            {
                LHSupdate = new LHSUpdate();
                if (!string.IsNullOrEmpty(Convert.ToString(dr["Associate Id"])))
                {
                    AssociateID = Convert.ToString(dr["Associate Id"]);
                }
                LHSupdate.AssciateID = AssociateID;
                if (!string.IsNullOrEmpty(Convert.ToString(dr["Associate Name"])))
                {
                    AssociateName = Convert.ToString(dr["Associate Name"]);
                }
                LHSupdate.Name = AssociateName;

                var designation = dsData.Tables["LHS"].AsEnumerable().Where(r => Convert.ToString(r["Associate Id"]).Trim() == LHSupdate.AssciateID.Trim());

                if (designation != null)
                {
                    foreach (var de in designation)
                    {
                        LHSupdate.Designation = Convert.ToString(de["Level"]);
                    }
                }

                else
                {
                    LHSupdate.Designation = "";
                }                
                LHSupdate.CourseName = Convert.ToString(dr["Trainings "]);
                LHSupdate.CourseStatus = Convert.ToString(dr["Training Status"]);
                LHSupdate.Score = Convert.ToString(dr["Credits"]);
                LHSupdate.LearningMode = Convert.ToString(dr["Venue"]);
                LHSupdate.StartDate = Convert.ToString(dr["Start Date"]);
                LHSupdate.EndDate = Convert.ToString(dr["End Date"]);                
                lstLHS.Add(LHSupdate);
            }

我在行中遇到错误:

var designation = dsData.Tables["LHS"].AsEnumerable().Where(r => Convert.ToString(r["Associate Id"]).Trim() == LHSupdate.AssciateID.Trim());

代码:

private DataSet Getdata()
        {
            string connectionString = "";
            string getExcelSheetName = string.Empty;
            if (fuLHSEntry.HasFile)
            {
                string fileName = Path.GetFileName(fuLHSEntry.PostedFile.FileName);
                string fileExtension = Path.GetExtension(fuLHSEntry.PostedFile.FileName);
                string fileLocation = Server.MapPath("~/App_Data/" + fileName);
                fuLHSEntry.SaveAs(fileLocation);

                if (fileExtension == ".xls")
                {
                    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + @";Extended Properties=" + Convert.ToChar(34).ToString() + @"Excel 8.0;Imex=1;HDR=Yes;" + Convert.ToChar(34).ToString();
                }
                else if (fileExtension == ".xlsx" || fileExtension == ".xlsm")
                {
                    connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + @";Extended Properties=" + Convert.ToChar(34).ToString() + @"Excel 12.0;IMEX=2;HDR=Yes;" + Convert.ToChar(34).ToString();
                }

                OleDbConnection con = new OleDbConnection(connectionString);
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection = con;
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
                DataTable dtExcelRecords = new DataTable();
                con.Open();
                DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                int count = 0;
                foreach (DataRow dr in dtExcelSheetName.Rows)
                {
                    getExcelSheetName = GetSheetName(dr);
                    if (!string.IsNullOrEmpty(getExcelSheetName))
                    {                        
                        cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
                        dAdapter.SelectCommand = cmd;
                        if (getExcelSheetName.ToUpper().Contains("LEARNING"))
                        {
                            getExcelSheetName = "LEARNING";
                        }
                        else
                        {
                            getExcelSheetName = "LHS";
                        }
                        dAdapter.Fill(dsData, getExcelSheetName);
                        count++;
                        if (count == 2)
                        {
                            break;
                        }
                    }
                }
                con.Close();
            }
            return dsData;
        }

请帮我解决这个问题。谢谢,拉吉

4

1 回答 1

0

从您的代码看来,该表可以有两个可能的名称。

要么给它起一个你以后在阅读时使用的名字:

dAdapter.Fill(dsData, "LHS");

或者您可以通过索引完全忽略其名称来获取表格:

var designation = dsData.Tables[0].AsEnumerable().Where(r => Convert.ToString(r["Associate Id"]).Trim() == LHSupdate.AssciateID.Trim());
于 2013-02-19T08:55:47.797 回答