C# / WPF - How should I save user-typed data all at once after a submit button click?
I'm learning WPF/C# via self-teaching and am currently building a small WPF app for practice instead of following tutorials, and so far it's been great; however, I have hit a roadblock that I've spent days on.
I have a WPF User Control with multiple TextBoxes where a user can type in a title, ingredients, tools, steps, notes, and add an image of what they made. After all of that is filled in, I would like for them to click a 'Submit' button and have that data saved and able to be accessed at another time.
Now for one, I don't know how I should go about doing that, I have seen mention of XML, JSON, SQLite, etc. I did try the XML method like so:
private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
Recipe recipe = new Recipe();
recipe.Title = TitleBox.Text;
recipe.Step1 = StepBox1.Text;
recipe.Step2 = StepBox2.Text;
recipe.Step3 = StepBox3.Text;
recipe.Step4 = StepBox4.Text;
recipe.Step5 = StepBox5.Text;
recipe.Step6 = StepBox6.Text;
recipe.Ingredients = IngredientBox.Text;
recipe.Tools = ToolBox.Text;
recipe.Notes = NoteBox.Text;
CreateViewModel.SaveData(recipe, TitleBox.Text);
}
In my ViewModel, I have this typed up:
public static void SaveData(object obj, string filename)
{
XmlSerializer sr = new XmlSerializer(obj.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, obj);
writer.Close();
}
This does end up "working" (without the image of course) in that I see the file and can access it manually via the explorer (loading the file in the program is a whole other thing I'm going to tackle another day), but I feel that this isn't the way to go about it from what I've seen on my searches. If there's a better strategy here that I'm missing, I would love to use and learn from it.
Thank you
EDIT
Alright so I downloaded and am currently using PostgreSQL thanks to @Jonathon Willcock (watched a long video on it to get the gist, and am currently diving deep into it even more as I implement it into my small app), so far I think I've been successful in saving the data into a table with the necessary values as I see it there currently, but I'm clueless when it comes to saving an image. I will continue to work at it and try to figure it out, but if anyone has any advice for me on how I could go about doing that, I would greatly appreciate it!
Comments
Post a Comment