Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your page load event you are not checking if it is a post back:</p> <pre><code> If Not IsPostBack Then 'process code if it is not a post back End If </code></pre> <p>Everytime you click the btnAdd button your page does a post back to the server.</p> <p>I just noticed that you probably are not understanding the life time of an object.</p> <p>You had done this in your code:</p> <pre><code>Public dt As New DataTable </code></pre> <p>The problem with that is you have defined this is a class variable and once the page has loaded you have an instance of type dt that may have some columns associated with it. But as soon as you record an event such as a button click that reference is destroyed and a new dt is created.</p> <p>You will have to make some use of session variables or a database to store the state of dt.</p> <p>Here is an example in C#:</p> <pre><code> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.Add("ID", System.Type.GetType("System.Int32")); dt.Columns.Add("univirsity", System.Type.GetType("System.Int32")); dt.Columns.Add("major", System.Type.GetType("System.Int32")); Session["MyDataTable"] = dt; } } protected void btnAdd_Click(object sender, EventArgs e) { DataTable t = (DataTable)Session["MyDataTable"]; DataRow row1 = t.NewRow(); row1["ID"] = t.Rows.Count + 1; row1["univirsity"] = 3; row1["major"] = 31; t.Rows.Add(row1); Session["MyDataTable"] = t; GridView1.DataSource = t; GridView1.DataBind(); } </code></pre> <p>And the same code in vb.net:</p> <pre><code>Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Page.IsPostBack Then Dim dt As New DataTable() dt.Columns.Add("ID", System.Type.[GetType]("System.Int32")) dt.Columns.Add("univirsity", System.Type.[GetType]("System.Int32")) dt.Columns.Add("major", System.Type.[GetType]("System.Int32")) Session("MyDataTable") = dt End If End Sub Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Dim t As DataTable = DirectCast(Session("MyDataTable"), DataTable) Dim row1 As DataRow = t.NewRow() row1("ID") = t.Rows.Count + 1 row1("univirsity") = 3 row1("major") = 31 t.Rows.Add(row1) Session("MyDataTable") = t GridView1.DataSource = t GridView1.DataBind() End Sub </code></pre> <p>So now what the code does is instantiate a new datatable object as long as we are on the page (first post back) and adds the columns. Once it has defined the data table we throw it in some session state. When you click the add button you cannot in your previous code just keep using dt because dt scope was lost in your prior code. We do this by assigning the sessioned datatable which was stored prior to a temp datatable. We add the row and reset the session that way the next time we add a row it will display the second row, then third row, and so on...</p> <p>I recommend a good asp.net book on such as Beginning ASP.net 3.5 in C# 2008. There are a ton of vb.net books on the same subject matter.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload