C
chike_oji
Please can someone help me. I am writing a web application,
that allows for the upload of an excel sheet into the database.
I have an upload button and a save button.
The upload button allows for the retrieval of the excel data into
a DataTable, which is bound to a GridView for previewing before
saving to the Database.
But, whenever I click on the save button, I lose the DataTable's data,
so I lose the data I want to save to the database.
I am using .net framework 2.0, vs2005 professional, win xp
professional
and below is the codebehind file for the page.
Thanks in advance.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 IConceptDBWebUpdate.BLL;
public partial class _Default : System.Web.UI.Page
{
DataTable dtData = null;
public string UploadID
{
get
{
return (ViewState["UploadID"] != null) ?
ViewState["UploadID"].ToString() : string.Empty;
}
set
{
ViewState["UploadID"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//GridView1.DataSource =
IConceptDBWebUpdate.BLL.StockDetail.GetAllStockDetails();
//GridView1.DataBind();
}
protected void Upload_Click(object sender, EventArgs e)
{
//DataTable dtData = null;
if (IsValid)
{
UploadID = Guid.NewGuid().ToString();
string path = string.Format("{0}\\{1}.xls",
ConfigurationManager.AppSettings["ExcelFilePath"], UploadID);
try
{
if (UploadExcel.PostedFile == null)
{
throw new Exception("Please select a File to
upload");
}
UploadExcel.PostedFile.SaveAs(path);
//Create query to be passed to the Excel conversion
method.
string sql = "Select * from [stockdetails$]";
//Store data from Excel file in a DataTable
dtData =
MicrosoftExcelClient.ConvertExcelToDataTable(path, sql);
//Bind the GridView to the DataTable to display the
data
GridView1.DataSource = dtData;
GridView1.DataBind();
//TODO: Delete all data in the current stockdetails
table
Upload.Visible = false;
btnSave.Visible = true;
//UploadExcel.Enabled = false;
}
catch (Exception ex)
{
Message.CssClass = "ErrorMessage";
string error = ex.Message.Replace("\n", "<br>");
Message.Text = String.Format("The following error
occured:<BR>{0}", error);
}
finally
{
System.IO.File.Delete(path);
}
}//End of IsValid check
}
protected void Cancel_Click(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
//if (!Page.IsPostBack)
//{
//Loop through the DataTable and add the rows to the
Database table stockdetails.
foreach (DataRow row in dtData.Rows)
{
string company = row["company"].ToString();
string keyword = row["keyword"].ToString();
string stockprice = row["stockprice"].ToString();
string stockworth = row["stockworth"].ToString();
StockDetail.InsertStockDetails(company, keyword,
stockprice, stockworth);
}
//Database update complete.
Message.Text = String.Format("The Database has now been
updated.<BR>");
GridView1.Enabled = false;
//UploadExcel.Enabled = true;
//}//End of IsPostBack block
}
}
that allows for the upload of an excel sheet into the database.
I have an upload button and a save button.
The upload button allows for the retrieval of the excel data into
a DataTable, which is bound to a GridView for previewing before
saving to the Database.
But, whenever I click on the save button, I lose the DataTable's data,
so I lose the data I want to save to the database.
I am using .net framework 2.0, vs2005 professional, win xp
professional
and below is the codebehind file for the page.
Thanks in advance.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 IConceptDBWebUpdate.BLL;
public partial class _Default : System.Web.UI.Page
{
DataTable dtData = null;
public string UploadID
{
get
{
return (ViewState["UploadID"] != null) ?
ViewState["UploadID"].ToString() : string.Empty;
}
set
{
ViewState["UploadID"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//GridView1.DataSource =
IConceptDBWebUpdate.BLL.StockDetail.GetAllStockDetails();
//GridView1.DataBind();
}
protected void Upload_Click(object sender, EventArgs e)
{
//DataTable dtData = null;
if (IsValid)
{
UploadID = Guid.NewGuid().ToString();
string path = string.Format("{0}\\{1}.xls",
ConfigurationManager.AppSettings["ExcelFilePath"], UploadID);
try
{
if (UploadExcel.PostedFile == null)
{
throw new Exception("Please select a File to
upload");
}
UploadExcel.PostedFile.SaveAs(path);
//Create query to be passed to the Excel conversion
method.
string sql = "Select * from [stockdetails$]";
//Store data from Excel file in a DataTable
dtData =
MicrosoftExcelClient.ConvertExcelToDataTable(path, sql);
//Bind the GridView to the DataTable to display the
data
GridView1.DataSource = dtData;
GridView1.DataBind();
//TODO: Delete all data in the current stockdetails
table
Upload.Visible = false;
btnSave.Visible = true;
//UploadExcel.Enabled = false;
}
catch (Exception ex)
{
Message.CssClass = "ErrorMessage";
string error = ex.Message.Replace("\n", "<br>");
Message.Text = String.Format("The following error
occured:<BR>{0}", error);
}
finally
{
System.IO.File.Delete(path);
}
}//End of IsValid check
}
protected void Cancel_Click(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
//if (!Page.IsPostBack)
//{
//Loop through the DataTable and add the rows to the
Database table stockdetails.
foreach (DataRow row in dtData.Rows)
{
string company = row["company"].ToString();
string keyword = row["keyword"].ToString();
string stockprice = row["stockprice"].ToString();
string stockworth = row["stockworth"].ToString();
StockDetail.InsertStockDetails(company, keyword,
stockprice, stockworth);
}
//Database update complete.
Message.Text = String.Format("The Database has now been
updated.<BR>");
GridView1.Enabled = false;
//UploadExcel.Enabled = true;
//}//End of IsPostBack block
}
}