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);
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.