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.
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;
}
}