Thanos Markou
Thanos Markou

Reputation: 2624

Datatable is being overwritten

In an ASP.NET project i have 2 textboxes and a submit button.

In the event handler of button clicked i want to save the values of the textboxes to a datatable and then bind the datatable to a Gridview.

This has to happen multiple times. But every time the datatable has one row, like being overwritten every time the event handler fires. It's like the datatable is being created from the beginning every time the event handler fires. Code below. Thanks for your time.

EDIT: Thanks for all the answers you gave.

      protected void BtnInsertCustomerLegalRelationsInsert_Click(object sender, EventArgs e)
      {
        string FullCompanyName = TbxFullCompanyName.Text.Trim();
        object LegalRelationAfm = TbxLegalRelationAfm.Text.Trim();
        if (dtCustomersLegalRelations.Columns.Count == 0)
        {
            dtCustomersLegalRelations.Columns.Add("FullCompanyName",typeof(string));
            dtCustomersLegalRelations.Columns.Add("LegalRelationAfm",typeof(string));
        }
        DataRow dr = dtCustomersLegalRelations.NewRow();
        dr["FullCompanyName"] = FullCompanyName;
        dr["LegalRelationAfm"] = LegalRelationAfm;
        dtCustomersLegalRelations.Rows.Add(dr);            
        GvCustomerRegalRelations.DataSource = dtCustomersLegalRelations;
        GvCustomerRegalRelations.DataBind();
       }

The whole code behind here

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class ConnectedBorrowers_CustomerBasicInput : System.Web.UI.UserControl
{
    DataTable dtCustomersLegalRelations = new DataTable();   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {            
            TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = false;
        }
    }

    protected void CbMandatoryAFM_CheckedChanged(object sender, EventArgs e)
    {
        if (CbMandatoryAFM.Checked == true)
        {
            TbxCustAfm.ReadOnly = true;
            TbxCustAfm.BackColor = Color.LightGray;
        }
        else
        {
            TbxCustAfm.ReadOnly = false;
            TbxCustAfm.BackColor = Color.White;            
        }
    }

    protected void CbLegalRelationsMandatoryAFM_CheckedChanged(object sender, EventArgs e)
    {
        if (CbLegalRelationsMandatoryAFM.Checked == true)
        {
            TbxLegalRelationAfm.ReadOnly = true;
            TbxLegalRelationAfm.BackColor = Color.LightGray;
        }
        else
        {
            TbxLegalRelationAfm.ReadOnly = false;
            TbxLegalRelationAfm.BackColor = Color.White;
        }
    }

    protected void CbLegalRelations_CheckedChanged(object sender, EventArgs e)
    {
        if (CbLegalRelations.Checked == true)
        {
            TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = true;            
        }
        else
        {
            TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = false; 
        }
    }

    protected void BtnInsertCustomerLegalRelationsInsert_Click(object sender, EventArgs e)
    {
        try
        {
            string FullCompanyName = TbxFullCompanyName.Text.Trim();
            object LegalRelationAfm = TbxLegalRelationAfm.Text.Trim();
            if (dtCustomersLegalRelations.Columns.Count == 0)
            {
                dtCustomersLegalRelations.Columns.Add("FullCompanyName",typeof(string));
                dtCustomersLegalRelations.Columns.Add("LegalRelationAfm",typeof(string));
            }
            DataRow dr = dtCustomersLegalRelations.NewRow();
            dr["FullCompanyName"] = FullCompanyName;
            dr["LegalRelationAfm"] = LegalRelationAfm;
            dtCustomersLegalRelations.Rows.Add(dr);            
            GvCustomerRegalRelations.DataSource = dtCustomersLegalRelations;
            GvCustomerRegalRelations.DataBind();
        }
        catch (Exception ex)
        {
            ((Label)this.Page.Master.FindControl("LblError")).Text = ex.Message;
        }
    }

Upvotes: 1

Views: 337

Answers (4)

suhaim
suhaim

Reputation: 314

When u click the button postback occurs. During each postback your datatable is created from the beginning. So u will lose ur old data. So one option is that u can keep the datatable with data in a session and retrieve the datatable from the session during the next button click and add the new row.

Upvotes: 1

FabJC
FabJC

Reputation: 36

The value of 'dtCustomersLegalRelations' will not persist through PostBack events - so when you add the row on the end of it, you're doing so on a new instance.

You need to fetch all the data back out of the GridView before you add the new row and re-bind it.

st4hoo's answer above should sort it for you.

Upvotes: 1

st4hoo
st4hoo

Reputation: 2204

Try such approach:

public partial class ConnectedBorrowers_CustomerBasicInput : System.Web.UI.UserControl
{
    DataTable dtCustomersLegalRelations;   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {            
            dtCustomersLegalRelations = new DataTable(); 
            Session["table"] = dtCustomersLegalRelations;  
            TrLegalRelations1.Visible = TrLegalRelations2.Visible = TrLegalRelations3.Visible = false;
        } else {
             dtCustomersLegalRelations = Session["table"] as DataTable;
        }
    }

  ...

}

Upvotes: 1

Wizrd
Wizrd

Reputation: 24

At what point do you create the datatable (dtCustomersLegalRelations)? Could you paste the complete code including the cration of the datatable and the button_click?

Upvotes: -1

Related Questions