How can we upload excelsheet into sqlserver 2008 using asp.net.
"just click on uploading ,we see the data using grid view at a time it save on database.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Joginder BangerPosted Nov 25, 2014, 7:59 AM
try this code,
string path = string.Concat((Server.MapPath("~/admin/Import/" + FileUpload.FileName)));
FileUpload.PostedFile.SaveAs(path);
string FileExt = System.IO.Path.GetExtension(FileUpload.FileName);
OleDbConnection OleDbcon = new OleDbConnection();
OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
OleDbcon.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
conn.Open();
DbDataReader dr = cmd.ExecuteReader();
dt.Load(dr);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
//do stuff here like upload data one by one
}
//call here your bind gridview function
}