Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i prepared excel sheet and read that excel data in to the database.

code as follows;

C#
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;

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


please help me.
Posted
Updated 3-Jan-13 7:15am
v2

http://connectionstrings.com/excel[^] might help you out.
 
Share this answer
 
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";
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900