WPF | How To Connect To SQLite Database | Read From SQLite | ListBox, Datagrid

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone um in this tutorial we are going to answer a question actually it's more of a request than a question so the request is how to use sql lite with wpf and that's because so far we did all with console application so in this tutorial we are going to create a new project in wpf and use sqlite with wpf okay so to start off i'm going to go to my solution explorer and actually you know what first let's just i'm gonna comment this last method we did with our a circular tutorial and uncomment to get people and run this let's see where we left off so we had sqlite database we created sqlite database and we have a few records inside our database so this is going to display get people it's going to display the people that we have in our database okay so that's our database content of the database and now we are going to go to solution explorer add a new project you can definitely do this on a separate solution i'm just it's going to be a simple tutorial and i'm trying to keep everything related in the same solution but it's kind of slows down but again feel free to create the new solution and do it do the same thing on a new solution okay so i'm going to add new project and it's going to be wpf.net click next i'm just going to add the keyword sqlite to the beginning of this project and click create okay so we have our project i'm going to make this startup project and flip the panes as i normally do and expand the xaml area okay so to start off i think i'm gonna add a list box actually split the column into a few pieces let's add two columns for now i may add more but to create that column definitions column definitions one column definitions two okay so we have two columns one two and let's start with a list box let's edit this box and give it a name let's call this lbx people okay let's close this out all right so we should have a list box right here okay let's uh build this and run it make sure it builds okay let's go ahead and run this okay so we have our list box right here so far so good um [Music] okay i want to actually add the startup position because i keep forgetting that windows startup's positioned startup location the center screen all right so let's go to our code behind here we have our default main window all right actually right now i don't need this we are going to first add the sqlite nougat package so manage nougat packages and let's go to browse okay i'm going to type in system data sqlite and take the first one right system data collect let's install this package let's click ok accept i think we are done so let's go ahead and rebuild this build again okay that's fine now i want to copy this couple of classes from our other tutorial one of them is the sqlite config cs and datacontext.cs or we can create two new classes and copy the contents which are easier for you you know let's i'm gonna drag and drop right here and let me double click on this class i'm just going to change the namespace to sqlite wpf app app one right that's it project name all right i think that's w should be capital not sure if it matters but let's make let's match it all right so we got error here that because we don't have the person class so let's bring in the person class let's drag and drop and if you don't want to change the namespaces you can just create classes and copy the contents whichever works for you so let's go to the person class and let's change the namespace over here too okay now that should clear this error here which it did let's bring in the sqlite config cs over and let's change the namespace okay let's build again it looks like build went through let's run this once okay no errors no exceptions so far so good let's close this one out okay so let's go back to our main windows yes um all right first i'm going to add another class here go to solution explorer add a new class this is going to be our data class so add new class i'm just going to call this um let's call this data just data that's fine okay all right i'm gonna make this public okay let's start with um get people so i'm going to create a static let's call it extreme public static and it's going to return a list of person and let's call this get people okay all right okay um let's create a variable of type of list of person okay actually new list of person let's create the object and then we are going to say similar to what we did in console application using var ctx close new data context okay so we're going to look through the database for each var p in ctx dot people i'm going to say list.add p okay and then we're going to return the lists okay all right once we have that we're going to go back to our main window and create a property here let me get rid of this comment here and go into our main window clasp and let's create a property so this is going to be list of person let's call this people and this is going to we're going to set this to data dot get people okay all right next i'm gonna go to our main window.xml let's create a data context first so data context actually window. data context it's going to be binding relative resource relative resource space self okay so basically we are setting the data context to the code behind okay now i'm going to go to my list box at the item source to people property okay all right so far let's see what happens once we build this and run this it looks like we got a problem we get an exception okay let's check the exception so we are getting a sql logic error no such table person okay let's find out why okay let's go to our solution explorer i think i forgot to bring the database over to our project so i'm going to open the file explorer go to bin folder that's why we can't find the table okay we don't have the tv file actually it's right here it created the table because we already have that set set up in the db context so if it doesn't if it can't find the database it's going to create here so we have a database but i think it's empty yeah so what i'm going to do i'm going to delete this okay oops did i delete i think i did all right and then i'm going to go back to our other projects and copy the database from there because we right now we have an empty database so okay this one here the last one we used xml to sqlite i mean you can use any of this as long as they have the person object in there and then you have to change the name here okay so i'm gonna go back to that folder um wpf app bin debug and paste it here okay now we should be okay i think let's build this again maybe it'll populate three up to date no it didn't pop late let's run this let's see what happens and there we have it so we got four person objects okay now we can do um things like display item pad i mean display memory pad you know what let's create around the template let's expand our list box and create template stack panel now data templates stack panel inside our stack panel i'm going to add a couple of text blocks this first one is going to be bound to id binding id and the second one is going to be bound to name and i think i'm going to set the orientation horizontal this way it displays side by side okay let's try this again run this all right sweet looks like we need some space in there but other than that we have all our data displayed in our list box okay if you want we can add a data grid here as well let's see let's add data grid to the second column data grid item source equals binding people and we need to place this inside grade dot column oops read that column one okay let's build this again and run it okay so we have data grid displaying the content of our sqlite database and then we have a list box doing the same okay all right so that was briefly how to connect our wpf application to our sqlite database so that's it for now um any questions comments or requests please drop in the comment section until the next one see ya take it easy
Info
Channel: tips'n tricks
Views: 9,535
Rating: undefined out of 5
Keywords: WPF, Visual Studio, C#, XAML, SQLite, Entity Framework, Datagrid, ListBox, Data Context
Id: 66K5Nmmc9g8
Channel Id: undefined
Length: 20min 31sec (1231 seconds)
Published: Fri Dec 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.