'How can I fill up my page with data located in PostgreSQL as opposed from the hardcoded dummy/template info found in my ViewModel?
In my application, I am trying to practice the MVVM format (although I probably violated it at a few points and I’m refactoring as I catch it). I have a UserControl where it is a “recipe screen” that shows the title, notes, instructions, ingredients, and tools required that a user is supposed to type in. The page does fill up with the dummy info via my model/viewmodel which you'll see below, however; I am drawing a blank when it comes to filling up those spots with the actual data found in my database?
For context, I have the table made already with data that I would like to see on my recipe page to make sure all of the bindings work, but the thing is, I’m a complete beginner at it and would like some direction.
Research I've done: I keep seeing questions/answers about the Entity Framework, of which I’m not using so I'm not sure how relevant it is to me, unless that’s required of me to do this? I have also tried "reading" the data from my db and applying it, which you'll see in my code - but I'm not sure I did that correctly. Here is my code for reference:
public class RecipeViewModel : ViewModelBase
{
public ICommand NavigateHomeCommand { get; }
public NavigationBarViewModel NavigationBarViewModel { get; }
public Recipe CurrentRecipe { get; set; }
// DATABASE SECTION
public static NpgsqlConnection GetConnection()
{
return new NpgsqlConnection(@"Server=localhost;Port=5432;User Id=postgres;Password=abc123;Database=practiceDB;");
}
// Currently inactive database code that I attemped to use before, to no avail
private void LoadData()
{
NpgsqlConnection con = GetConnection();
string query = @"select * from recipes";
NpgsqlCommand cmd = new NpgsqlCommand(query, con);
con.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
var recipe = new Recipe()
{
Title = reader.GetString(reader.GetOrdinal("Title")),
Ingredients = reader.GetString(reader.GetOrdinal("Ingredients")),
Tools = reader.GetString(reader.GetOrdinal("Tools")),
Notes = reader.GetString(reader.GetOrdinal("Notes")),
Step1 = reader.GetString(reader.GetOrdinal("StepOne")),
Step2 = reader.GetString(reader.GetOrdinal("StepTwo")),
Step3 = reader.GetString(reader.GetOrdinal("StepThree")),
Step4 = reader.GetString(reader.GetOrdinal("StepFour")),
Step5 = reader.GetString(reader.GetOrdinal("StepFive")),
Step6 = reader.GetString(reader.GetOrdinal("StepSix")),
};
CurrentRecipe = recipe;
}
con.Close();
}
// Constructor with the dummy info
public RecipeViewModel(NavigationBarViewModel navigationBarViewModel, NavigationService<HomeViewModel> homeNavigationService)
{
NavigationBarViewModel = navigationBarViewModel;
NavigateHomeCommand = new NavigateCommand<HomeViewModel>(homeNavigationService);
CurrentRecipe = new Recipe();
CurrentRecipe.Title = "Example 1";
CurrentRecipe.Step1 = "Sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum. " +
"Ut diam quam nulla porttitor. Semper viverra nam libero justo laoreet sit. Potenti nullam ac tortor vitae purus faucibus." +
"Eget felis eget nunc lobortis mattis. Vel quam elementum pulvinar etiam non quam.";
CurrentRecipe.Step2 = "Sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum. " +
"Ut diam quam nulla porttitor. Semper viverra nam libero justo laoreet sit. Potenti nullam ac tortor vitae purus faucibus." +
"Eget felis eget nunc lobortis mattis. Vel quam elementum pulvinar etiam non quam.";
CurrentRecipe.Step3 = "Sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum. " +
"Ut diam quam nulla porttitor. Semper viverra nam libero justo laoreet sit. Potenti nullam ac tortor vitae purus faucibus." +
"Eget felis eget nunc lobortis mattis. Vel quam elementum pulvinar etiam non quam.";
CurrentRecipe.Step4 = "Sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum. " +
"Ut diam quam nulla porttitor. Semper viverra nam libero justo laoreet sit. Potenti nullam ac tortor vitae purus faucibus." +
"Eget felis eget nunc lobortis mattis. Vel quam elementum pulvinar etiam non quam.";
CurrentRecipe.Step5 = "Sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum. " +
"Ut diam quam nulla porttitor. Semper viverra nam libero justo laoreet sit. Potenti nullam ac tortor vitae purus faucibus." +
"Eget felis eget nunc lobortis mattis. Vel quam elementum pulvinar etiam non quam.";
CurrentRecipe.Step6 = "Sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum. " +
"Ut diam quam nulla porttitor. Semper viverra nam libero justo laoreet sit. Potenti nullam ac tortor vitae purus faucibus." +
"Eget felis eget nunc lobortis mattis. Vel quam elementum pulvinar etiam non quam.";
CurrentRecipe.Ingredients = "Insert Ingredients Here" +
"\n" +
"\n" +
"Insert Ingredients Here" +
"\n" +
"\n" +
"Insert Ingredients Here";
CurrentRecipe.Tools = "Insert Tools Here" +
"\n" +
"\n" +
"Insert Tools Here" +
"\n" +
"\n" +
"Insert Tools Here" +
"\n" +
"\n" +
"Insert Tools Here";
CurrentRecipe.Notes =
"Sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum. " +
"Ut diam quam nulla porttitor. Semper viverra nam libero justo laoreet sit. Potenti nullam ac tortor vitae purus faucibus." +
"Eget felis eget nunc lobortis mattis. Vel quam elementum pulvinar etiam non quam.";
}
}
In my XAML for the Title section, this is what I have to give you an idea of how I'm binding these:
<Label
Grid.Row="0"
Grid.Column="0"
Background="#1D3749"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
<Label>
<TextBlock
FontSize="35"
Foreground="White"
TextWrapping="WrapWithOverflow"
Grid.Row="0"
Grid.Column="0"
Text="{Binding CurrentRecipe.Title}" />
</Label>
</Label>
Solution 1:[1]
Entity Framework is not necessary, but it will simplify much of the data manipulation, so is recommended. The core issue is that your LoadData() method does not appear to be called from anywhere, so it will never execute. Additionally, you read all of your recipes from the database, and overwrite each recipe with the subsequent one that is read.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jonathan |
