简体   繁体   中英

Dynamically adding multiple rows in data table in C#

I have been tried to use the DATATABLE for storing the values for the entire application. I haven't use any database for storing values permanently. This are the dummy values I could use through out the application.

The issue is actually whenever I add a new row value into my DATATABLE it just overrides the previous values in the DATATABLE instead of adding an new row.

Is there any chance to add multiple rows to the DATATABLE dynamically and I can use it through out the application session.

** MY REGISTER PAGE**

public partial class _Default : System.Web.UI.Page
{
    DataTable myDataTable;

    protected void Page_Load(object sender, EventArgs e)
    {
        myDataTable = new DataTable();
        myDataTable.Columns.Add("username");
        myDataTable.Columns.Add("firstname");
        myDataTable.Columns.Add("lastname");
        myDataTable.Columns.Add("password");
        myDataTable.Columns.Add("conpassword");
        myDataTable.Columns.Add("email");

        myDataTable.Rows.Add("kavi1", "kavi", "rajan", "123456", "123456", "kavi@gmail.com");
        myDataTable.Rows.Add("sri1", "sri", "prem", "123456", "123456", "sri@gmail.com");
        myDataTable.Rows.Add("mani1", "mani", "vanan", "123456", "123456", "mani@gmail.com");
        HttpContext.Current.Session["name"] = myDataTable;

        if (!IsPostBack)
        {


            if (Session["name"] != null)
            {

                myDataTable = Session["name"] as DataTable;

            }
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        myDataTable.Rows.Add(new object []{ TextBox1.Text ,TextBox2.Text,TextBox3.Text , TextBox4.Text,TextBox5.Text ,TextBox6.Text });
        HttpContext.Current.Session["name"] = myDataTable;

        if (Session["name"] != null)
        {
            Response.Write("success");

        }

    }

MY LOGIN PAGE

public partial class login : System.Web.UI.Page
{
    DataTable dtResult;

    protected void Page_Load(object sender, EventArgs e)
    {
        dtResult = (DataTable)Session["name"];
    }
    protected void Button1_Click(object sender, EventArgs e)
    {


        for (int i = 0; i < dtResult.Rows.Count; i++)
        {

            string un = dtResult.Rows[i].Field<string>("username");
            string fn = dtResult.Rows[i].Field<string>("password");


            if (TextBox1.Text == un && TextBox2.Text == fn)
            {
                //Response.Write("success");
                Response.Redirect("home.aspx");
            }

        }


    }

Issue:

At every Postback you are creating a new datatable and overriding it to the previous:

protected void Page_Load(object sender, EventArgs e)
    {
        myDataTable = new DataTable();
        //....
        HttpContext.Current.Session["name"] = myDataTable;    //overriding to the previous

So when you click on the button to add the new row, it posts back, and recreates a new datatable and then add a new row to that table.

Solution:

You just need to modify the Page_Load event handler:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["name"] != null)    //this is you have to do at every postback
    {
        myDataTable = Session["name"] as DataTable;
    }

    if (!IsPostBack)
    {
        //if it is not post back, create a new DataTable and add values to it
        myDataTable = new DataTable();
        myDataTable.Columns.Add("username");
        myDataTable.Columns.Add("firstname");
        myDataTable.Columns.Add("lastname");
        myDataTable.Columns.Add("password");
        myDataTable.Columns.Add("conpassword");
        myDataTable.Columns.Add("email");

        myDataTable.Rows.Add("kavi1", "kavi", "rajan", "123456", "123456", "kavi@gmail.com");
        myDataTable.Rows.Add("sri1", "sri", "prem", "123456", "123456", "sri@gmail.com");
        myDataTable.Rows.Add("mani1", "mani", "vanan", "123456", "123456", "mani@gmail.com");
        HttpContext.Current.Session["name"] = myDataTable;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM