Issues with datatable

Joined
Feb 17, 2010
Messages
2
Reaction score
0
I'm working on a database-driven food pantry application which will allow a user to add a new household to the database. The user would theoretically add members to this household by using a modal popup form to add a new family member by either entering new information in a form, or search for a person who is already in the system but is not assigned to a household. The new family member would be added to a datable which is then databinded to a gridview. When the household information is saved, we theoretically add everyone in the datable to the newly created household by changing their HHID (the foreign key in the database linking them to the database) to that of the newly created household. It makes use of Ajax update panels to dynamically add data without reloading the page.

Now here is where the problem comes into play: when the user adds the new family member (either by searching or entering new data), the datatable is cleaned out, and only the newly created family member shows up. My code is as follows:

This function is what actually puts the data into the datatable.

Code:
        protected void AddNewFamilyMemberToDatatable(string SSN, string First, string Last, string DOB, string AdultChild, string Gender, bool Head, String ConsentForm, string Ethnicity)
        {
            FamilyWarning.Text += "<br/>There are now" + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table before adding empty columns and after getting changes";     
            AddEmptyColumnsToDatatable(NewHouseholdDatatable);//This is used to add empty columns to the datatable in case they aren't there already, otherwise I'd get a "does not contain column _ exception.
 
             FamilyWarning.Text += "<br/>There are now" + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table after adding empty columsn";

            FamilyWarning.Text += "<br/>There are now" + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table after assigning datasource";

            //NewHouseholdDatable is the name of the datatable I'm using, and it's set up in the page's Init event.
            DataRow Row = NewHouseholdDatatable.NewRow();
            //This stuff simply sets the values to the new row
            if (Head)
            {
                Row["HeadOfHousehold"] = true;
            }
            else
            {
                Row["HeadOfHousehold"] = false;

            }
            Row["Firstname"] = First;
            Row["DOB"] = DOB;
            Row["Lastname"] = Last;
            Row["SSN"] = SSN;
            Row["AdultChild"] = AdultChild;
            Row["MaleFemale"] = Gender;

            if (ConsentForm == "1")
            { Row["ConsentForm"] = true; }
            else
            { Row["ConsentForm"] = false; }
            Row["Ethnicity"] = Ethnicity;
            if (!CheckIsDate(DOB))
            {
                Row["Age"] = 0;
                Row["DateOfBirth"] = "Unknown, please edit and enter proper data";
            }
            else
            {
                Row["Age"] = GetAge(DateTime.Parse(DOB));
                DateTime DateAndTimeOfBirth = Convert.ToDateTime(DOB);
                Row["DateOfBirth"] = DateAndTimeOfBirth.ToShortDateString();
            }
            try
            {
                FamilyWarning.Text += "<br/>There are " + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table before adding";
                    NewHouseholdDatatable.Rows.Add(Row);
                            
   
                FamilyWarning.Text += "<br/>There are now" + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table after accepting changes";
                ClearForm();
                //FamilyMembers is the name of the gridview that I'm binding it to.,
             FamilyMembers.DataSource = NewHouseholdDatatable;
                FamilyMembers.DataBind();
                FamilyWarning.Text += "<br/>There are now" + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table after databinding";
               // FamilyWarning.Text = Row["Firstname"] + " " + Row["Lastname"] + " successfully added.  There are " + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table.";
                
            }
            catch (Exception ex)
            {
                FamilyWarning.Text += "<br/>Unable to add this person to the table, " + ex.Message;
            }
            
        }
 
Joined
Feb 17, 2010
Messages
2
Reaction score
0
Nevermind, Problem solved

Nevermind, I solved it. My problem was that whenever I did an Ajax postback, the entire table would be lost, not emptied. So all I had to do was copy it to a viewstate, and then reload it from the viewstate. It goes as follows:

Code:
//First I added these two lines to a function inside my load event, and the function only ran if the page wasn't a postback:
NewHouseholdDatatable = new DataTable();
                AddEmptyColumnsToDatatable(NewHouseholdDatatable);

Then I changed my adding code to the following:

Code:
protected void AddNewFamilyMemberToDatatable(string SSN, string First, string Last, string DOB, string AdultChild, string Gender, bool Head, String ConsentForm, string Ethnicity)
        {
          //If there was something in the viewstate, I loaded it from memory.  Otherwise, I simply recreated the table.
                if (ViewState["NewHouseholdDatatable"] != null)
                {
                    NewHouseholdDatatable = (DataTable)ViewState["NewHouseholdDatatable"];
                    //  FamilyWarning.Text += "<br/>There are now" + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table before adding empty columns and after getting changes";     
                    AddEmptyColumnsToDatatable(NewHouseholdDatatable);//This is used to add empty columns to the datatable in case they aren't there already, otherwise I'd get a "does not contain column _ exception.
                }
                else
                {
                    NewHouseholdDatatable = new DataTable();
                    AddEmptyColumnsToDatatable(NewHouseholdDatatable);
                }
                FamilyWarning.Text += "<br/>There are now" + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table after adding empty columsn";

                FamilyWarning.Text += "<br/>There are now" + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table after assigning datasource";

                //NewHouseholdDatable is the name of the datatable I'm using, and it's set up in the page's Init event.
                DataRow Row = NewHouseholdDatatable.NewRow();
                //This stuff simply sets the values to the new row
                if (Head)
                {
                    Row["HeadOfHousehold"] = true;
                }
                else
                {
                    Row["HeadOfHousehold"] = false;

                }
                Row["Firstname"] = First;
                Row["DOB"] = DOB;
                Row["Lastname"] = Last;
                Row["SSN"] = SSN;
                Row["AdultChild"] = AdultChild;
                Row["MaleFemale"] = Gender;

                if (ConsentForm == "1")
                { Row["ConsentForm"] = true; }
                else
                { Row["ConsentForm"] = false; }
                Row["Ethnicity"] = Ethnicity;
                if (!CheckIsDate(DOB))
                {
                    Row["Age"] = 0;
                    Row["DateOfBirth"] = "Unknown, please edit and enter proper data";
                }
                else
                {
                    Row["Age"] = GetAge(DateTime.Parse(DOB));
                    DateTime DateAndTimeOfBirth = Convert.ToDateTime(DOB);
                    Row["DateOfBirth"] = DateAndTimeOfBirth.ToShortDateString();
                }
                try
                {
                    FamilyWarning.Text += "<br/>There are " + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table before adding";
                    NewHouseholdDatatable.Rows.Add(Row);


                    FamilyWarning.Text += "<br/>There are now" + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table after accepting changes";
                    ClearForm();
                    //FamilyMembers is the name of the gridview that I'm binding it to.,
                    ViewState["NewHouseholdDatatable"] = NewHouseholdDatatable;
                    FamilyMembers.DataSource = NewHouseholdDatatable;
                    FamilyMembers.DataBind();
                    FamilyWarning.Text += "<br/>There are now" + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table after databinding";
                    // FamilyWarning.Text = Row["Firstname"] + " " + Row["Lastname"] + " successfully added.  There are " + NewHouseholdDatatable.Rows.Count.ToString() + " rows in the table.";

                }
                catch (Exception ex)
                {
                    FamilyWarning.Text += "<br/>Unable to add this person to the table, " + ex.Message;
                }

            
        }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top