Maintain a DataTable between postbacks...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason

    #1

    Maintain a DataTable between postbacks...

    I have a DataTable from which I am trying to delete rows using the
    DataGrid control. However, I'm having trouble maintaining the changes
    to the DataTable between PostBacks. This is my first go 'round with
    the DataGrid control, so pardon me if there are glaring mistakes...

    public partial class control_panel_m ail_Default : System.Web.UI.P age
    {
    private DataTable dt;

    protected void Page_Load(objec t sender, EventArgs e)
    {
    dt = new DataTable();
    LoadContacts();
    }

    private void LoadContacts()
    {
    if (!IsPostBack)
    {
    DataColumn dc;

    dc = new DataColumn();
    dc.ColumnName = "blog_contact_f irstname";
    dc.DataType = System.Type.Get Type("System.St ring");
    dt.Columns.Add( dc);

    dc = new DataColumn();
    dc.ColumnName = "blog_contact_l astname";
    dc.DataType = System.Type.Get Type("System.St ring");
    dt.Columns.Add( dc);

    dc = new DataColumn();
    dc.ColumnName = "blog_contact_e mail";
    dc.DataType = System.Type.Get Type("System.St ring");
    dt.Columns.Add( dc);

    SqlConnection loConnection = new
    SqlConnection(C onfigurationMan ager.AppSetting s["Data_Connectio n_String"].ToString());
    SqlDataAdapter loAdapter = new SqlDataAdapter( "SELECT
    blog_contact_fi rstname, blog_contact_la stname, blog_contact_em ail FROM
    tbblog_contact WHERE blog_contact_de leted = 0 AND blog_contact_op tout =
    0 ORDER BY blog_contact_fi rstname", loConnection);
    loAdapter.Fill( dt);

    gvRecipientList .DataSource = dt;
    gvRecipientList .DataBind();
    }
    else
    {
    dt = ViewState["ContactLis t"] as DataTable;
    gvRecipientList .DataSource = dt;
    gvRecipientList .DataBind();
    }
    }

    protected void gvRecipientList _RowDeleting(Ob ject sender,
    GridViewDeleteE ventArgs e)
    {
    DataRow dr = dt.Rows[e.RowIndex];
    dt.Rows.Remove( dr);
    ViewState.Add(" ContactList", dt);
    }
    }

    It fails on the statement:
    DataRow dr = dt.Rows[e.RowIndex];
    stating that the object reference not set to an instance of an
    object... This happens even on the very first PostBack after the
    initial load of the page.

    Thanks in advance!
    Jason

  • liming

    #2
    Re: Maintain a DataTable between postbacks...

    I might be wrong...

    The first time when you do a postback, there is no ViewState["ContactLis t"]
    yet it seems. when it gets to the deleting part, i imagine there is nothing
    for it to delete.

    You should add ViewState.Add(" ContactList", dt) into your
    if(!Page.IsPost Back) block.




    "Jason" <jasonmwilkerso n@hotmail.com> wrote in message
    news:1147286013 .564194.288180@ q12g2000cwa.goo glegroups.com.. .[color=blue]
    >I have a DataTable from which I am trying to delete rows using the
    > DataGrid control. However, I'm having trouble maintaining the changes
    > to the DataTable between PostBacks. This is my first go 'round with
    > the DataGrid control, so pardon me if there are glaring mistakes...
    >
    > public partial class control_panel_m ail_Default : System.Web.UI.P age
    > {
    > private DataTable dt;
    >
    > protected void Page_Load(objec t sender, EventArgs e)
    > {
    > dt = new DataTable();
    > LoadContacts();
    > }
    >
    > private void LoadContacts()
    > {
    > if (!IsPostBack)
    > {
    > DataColumn dc;
    >
    > dc = new DataColumn();
    > dc.ColumnName = "blog_contact_f irstname";
    > dc.DataType = System.Type.Get Type("System.St ring");
    > dt.Columns.Add( dc);
    >
    > dc = new DataColumn();
    > dc.ColumnName = "blog_contact_l astname";
    > dc.DataType = System.Type.Get Type("System.St ring");
    > dt.Columns.Add( dc);
    >
    > dc = new DataColumn();
    > dc.ColumnName = "blog_contact_e mail";
    > dc.DataType = System.Type.Get Type("System.St ring");
    > dt.Columns.Add( dc);
    >
    > SqlConnection loConnection = new
    > SqlConnection(C onfigurationMan ager.AppSetting s["Data_Connectio n_String"].ToString());
    > SqlDataAdapter loAdapter = new SqlDataAdapter( "SELECT
    > blog_contact_fi rstname, blog_contact_la stname, blog_contact_em ail FROM
    > tbblog_contact WHERE blog_contact_de leted = 0 AND blog_contact_op tout =
    > 0 ORDER BY blog_contact_fi rstname", loConnection);
    > loAdapter.Fill( dt);
    >
    > gvRecipientList .DataSource = dt;
    > gvRecipientList .DataBind();
    > }
    > else
    > {
    > dt = ViewState["ContactLis t"] as DataTable;
    > gvRecipientList .DataSource = dt;
    > gvRecipientList .DataBind();
    > }
    > }
    >
    > protected void gvRecipientList _RowDeleting(Ob ject sender,
    > GridViewDeleteE ventArgs e)
    > {
    > DataRow dr = dt.Rows[e.RowIndex];
    > dt.Rows.Remove( dr);
    > ViewState.Add(" ContactList", dt);
    > }
    > }
    >
    > It fails on the statement:
    > DataRow dr = dt.Rows[e.RowIndex];
    > stating that the object reference not set to an instance of an
    > object... This happens even on the very first PostBack after the
    > initial load of the page.
    >
    > Thanks in advance!
    > Jason
    >[/color]


    Comment

    • Jason

      #3
      Re: Maintain a DataTable between postbacks...

      You are correct. When the page is initially loaded, (!IsPostBack) is a
      true condition, so, it grabs the information from the database. That
      is obvious because the GridView is populated with the correct data upon
      initial load.

      But, it is when you enter gvRecipientList _RowDeleting for the first
      time, it's saying that "dt" is not set to an instance of an object.
      That's what I'm not understanding at the moment...

      Comment

      • liming

        #4
        Re: Maintain a DataTable between postbacks...

        So did it work for you or ..?

        1. First time enter in the page, gridview binded directly to the data from
        db.
        2. PostBack, Gridview binded to a null ViewState object, so did "dt".
        3. Enter gvRecipientList _RowDeleting, you try to retrieve "dt".Rows.. . but
        "dt" is null at this time.


        "Jason" <jasonmwilkerso n@hotmail.com> wrote in message
        news:1147290118 .243299.227390@ u72g2000cwu.goo glegroups.com.. .[color=blue]
        > You are correct. When the page is initially loaded, (!IsPostBack) is a
        > true condition, so, it grabs the information from the database. That
        > is obvious because the GridView is populated with the correct data upon
        > initial load.
        >
        > But, it is when you enter gvRecipientList _RowDeleting for the first
        > time, it's saying that "dt" is not set to an instance of an object.
        > That's what I'm not understanding at the moment...
        >[/color]


        Comment

        • Jason

          #5
          Re: Maintain a DataTable between postbacks...

          I don't know if this is a proper solution, but I got this to work:

          public partial class control_panel_m ail_Default : System.Web.UI.P age
          {
          private DataTable dt;

          protected void Page_Load(objec t sender, EventArgs e)
          {
          dt = new DataTable();
          if (!IsPostBack)
          {
          LoadContacts();
          }
          else
          {
          dt = ViewState["ContactLis t"] as DataTable;
          }
          ViewState.Add(" ContactList", dt);
          gvRecipientList .DataSource = dt;
          gvRecipientList .DataBind();
          }

          private void LoadContacts()
          {
          DataColumn dc;

          dc = new DataColumn();
          dc.ColumnName = "blog_contact_f irstname";
          dc.DataType = System.Type.Get Type("System.St ring");
          dt.Columns.Add( dc);

          dc = new DataColumn();
          dc.ColumnName = "blog_contact_l astname";
          dc.DataType = System.Type.Get Type("System.St ring");
          dt.Columns.Add( dc);

          dc = new DataColumn();
          dc.ColumnName = "blog_contact_e mail";
          dc.DataType = System.Type.Get Type("System.St ring");
          dt.Columns.Add( dc);

          SqlConnection loConnection = new
          SqlConnection(C onfigurationMan ager.AppSetting s["Data_Connectio n_String"].ToString());
          SqlDataAdapter loAdapter = new SqlDataAdapter( "SELECT
          blog_contact_fi rstname, blog_contact_la stname, blog_contact_em ail FROM
          tbblog_contact WHERE blog_contact_de leted = 0 AND blog_contact_op tout =
          0 ORDER BY blog_contact_fi rstname", loConnection);
          loAdapter.Fill( dt);

          gvRecipientList .DataSource = dt;
          gvRecipientList .DataBind();
          }

          protected void gvRecipientList _RowDeleting(Ob ject sender,
          GridViewDeleteE ventArgs e)
          {
          dt = ViewState["ContactLis t"] as DataTable;
          dt.Rows.Remove( dt.Rows[e.RowIndex]);
          ViewState.Add(" ContactList", dt);
          gvRecipientList .DataSource = dt;
          gvRecipientList .DataBind();
          }
          }

          Comment

          Working...