AL MUBARAK

AL MUBARAK

  • NA
  • 96
  • 68.7k

importing excel sheet. Not working in remote machine, and its working fine in local machine.

May 13 2012 6:13 AM
Dear all,

   I'm working as developer in reputed concern. and I'm working on SMS application project. and had one of functionality with uploading Excel sheet with mobile number. In a contact group. when the admin person, import numbers from excel sheet to application. it reads and and stored the numbers into database. So, they can send SMS through the module vice versa.

  I'm creating some code for importing excel sheet. its working fine in local machine. but before 10 days ago, i just upload in client remote server. it requires some local and remote connections error. I just configured SQL SERVER on client's remote server. after i tried to import, the run time error shows like (See the attachment - run time-error).

   please go through my codings also, and please clarify me to resolved the issue. and please feel free to guide me, if anything need to add more or i handled improper code.

My Code : clicking the upload button, the below function will call to import excel sheet.

void UploadXL()
        {
            DataSet ds = new DataSet();
            DataTable dt=new DataTable();

          
            if (xlFileUpload.PostedFile != null)
                lblErrMessage.Text = "Importing ...";
                if (xlFileUpload.PostedFile.FileName != "")
                {
                  
                    string sFilePath = Server.MapPath("../ExcelFiles");
                    string[] sFileName = xlFileUpload.PostedFile.FileName.Split('\\');
                    xlFileUpload.SaveAs(sFilePath + "\\" + sFileName[sFileName.Length - 1]);
                    //Response.Write("P:" + sFilePath + "\\" + xlFileUpload.PostedFile.FileName);
                    uploadFromXL(sFilePath + "\\" + sFileName[sFileName.Length - 1], out dt);                   
                    if (xlFileUpload.PostedFile.FileName != "")
                    {
                       DataTable DD = new DataTable();
                      
                        if (dt.Rows.Count > 0)
                        {
                            DataColumn dc = new DataColumn("RowID", typeof(Int32));

                            dt.Columns.Add(dc);
                            int id = 1;
                            //DataRow drw;
                            List<string> mobile = (from q in dt.AsEnumerable()
                                                   select q.Field<string>("mobile")).Distinct().ToList();
                            for (int i = 0; i < mobile.Count; i++)
                                if (mobile[i] != null)
                                    mobile[i] = mobile[i].TrimStart('0');
                            int count = mobile.Count();
                            while (count >= 1)
                            {
                                dt.Rows[id - 1]["RowID"] = id;
                                id = id + 1;
                                count = count - 1;
                            }

                            int TotTables = Convert.ToInt32(dt.Rows.Count / 1000);
                            int reminning = dt.Rows.Count % 1000;
                            if (reminning > 0)
                                TotTables = TotTables + 1;


                            //List<string> contacts = (from p in dt.AsEnumerable()
                            //                         select p.Field<int>("RowID")).Distinct().ToList();

                            int a = 1000;
                            int b = 0;
                            //DataTable de;
                            while (TotTables > 0)
                            {
                                //de = new DataTable();
                                //DataRow[] dw;
                                //if (b == 0)
                                //{
                                //    dw = dt.Select("RowID>" + a, "RowID ASC");
                                //    de.Rows.Add(dw);
                                //    ds.Tables.Add(de);
                                //}
                                //else
                                //{
                                //    dw = dt.Select("RowID>" + a + " and RowID<" + b, "RowID ASC");
                                //    de.Rows.Add(dw);
                                //    ds.Tables.Add(de);
                                //}
                                if (b == 0)
                                {
                                    ds.Tables.Add((from d in dt.AsEnumerable()
                                                   where d.Field<int>("RowID") <= a
                                                   select d).CopyToDataTable());
                                }
                                else
                                {
                                    ds.Tables.Add((from d in dt.AsEnumerable()
                                                   where d.Field<int>("RowID") <= a && d.Field<int>("RowID") > b
                                                   select d).CopyToDataTable());
                                }
                                b = a;
                                a = a + 1000;
                                TotTables = TotTables - 1;

                            }
                            for (int i = 0; ds.Tables.Count > i; i++)
                            {
                                XLUpload(ds.Tables[i]);
                                ds.Tables[i].TableName = i.ToString();
                            }

                        }
                    }
                        lblErrMessage.Text = "Data Imported Successfully.";
                    }
              
          
              
        }

Answers (1)