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 

Why my DataTables in DataSet are cleared?

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



Joined: 21 Nov 2007
Posts: 3

PostPosted: Thu Dec 13, 2007 11:40 am    Post subject: Why my DataTables in DataSet are cleared? Reply with quote

I am using ASP.NET 2.0.

My DataTable in DataSet are cleared automatically!

The follow is my code:
//--------------- I just keep what core part related to the
problem-----------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.UI;

public partial class _Default : System.Web.UI.Page
{
DataTable ds = new DataSet();
string cs = "Data Source=.;Initial Catalog=Northwind;Integrated
Security=True";

protected void Button1_Click(object sender, EventArgs e)
{
string ss = "select
CompanyName,ContactName,ContactTitle,Country,HomePage,Fax,Phone from
Suppliers where "+ selColumn + " = '" + selValue+" ' ";
SqlDataAdapter sda = new SqlDataAdapter(ss,cs);
sda.Fill(ds,"suppliers"); // Everything works well here,
int test = ds.Tables.Count // test will be assigned to
1.
}
protected void btnFirst_Click(object sender, EventArgs e)
{

int test = ds.Tables.Count; // test is 0! Where did my
"suppliers" table gone?
if (ds.Tables["suppliers"].Rows.Count > 0) // Hence, exception:
NullReferenceException.
{
// to do something;
}
}

I still have reference in dataset to the datatable, how can the
datatable be released?

Please help me.. Thanks !

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



Joined: 11 Aug 2007
Posts: 2

PostPosted: Thu Dec 13, 2007 11:47 am    Post subject: Re: Why my DataTables in DataSet are cleared? Reply with quote

Are you clicking btnFirst without clicking on Button1 by mistake?

Kalpesh
Back to top
View user's profile Send private message
Kevin.Li



Joined: 21 Nov 2007
Posts: 3

PostPosted: Thu Dec 13, 2007 12:08 pm    Post subject: Re: Why my DataTables in DataSet are cleared? Reply with quote

On 12月13日, 下午10时47分, Kalpesh wrote:
> Are you clicking btnFirst without clicking on Button1 by mistake?
>
> Kalpesh

Kalpesh,Thanks for your quick reply.

I was not clicking btnFirst without clicking on Button1 by mistake. I
had tested several times before asking for help.

I understand the reason now.

That was because:

When I click another button on the page, I 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 ds = new DataSet(); // When click the btnSave button,
dtData will be assigned to null.
...
...

}

Kalpesh , thanks again.
Back to top
View user's profile Send private message
Kalpesh



Joined: 11 Aug 2007
Posts: 2

PostPosted: Thu Dec 13, 2007 12:42 pm    Post subject: Re: Why my DataTables in DataSet are cleared? Reply with quote

My bad. For the moment, I missed the point that it is asp.net Smile

Kalpesh
Back to top
View user's profile Send private message
Peter Bromberg [C# MVP]



Joined: 31 Oct 2007
Posts: 22

PostPosted: Thu Dec 13, 2007 3:26 pm    Post subject: RE: Why my DataTables in DataSet are cleared? Reply with quote

In addition to your announced discovery vis-a-vis ASP.NET, consider fixing up
this line:

DataTable ds = new DataSet();

You are typing "ds" as a DataTable and then you proceed to instantiate it as
a DataSet? That won't work at all.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com


"Kevin.Li" wrote:

> I am using ASP.NET 2.0.
>
> My DataTable in DataSet are cleared automatically!
>
> The follow is my code:
> //--------------- I just keep what core part related to the
> problem-----------------------------------------
> using System;
> using System.Data;
> using System.Data.SqlClient;
> using System.Configuration;
> using System.Web;
> using System.Web.UI;
>
> public partial class _Default : System.Web.UI.Page
> {
> DataTable ds = new DataSet();
> string cs = "Data Source=.;Initial Catalog=Northwind;Integrated
> Security=True";
>
> protected void Button1_Click(object sender, EventArgs e)
> {
> string ss = "select
> CompanyName,ContactName,ContactTitle,Country,HomePage,Fax,Phone from
> Suppliers where "+ selColumn + " = '" + selValue+" ' ";
> SqlDataAdapter sda = new SqlDataAdapter(ss,cs);
> sda.Fill(ds,"suppliers"); // Everything works well here,
> int test = ds.Tables.Count // test will be assigned to
> 1.
> }
> protected void btnFirst_Click(object sender, EventArgs e)
> {
>
> int test = ds.Tables.Count; // test is 0! Where did my
> "suppliers" table gone?
> if (ds.Tables["suppliers"].Rows.Count > 0) // Hence, exception:
> NullReferenceException.
> {
> // to do something;
> }
> }
>
> I still have reference in dataset to the datatable, how can the
> datatable be released?
>
> Please help me.. Thanks !
>
>

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
Datatables and XML I have been told that a Datatable hold its data in XML format. If that is true, then using datatable would be extremely resource intensive, wouldn't it. Can everyone confirm that? -- cody [Freeware, Games and Humor] ||

Problem display several DataTables with relations Hello, I have a dataset with 2 DataTables : Groups and Persons. I make a relation between this 2 DataTables using one DataColumn in each datatable. Finally I use this dataset in a dataGrid. So I would like to have a display like : groupe1 groupe2 + (perso

dataset Good day. We have a trouble with a datatable field in a dataset (FH.GTVTC). It has a sum expression referencing a child datatable field (BH.SVTC). This Field is also a sum expression from another child datatable field (ED.VT). Problem is than dt1.f1 value

DataSet and WSE 2.0TP Does anybody built Web Methods with DataSet as a parameter using WSE 2.0? VS generate Proxy-class with type of parameter if it has DataSet type in original. What you can tell about it?

dataset Issues This is all very confusing, but I'm trying. I am using strongly typed datasets, that I simply drag on to the form. I have a dataset created here: Its there, because I have a datagridview that uses it. No
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