storing problem

  • Thread starter Thread starter Andy Cortvriend
  • Start date Start date
A

Andy Cortvriend

Hi,

I'm developping an asp.net application and i ran to the following problem.
When i press an add button in the aspx page I want to store date in an
datatable. The datatable is created in a seperated class, but every time I
click the button only the last item I added is in that table.
Is this because on the button click a new object of the class is created on
every button click? Is there anyone who could help me to fix the problem?
How can I store the info in the class??

aspx-file

private Tabelletje t = new Tabelletje();

private void Button1_Click(object sender, System.EventArgs e)

{

this.DataGrid1.DataSource =
t.insertResoureInDataTable(Int32.Parse(this.TextBox1.Text),Int32.Parse(this.
TextBox2.Text));

this.DataGrid1.DataBind();

}



class

public class Tabelletje

{

private DataSet ds;

private DataTable table;

private DataColumn col;

private DataRow row;

public Tabelletje()

{

this.createDataTableResources();

}

public void createDataTableResources()

{

ds = new DataSet();

table = new DataTable("test");

col = new DataColumn();

col.DataType = System.Type.GetType("System.Int32");

col.ColumnName = "resID";

table.Columns.Add(col);

col = new DataColumn();

col.DataType = System.Type.GetType("System.Int32");

col.ColumnName = "evrAantal";

table.Columns.Add(col);

ds.Tables.Add(table);

}

public DataSet insertResoureInDataTable(int resID, int aantal)

{

row = table.NewRow();

row["resID"] = resID;

row["evrAantal"] = aantal;

table.Rows.Add(row);

return ds;

}

}


thx
 
Every PostBack means that each and every class in a Page has to be
re-created on the server. Therefore, if you add to a DataTable during one
PostBack, you have to add again with each successive PostBack. Look at some
of the server-side caching alternatives for a solution.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
thx

Kevin Spencer said:
Every PostBack means that each and every class in a Page has to be
re-created on the server. Therefore, if you add to a DataTable during one
PostBack, you have to add again with each successive PostBack. Look at some
of the server-side caching alternatives for a solution.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Andy Cortvriend said:
Hi,

I'm developping an asp.net application and i ran to the following problem.
When i press an add button in the aspx page I want to store date in an
datatable. The datatable is created in a seperated class, but every time I
click the button only the last item I added is in that table.
Is this because on the button click a new object of the class is created on
every button click? Is there anyone who could help me to fix the problem?
How can I store the info in the class??

aspx-file

private Tabelletje t = new Tabelletje();

private void Button1_Click(object sender, System.EventArgs e)

{

this.DataGrid1.DataSource =
t.insertResoureInDataTable(Int32.Parse(this.TextBox1.Text),Int32.Parse(this.
TextBox2.Text));

this.DataGrid1.DataBind();

}



class

public class Tabelletje

{

private DataSet ds;

private DataTable table;

private DataColumn col;

private DataRow row;

public Tabelletje()

{

this.createDataTableResources();

}

public void createDataTableResources()

{

ds = new DataSet();

table = new DataTable("test");

col = new DataColumn();

col.DataType = System.Type.GetType("System.Int32");

col.ColumnName = "resID";

table.Columns.Add(col);

col = new DataColumn();

col.DataType = System.Type.GetType("System.Int32");

col.ColumnName = "evrAantal";

table.Columns.Add(col);

ds.Tables.Add(table);

}

public DataSet insertResoureInDataTable(int resID, int aantal)

{

row = table.NewRow();

row["resID"] = resID;

row["evrAantal"] = aantal;

table.Rows.Add(row);

return ds;

}

}


thx
 
Back
Top