Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
Hello all

I've got a userControl that houses other userControls within tab items. They are made visible and cleared by interaction with various checkbox's on my parent userControl. I've validation and writetodatabase functions in place which works for parent userControl. What I need to have happen is when "Update" (utilizes CommandBinding) button on uC1 is clicked, that all relevant uC data on other tabs are saved to db as well. I've got validation to work but am stumped in saving the data of uC2/uC3/uC4 etc. ... any pointers would be most welcome.
PS I'm trying to keep my uC's as loosely coupled as possible... as well as I'm a beginner and step by step instructions would be great.

Thanks in advance.
Posted
Updated 12-May-13 13:40pm
v3
Comments
_Maxxx_ 12-May-13 21:25pm    
When you say you are keeping the UCs loosely coupled - just how loose? i.e. do you have a reference to the Children in your parent?
If you do have a reference then there's no real difference between there being a user control on the other tabs and, say, a text box. When your parent saves, it needs to access the data from the child controls and save it too.
It's difficult to do step by step instructions without seeing what you've already done - because there are simply so many ways you may have implemented it already.
_Maxxx_ 13-May-13 0:56am    
No worries. Ive looked at your code and am finding it difficult to follow what you're trying to do.
I THINK what you are asking for is simple - but I may be missing the point.
In UC2...UC99 have a public property that returns the data to be added/updated to the database.
Then in your update process you can call your WriteToDatabaseMCD() method passing that property as the parameter.
If necessary, the Getter of the property in UC2...UC99 will need to build your ManCageData object before returning it.
Another public property may be required for the IsUpdate parameter.
SDavisworth 13-May-13 2:27am    
Just been playing with tabs and events and have noticed that any values from uC2 return null error if focus on it is lost, hence your comment about rebuilding ManCageData before returning it (I guess). You have interpreted my requirements to the T. Is it possible to maintain my "WriteToDatabase" methods within each uC and have it processing its own added/updated methods... beginning to lose myself in all this code. PS. In your reply were you actually suggesting a way to do it or was it your synopsis of what you thought I was trying to accomplish? :-)
_Maxxx_ 13-May-13 2:57am    
If something like returning null is happening when you lose focus then something is wrong. My comment about rebuilding the data was not related to that - it's just that you need something to tell the child control "give me your data now" - and assuming on the child control there is a bunch of controls which the user has changed (textboxes or whatever) then the child control would have to build the object to return to the parent for it to save.
Of course you can have a write to database method in each UC - then all you need to do is to call that method on each of your child controls when the user clicks the save button on the parent control.
Personally I tend to keep any user controls small and low on functionality - so just capture the data and return it via a property. The Controller (in your case the parent control) then does the dirty work like saving it to the database.
There are arguments for doing it either way, though.

I was suggesting what you could do - it wasn't a synopsys.

SDavisworth 13-May-13 0:48am    
Got a ref to my Certificate repository as follows…
using PTWS.MainPanelControls.FormControls.Certificates;

To load uC2 into tab item dependant on checkbox….

else if (e.Command == cmdManCageRequiredYN)
{
switch (checkBoxManCageRequired.IsChecked)
{
case true:
stackPanelManCageDetail.Children.Clear();
_currentManCageDetail = new ManCageDetailUControl();
stackPanelManCageDetail.Children.Add(_currentManCageDetail);
tabManCageRequired.Visibility = Visibility.Visible;
IInputElement ieIn = Keyboard.Focus(tabManCageRequired);
break;
case false:
stackPanelManCageDetail.Children.Clear();
tabManCageRequired.Visibility = Visibility.Collapsed;
IInputElement ieOut = Keyboard.Focus(tabWorkPermit);
break;
}

Then update button is hooked into CommandBindings
<commandbinding command="Save" canexecute="UpdateDraftPermit_CanExecute" executed="UpdateDraftPermit_Executed">


Validation as follows:
private void Validation_Error(object sender, ValidationErrorEventArgs e)
{
if (e.Action == ValidationErrorEventAction.Added)
_noOfErrorsOnScreen++;
else
_noOfErrorsOnScreen--;
}

_Executed Routed Event as follows:
PermitData updateDraftPermit = new PermitData
{
… Blah blah
};

WriteToDataBase(updateDraftPermit, isUpdate);

Write to database – WriteToDataBase
private void WriteToDataBase(PermitData tableData, bool isUpdate)
{
#region Add New
if (!isUpdate)
{
db = new PTWDatabase();
Dictionary<string, string=""> data = new Dictionary<string, string="">();
data.Add("Status", tableData.PermitStatus);
data.Add… blah blah
try
{
db.Insert("Permits", data);
}

catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
#endregion

#region Update
else
{
db = new PTWDatabase();
Dictionary<string, string=""> data = new Dictionary<string, string="">();
data.Add("Status", tableData.PermitStatus);
data.Add… blah blah

try
{
db.Update("Permits", data, String.Format("Permits.PermitID = {0}", tableData.PermitID));
}

catch (Exception fail)
{
String error = "xxThe following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
#endregion
}

private void WriteToDataBaseMCD(ManCageData tableData, bool isUpdate)
{
#region Add New
if (!isUpdate)
{
db = new PTWDatabase();
Dictionary<string, string=""> data = new Dictionary<string, string="">();
data.Add("ManCageNumber", tableData.ManCageNumber);

try
{
db.Insert("CertificateManCage", data);
}

catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}

1 solution

Have you looked into member attributes? One of the things you can do in the dot net framework, is create a custom attribute and then tag class members with that attribute. In other words, you can tag the properties you want to save within a specific control. Once you've tagged the parts you want to save, the parent control can loop through the collection of child controls and use reflection to get the properties that are tagged with your custom attribute. I once wrote a *.ini file reader writer that would serialize and deserialize various controls this way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900