vivek shakya

vivek shakya

  • NA
  • 1
  • 2k

When import exel file on the server getting error,,,,

Dec 4 2014 2:03 AM
i am currenlty building website in asp.net2.0
in which i emport excel file into gridview
when iam running this code in localhost iam able to emport excel data to Gridview .
 but when iam runnign the same in my online server it is not getting emport instead its giving an error
'object reference not set to an instance of an object'
i am not able to find it out the errror.
i also tried with dubugging but no such error is occuring but why its this error while in online.
the below is my code where emport excel file into Gridview.

plz Write aproprite code.

  public void BindGridView()
    {
       // string FilePath = ResolveUrl("./Upload/"); // Give Upload File Path
        string FilePath = Server.MapPath("./Upload/");
        string filename = string.Empty;
        if (FileUpload1.HasFile) // Check FileControl has any file or not
        {
            try
            {
                string[] allowdFile = { ".xls", ".xlsx" };
                string FileExt = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower();// get extensions
                bool isValidFile = allowdFile.Contains(FileExt);

                // check if file is valid or not
                if (!isValidFile)
                {
                    lblMsg.Visible = true;
                    lblMsg.Style.Add("color", "red");
                    lblMsg.Text = "Please upload only Excel";
                }
                else
                {
                    int FileSize = FileUpload1.PostedFile.ContentLength; // get filesize
                    if (FileSize <= 1048576) //1048576 byte = 1MB
                    {
                        //filename = Path.GetFileName(Server.MapPath(FileUpload1.FileName));// get file name
                        //FileUpload1.SaveAs(Server.MapPath(FilePath) + filename); // save file to uploads folder
                        //string filePath = Server.MapPath(FilePath) + filename;

                        filename = Path.GetFileName(Server.MapPath(FileUpload1.FileName));// get file name
                        FileUpload1.SaveAs(FilePath + filename);// save file to uploads folder

                     
                        string filePath = FilePath + filename;

                        string conStr = "";
                        if (FileExt == ".xls") // check for excel file type
                        {
                            conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                        }
                        else if (FileExt == ".xlsx")
                        {
                            conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                        }

                        conStr = String.Format(conStr, filePath, "Yes");
                        OleDbConnection con = new OleDbConnection(conStr);
                        OleDbCommand ExcelCommand = new OleDbCommand();
                        ExcelCommand.Connection = con;
                        con.Open();
                        DataTable ExcelDataSet = new DataTable();
                        ExcelDataSet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        DataTable dt = new DataTable();
                        if (ExcelDataSet != null && ExcelDataSet.Rows.Count > 0)
                        {
                            string SheetName = ExcelDataSet.Rows[0]["TABLE_NAME"].ToString(); // get sheetname
                            ExcelCommand.CommandText = "SELECT * From [" + SheetName + "]";
                            OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
                            ExcelAdapter.SelectCommand = ExcelCommand;
                            ExcelAdapter.Fill(dt);
                        }
                        Tr_Heading.Attributes.Add("style", "display:none");
                        con.Close();
                        if (dt != null && dt.Rows.Count > 0) // Check if File is Blank or not
                        {
                            gvSubscriberProject.DataSource = dt;
                            gvSubscriberProject.DataBind();
                            lblMsg.Visible = false;
                        }
                        else
                        {

                            lblMsg.Visible = true;
                            lblMsg.Style.Add("color", "red");
                            lblMsg.Text = "There are No Rows in this File!!!";
                        }
                        FilePath = ResolveUrl("~/Upload/");
                        string fileName = Server.MapPath(FilePath) + filename;
                        FileInfo f = new FileInfo(fileName);
                        if (f.Exists)
                        {
                            f.IsReadOnly = false;
                            f.Delete();
                        }
                    }
                    else
                    {
                        lblMsg.Visible = true;
                        lblMsg.Style.Add("color", "red");
                        lblMsg.Text = "Attachment file size should not be greater then 1 MB!";
                    }
                }
            }
            catch (Exception ex)
            {
                lblMsg.Visible = true;
                lblMsg.Style.Add("color", "red");
                lblMsg.Text = "Error occurred while uploading a file: " + ex.Message;
            }
        }
        else
        {
            lblMsg.Visible = true;
            lblMsg.Style.Add("color", "red");
            lblMsg.Text = "Please select a file to upload.";
        }
    }

Answers (2)