Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Table Problem 1

Status
Not open for further replies.

davecapone

Programmer
Oct 3, 2000
48
US
Hi,

I have an asp.net page that has an asp table on it. what I am looking to do, is add rows to that table when someone fills in information in some text boxes and clicks a button. Everything works perfectly, button clicked, new row added.

the problem is, when you go to add another row, it replaces the first row. And if a postback event occurs from an operation other than what cause a row add, the added row disappears.

Anyone know what would cause this or if a certain method needs to be called to make the row addition permanent?
 
Also, I'm wondering if a datagrid can be used for this somehow.

The problem is the Data doesn't come from a Database or other source the data is dynamically added with each entry into the form and click of the button
 
Add your data row to your datatable, then assign it to a session variable. This will allow it to be available over postbacks (so you can just bind the datagrid to the datatable on every page load, ensuring that any new data items are included)

D'Arcy
 
Hey JFrost,

I tried that and was having some problems and I figured maybe a DataTable varible couldn't be carried in a Session Variable, but maybe you can help me.

This is the code I have:

<code>
void Page_Load(Object sender, EventArgs e) {
if (!IsPostBack) {
InitDropDowns();
DataTable dt=new DataTable(&quot;TableData&quot;);
DataColumn dc=new DataColumn();
dc.DataType=System.Type.GetType(&quot;System.Int32&quot;);
dc.ColumnName=&quot;c1&quot;;
dt.Columns.Add(dc);
dc=new DataColumn();
dc.DataType=System.Type.GetType(&quot;System.Int32&quot;);
dc.ColumnName=&quot;c2&quot;;
dt.Columns.Add(dc);
dc=new DataColumn();
dc.DataType=System.Type.GetType(&quot;System.Currency&quot;);
dc.ColumnName=&quot;c3&quot;;
dt.Columns.Add(dc);
DataView dv=new DataView(dt);
OrderData.DataSource=dv;
Session[&quot;TheData&quot;]=dt;
OrderData.DataBind();
}
}

void AddSelectedToGrid(Object Sender, EventArgs e) {
if (ProductName.SelectedValue!=&quot;&quot;) {
DataTable dt=new DataTable(&quot;TableData&quot;);
dt=(DataTable) Session[&quot;TheData&quot;];
DataRow r=dt.NewRow();
r[&quot;c1&quot;]=ProductName.SelectedValue.ToString().Split(':')[1];
r[&quot;c2&quot;]=&quot;1&quot;;
r[&quot;c3&quot;]=ProductName.SelectedValue.ToString().Split(':')[3];
dt.Rows.Add(r);
Session[&quot;theData&quot;]=dt;
OrderData.DataSource=dt.DefaultView;
OrderData.DataBind();
}
}
</code>

The Problem is when the button is pressed I get an error on the Datarow r=dt.NewRow(); line that the object reference is not set to an instance of the object. I'm guessing this problem is referring to the DataTable object as I have created DataRow in the same manner in the past without any issues.

Any help is appreciated.
 
Hey Dave,

I'm not a C# programmer (work with VB.NET), but I tried to convert the code you had to vb and got it working. Here's what my code in the &quot;AddSelectedToGrid&quot; looks like:

Dim dt = New DataTable(&quot;TableData&quot;)
dt = Session(&quot;TheData&quot;)
Dim dr As DataRow = dt.NewRow

and here's yours in C#

DataTable dt=new DataTable(&quot;TableData&quot;);
dt=(DataTable) Session[&quot;TheData&quot;];
DataRow r=dt.NewRow();

The only thing I can think of is that maybe its the way you're declaring the datarow variable. Maybe it needs to be DataRow r as DataRow, then r = dt.NewRow();?

D'Arcy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top