Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 545955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:48:58+00:00 2026-05-13T10:48:58+00:00

I have the following code: Imports System.Data Partial Class Students_AddWishes Inherits System.Web.UI.Page Public dt

  • 0

I have the following code:

Imports System.Data

Partial Class Students_AddWishes Inherits System.Web.UI.Page

    Public dt As New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        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"))
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim row1 As DataRow = dt.NewRow()
        row1("ID") = dt.Rows.Count + 1
        row1("univirsity") = ddlUnivs.SelectedValue
        row1("major") = ddlMajors.SelectedValue
        dt.Rows.Add(row1)
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

End Class

The problem is it shows only one row or record. How to make it shows many records?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:48:58+00:00Added an answer on May 13, 2026 at 10:48 am

    Your page load event you are not checking if it is a post back:

     If Not IsPostBack Then
         'process code if it is not a post back
     End If
    

    Everytime you click the btnAdd button your page does a post back to the server.

    I just noticed that you probably are not understanding the life time of an object.

    You had done this in your code:

    Public dt As New DataTable 
    

    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.

    You will have to make some use of session variables or a database to store the state of dt.

    Here is an example in C#:

     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(); 
        }
    

    And the same code in vb.net:

    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 
    

    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…

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: Imports System.Data Imports System.Data.OleDb Partial Class Dummy Inherits System.Web.UI.Page
I have the following code: Imports MySql.Data.MySqlClient Imports MySql.Data.Types Public Class FormMain Private Sub
I have the following code : Imports System.Data.OleDb Private Sub getData() Dim connStr As
I have tried the following code but has no effect: Imports system.Runtime.InteropServices <DllImport(UxTheme.DLL, BestFitMapping:=False,
I have the following Haskell code import Data.Int import System.Environment type Coord = (Int16,
I have written the following IronPython code: import clr clr.AddReference(System.Drawing) from System import *
I have the following code: import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; public
I have the following code: import com.apple.dnssd.*; public interface IServiceAnnouncer { public void registerService();
I have the following Java code: import java.util.concurrent.*; class Foo{ static Semaphore s =
I have the following code import smtplib from email.mime.text import MIMEText smtpserver = 'smtp.gmail.com'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.