Click here to Skip to main content
11,494,851 members (36,374 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i prepared excel sheet and read that excel data in to the database.

code as follows;

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
 

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection conn = new SqlConnection("Server=(local);Initial Catalog=master;Integrated Security=True  ");
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
 

        if ((txtFilePath.HasFile))
        {
 
            SqlConnection conn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            String query = null;
            string connString = "";
            string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmSS");
         string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
 

         
string strNewPath = Server.MapPath("~UploadedExcel/" + strFileName + strFileType);
 

 
            //Connection String to Excel Workbook

            if (strFileType.Trim() == ".xls")
            {
 
                connString = "Server=(local);Initial Catalog=master;Integrated Security=True" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
 
            }
 
            else if (strFileType.Trim() == ".xlsx")
            {
                connString = "Server=(local);Initial Catalog=master;Integrated Security=True" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
 
            query = "select * from  StaffDetails";
 
            if (conn.State == ConnectionState.Closed) conn.Open();
 
            cmd = new SqlCommand(query, conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);
 
            grvExcelData.DataSource = ds.Tables[0];
            grvExcelData.DataBind();
 
            lblMessage.Text = " data retrieved successfully! TOtal Records:" + ds.Tables[0].Rows.Count;
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Visible = true;
 
            da.Dispose();
            conn.Close();
            conn.Dispose();
        }
 
        else
        {
            lblMessage.Text = "Please select an excel file first";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Visible = true;
        }
    }
}

when i run error shows;

The ConnectionString property has not been initialized.

in the conncetionstring line what is the error.please help line.

i think in the following line error;

connString = "Server=(local);Initial Catalog=master;Integrated Security=True" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

please help me.
Posted 3-Jan-13 6:56am
Edited 3-Jan-13 8:15am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try to use this below connection string.

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 Sergey Alexandrovich Kryukov 350
2 CHill60 318
3 Sascha Lefèvre 280
4 Nakul Chaudhari 236
0 Sergey Alexandrovich Kryukov 10,297
1 OriginalGriff 8,821
2 Sascha Lefèvre 3,864
3 Maciej Los 3,412
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web02 | 2.8.150520.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100