Good morning everyone, I really need some help.
I started this little project to help me study, while I am reading through O'Reilly's Code First book. I understand how to create a database and I can also populate my table with data that I provide from a WPF application, namely a few textboxes, open dialog/ image control.
My problem is that I don't understand how to retrieve my data from my table, using codefirst.
I have tried using the context.sqlquery and it just throws me errors.
Here is my Model:
namespace BusinessModel
{
public class Vinyl
{
public int VinylId { get; set; }
public string Name { get; set; }
public string VinylLabel { get; set; }
public string Genre { get; set; }
public string SubGenre { get; set; }
public byte[] Photo { get; set; }
public string Url { get; set; }
}
}
Here is my DataAccess:
public class VinylContext : DbContext
{
public DbSet<Vinyl> VinylSet { get; set; }
}
Here is my Xaml for adding an entry:
<Grid Margin="0,0,-226,-26">
<Button x:Name="btnSave" Content="Save" HorizontalAlignment="Left" Margin="432,288,0,0" VerticalAlignment="Top" Width="75" Click="btnSave_Click"/>
<TextBox x:Name="tbName" HorizontalAlignment="Left" Height="23" Margin="10,74,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="tbVinylLabel" HorizontalAlignment="Left" Height="23" Margin="10,133,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="tbGenre" HorizontalAlignment="Left" Height="23" Margin="10,192,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="tbSubGenre" HorizontalAlignment="Left" Height="23" Margin="10,251,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label x:Name="lblName" Content="Name" HorizontalAlignment="Left" Margin="10,43,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
<Label Content="Add New Vinyl" Margin="10,10,10,0" VerticalAlignment="Top" FontSize="16" FontWeight="Bold"/>
<Label x:Name="lblVinylLabel" Content="Vinyl Label" HorizontalAlignment="Left" Margin="10,102,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
<Label x:Name="lblGenre" Content="Genre" HorizontalAlignment="Left" Margin="10,161,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
<Label x:Name="lblSubGenre" Content="Sub Genre" HorizontalAlignment="Left" Margin="10,220,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
<Button x:Name="btnUpload" Content="Upload" HorizontalAlignment="Left" Margin="10,288,0,0" VerticalAlignment="Top" Width="75" Click="btnUpload_Click"/>
<Label x:Name="lblImageUpload" Visibility="Hidden" Content="Upload Image" HorizontalAlignment="Right" Margin="0,284,90,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51" Width="337"/>
<Label x:Name="lblUrl" Content="Website" HorizontalAlignment="Left" Margin="135,46,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
<TextBox x:Name="tbUrl" HorizontalAlignment="Left" Height="23" Margin="135,74,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="292" Text="http://"/>
<Image x:Name="imgPreview" HorizontalAlignment="Left" Height="141" Margin="135,133,0,0" VerticalAlignment="Top" Width="292"/>
<Label x:Name="lblImage" Content="Image Preview" HorizontalAlignment="Left" Margin="135,102,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
</Grid>
Here is my back end:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Microsoft.Win32;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using BusinessModel;
using DataAccess;
namespace VinylCollection
{
public partial class ucAddVinyl : UserControl
{
public ucAddVinyl()
{
InitializeComponent();
}
#region Variable
byte[] newImage;
#endregion
#region Methods
public void InsertVinyl()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<VinylContext>());
Vinyl vinyl = new Vinyl();
vinyl.Name = tbName.Text;
vinyl.VinylLabel = tbVinylLabel.Text;
vinyl.Genre = tbGenre.Text;
vinyl.SubGenre = tbSubGenre.Text;
vinyl.Url = tbUrl.Text;
vinyl.Photo = newImage;
VinylContext contextVinyl = new VinylContext();
contextVinyl.VinylSet.Add(vinyl);
contextVinyl.SaveChanges();
}
public void OpenImage()
{
OpenFileDialog ofg = new OpenFileDialog();
ofg.Title = "Select an image";
ofg.Filter = "Images (.jpg, .png, .gif, .bmp)|*.jpg;*.png;*.gif;*.bmp";
ofg.Multiselect = false;
if (ofg.ShowDialog() == true)
{
if (ofg.FileName != null && ofg.FileName.Length > 0)
{
ofg.OpenFile();
FileStream fs = new FileStream(ofg.FileName, FileMode.Open, FileAccess.Read);
lblImageUpload.Content = ByteImageConverter.ImageToByte(fs);
byte[] imgStr = Convert.FromBase64String(lblImageUpload.Content.ToString());
newImage = imgStr;
imgPreview.Source = ByteImageConverter.ByteToImage(imgStr);
}
}
}
#endregion
#region Classes
public class ByteImageConverter
{
public static ImageSource ByteToImage(byte[] imageData)
{
BitmapImage biImg = new BitmapImage();
MemoryStream ms = new MemoryStream(imageData);
biImg.BeginInit();
biImg.StreamSource = ms;
biImg.EndInit();
ImageSource imgSrc = biImg as ImageSource;
return imgSrc;
}
public static string ImageToByte(FileStream fs)
{
byte[] imgBytes = new byte[fs.Length];
fs.Read(imgBytes, 0, Convert.ToInt32(fs.Length));
string encodeData = Convert.ToBase64String(imgBytes, Base64FormattingOptions.InsertLineBreaks);
return encodeData;
}
}
#endregion
#region Events
private void btnSave_Click(object sender, RoutedEventArgs e)
{
InsertVinyl();
}
private void btnUpload_Click(object sender, RoutedEventArgs e)
{
OpenImage();
}
#endregion
}
}
So this all works fine, the problem is getting that data back, and putting it into some labels and an image.
Here is my Xaml:
<Grid Margin="0,0,-149,-48">
<TextBox x:Name="tbSearch" HorizontalAlignment="Left" Height="23" Margin="10,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
<Label x:Name="lblSearch" Content="Vinyl Search" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontSize="16" FontWeight="Bold"/>
<Button x:Name="btnSearch" Content="Search" HorizontalAlignment="Left" Margin="215,41,0,0" VerticalAlignment="Top" Width="75" Click="btnSearch_Click"/>
<ComboBox x:Name="cbVinylResults" HorizontalAlignment="Left" Margin="10,105,0,0" VerticalAlignment="Top" Width="200"/>
<Label x:Name="lblVinylResults" Content="Vinyl Results" HorizontalAlignment="Left" Margin="10,69,0,0" VerticalAlignment="Top"/>
<Label x:Name="lblName" Content="Name" HorizontalAlignment="Left" Margin="10,133,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
<Label x:Name="lblVinylLabel" Content="Vinyl Label" HorizontalAlignment="Left" Margin="10,164,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
<Label x:Name="lblGenre" Content="Genre" HorizontalAlignment="Left" Margin="10,195,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
<Label x:Name="lblSubGenre" Content="Sub Genre" HorizontalAlignment="Left" Margin="10,226,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
<Label x:Name="lblImageUpload" Visibility="Hidden" Content="Upload Image" HorizontalAlignment="Right" Margin="0,284,90,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51" Width="337"/>
<Label x:Name="lblUrl" Content="Website" HorizontalAlignment="Left" Margin="10,257,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
<Image x:Name="imgPreview" HorizontalAlignment="Left" Height="115" Margin="123,164,0,0" VerticalAlignment="Top" Width="167"/>
<Label x:Name="lblImage" Content="Image Preview" HorizontalAlignment="Left" Margin="123,133,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51"/>
</Grid>
I have no idea where to begin with getting the data back. The idea is to search for a file, and the search button will find all entries for that word in the data, then put them in the listbox. Then when you select an item, you can view the details in using the labels.
(UPDATE) I have added the code to bring back all entries, however, I am still unsure how to use the textbox to search for a specific name, and populate the listbox with the resulting name.
public void GetVinyl()
{
using (var db = new VinylContext())
{
var query = from b in db.VinylSet
orderby b.VinylId
select b;
foreach (var item in query)
{
cbVinylResults.Items.Add(item.Name);
}
}
}
Also I need for the listbox item to be selected and then that names information is then populated into my labels.
I know it might be easier to use listview or a datagrid, but this is the way I have to do it.
Thank you...