troubleshooting Question

import datatable to sql

Avatar of Seven price
Seven priceFlag for United States of America asked on
.NET Programming
3 Comments1 Solution254 ViewsLast Modified:
I have this datatable display in a gridview. I want to import it into a sql database after.
 protected void btnDetails_Click(object sender, EventArgs e)
        {

            string path = Server.MapPath("~/UploadFiles/" + fupExcel.FileName);
            if (!File.Exists(path.ToString().ToUpper()))
            {
                fupExcel.SaveAs(path);
            }

             OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
              path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
            conn.Open();
           
            OleDbCommand cmd = new OleDbCommand(); ;
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            DataSet ds = new DataSet();

            // selecting distict list of EmpNo 
                       cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM [1108$]";
            //[Sheet1$] is Excel sheet name in your file
            oleda = new OleDbDataAdapter(cmd);
            oleda.Fill(ds, "dsSlno");
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                
                grvData.DataSource = ds.Tables[0].DefaultView;
                grvData.DataBind();
                          
            }
            else
            {
                grvData.DataSource = null;
                grvData.DataBind();
            }

        }
    }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros