Search code examples
asp.netexceptionfile-uploadargumentnullexception

ArgumentNull exception in asp.net web application


I have developed an asp.net application in which input will be given through an excel sheet. This application is working fine in a system with WINDOWS XP and MS office 2008. If i try to run the same application in a system with WINDOWS 7 and MS office 2010 i am getting a Argument Null Exception.

Code:

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);
            }

I am getting error in the line:

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

Code:

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;
        }

Please help me in resolving this issue. Thanks, Raji


Solution

  • From your code it appears that the table can have two possible names.

    Either give it always the same name that you're using later when reading:

    dAdapter.Fill(dsData, "LHS");
    

    Or you can take the table by index ignoring its name altogether:

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