MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

I lose data, when I try to save a loaded DataTable on the cl

 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp
Author Message
chike_oji



Joined: 13 Dec 2007
Posts: 10

PostPosted: Thu Dec 13, 2007 11:06 am    Post subject: I lose data, when I try to save a loaded DataTable on the cl Reply with quote

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", "");

Message.Text = String.Format("The following error
occured:{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.");

GridView1.Enabled = false;
//UploadExcel.Enabled = true;

//}//End of IsPostBack block
}
}

Archived from group: microsoft>public>dotnet>languages>csharp
Back to top
View user's profile Send private message
Kevin.Li



Joined: 21 Nov 2007
Posts: 3

PostPosted: Thu Dec 13, 2007 12:00 pm    Post subject: Re: I lose data, when I try to save a loaded DataTable on th Reply with quote

hey, brother.

The reason you lost your datatable is that:

When you click another button on the page, you will generate *new*
request to the server, which means the code-behind will be re-
executed. therefore :
public partial class _Default : System.Web.UI.Page
{
int test = 0; // Set a break-point here, you
find out everything.
DataTable dtData = null; // When click the btnSave button,
dtData will be assigned to null.
...
...
}

//

Use session to store the dataset is a possible solution, however,
which I know is certainly not effient.

Does anyone have better solution.
Back to top
View user's profile Send private message
Nicholas Paldino [.NET/C#



Joined: 08 Aug 2007
Posts: 71

PostPosted: Thu Dec 13, 2007 3:01 pm    Post subject: Re: I lose data, when I try to save a loaded DataTable on th Reply with quote

The reason for this is that your instance is not maintained between
calls. You are better off getting the data source from the GridView1
(assuming that you are using viewstate, but that will be VERY expensive in
terms of how much data you are moving across the wire) and then working with
that on the save.

Or, you could store the data table in the session, and retrieve it when
saving.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

wrote in message @i29g2000prf.googlegroups.com...
> 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", "");
>
> Message.Text = String.Format("The following error
> occured:{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.");
>
> GridView1.Enabled = false;
> //UploadExcel.Enabled = true;
>
> //}//End of IsPostBack block
> }
> }
Back to top
View user's profile Send private message
chike_oji



Joined: 13 Dec 2007
Posts: 10

PostPosted: Wed Jan 23, 2008 12:14 pm    Post subject: Re: I lose data, when I try to save a loaded DataTable on th Reply with quote

Thank you everybody for your responses. I was able to progress with
the advice
I got from you all.

Regards
chike.
On Dec 13 2007, 4:01 pm, "Nicholas Paldino [.NET/C# MVP]"
wrote:
>     The reason for this is that your instance is not maintained between
> calls.  You are better off getting the data source from the GridView1
> (assuming that you are using viewstate, but that will be VERY expensive in
> terms of how much data you are moving across the wire) and then working with
> that on the save.
>
>     Or, you could store the data table in the session, and retrieve it when
> saving.
>
> --
>           - Nicholas Paldino [.NET/C# MVP]
>           - m...@spam.guard.caspershouse.com
>
> wrote in message
>
> @i29g2000prf.googlegroups.com...
>
>
>
> > 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", "");
>
> >                Message.Text = String.Format("The following error
> > occured:{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.");
>
> >            GridView1.Enabled = false;
> >            //UploadExcel.Enabled = true;
>
> >        //}//End of IsPostBack block
> >    }
> > }- Hide quoted text -
>
> - Show quoted text -

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
Has anyone seen a delegate lose track of its target? I have written code the subscibe to a delegate that is called at a specific interval. Initially, everything is fine, and the delegate works. However, after some (seemingly random) amount of time, the delegate appears to get disconnected. I have trace st

Which .NET Framework gets loaded? If I have two applications, one compiled using VS.NET 2002 Framework 1.0 and the other VS.NET 2003 Framework 1.1, then which framework get loaded when I run them on an enviroment that has both frameworks installed? Does this differ is one is hosting the o

The IDE Closes as soon as the Projects are loaded. Hi, This is a strange problem which is not giving any chance to trouble shoot. The solution closes and I get a alert: Microsft visual studio needs to close as it encountered an error. I am getting this this every time i try to open the solution for coding

ERROR [IM003] Specified driver could not be loaded due to sy Hi , I use ODBC DSN to connect Oracle. string newConnString m_connection = new when I setup the DSN in windows ODBC Datasource manager, the test connection

ERROR [IM003] Specified driver could not be loaded Hi All, I am new to this News Group and this is my first message. Problem Description ============= I am using ASP.NET and Softvelocity Topspeed Database I can connect to the database through my ASP.NET application. When I make some Chang
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group