Mastering C# Database Programming - How to populate ComboBox From Sql Server Database in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to my Channel today in this video tutorial we are going to create a combo box on this registration form and we will populate that uh combo box from a table in the database so I'll go to this label I'll duplicate this now we'll change its text so I'll change a text to districts and now I'll pick this a bit and now I'll go here and drag in a combo box to this and I will expand it a bit now it's okay now I'll drag it down a bit now we are good to go so now we will go to we will ah ssms scary several management studio and here we will create a new table so new table and table and here we will have two columns in this table on this trick district ID and it will be of type end and we will go to this and it will be top end and then we will have District no more just check itself so District name and it will be up in our car 50 and and this we will remove this and we will make this uh set as family key and we will return comment this as well so for that I will come to this uh identity specification and I will make this identity yes and now you will control s this and this will be underscore table and now click ok and now we will insert a few districts into this table so I'll refresh this so that the table appears so it is here now I'll edit top uh 200 rows and now I'll make some entries here so I'll make the shower path and then I'll make um on the ik DJ djg k and we will make mardan as well as noshera so we have this District now we will populate our combo box from this table now I will do have two things in that combo box we will have this uh district ID and District name both of them but this will be hidden and this District name will be shown in the combo box so for that I will come to this um which we studio and here we will go to the save not the shape but we will create a method down there here so it will be private data table and system type will be a data table um you know populate combo this is our method and now I'll use this now it is showing an error because we have to return a data table so we will use data table space DT equals new data table and here we will put semicolon and then we will return this data DT semicolon I will stop showing error but now we we have to write here the code so first we will create uh the connection so for that we will use using [Applause] skill connection con equals new SQL connection and here we will use curly braces now we have to copy our connection string here but the connection string is not Global so I'll copy it from here Ctrl C this or Ctrl X this big space and then now we will go here and we will use it yeah okay now this will be now I'll use the connection string here I'll use corn uh strain and now we are good to go so now I'll use command so using um s cable command CMD equals new let's give our Command and here we will use this now we will pass in the stored procedure here so here I will use a stored procedure get um districts in this store procedure we will create in our database so now here we will pass the connection and now here I'll open this connection because we later on forget to open this connection so con dot open and now our connection is open so now we have to create a the stored procedure for first I will use here CMD dot command type this one equals to command type dot stored procedure this one and I'll put semicolon here now this is done so now we will go to our ssms and here uh to this programmability and here to distort procedure now first I will copy the stroke procedure name from here this one because we have to create this stored procedure so I'll Ctrl C this now we will go here to our ssms and now I'll right click on this store procedure and new stored procedure and now it is showing us a template of store procedure but we will control a this and backspace this remove this now I'll use create Rock our procedure but if you use only proc it will work and then we will use straw procedure name then we will have a parameters if we have any and then we have S and then begin then we will have the select query select um District underscore ID comma District underscore name form um districts and let's go table and then and this is our procedure if we have any parameter we will use that here now I'll execute this and let's see if it is working now it is successfully executed so our store procedure is created now here if we have any parameter we can pass that parameter here so we do not have any parameter so we will now execute this stored procedure so I'll use this SQL data reader and reader equals new not new but CMD dot execute reader and now I'll put semicolon here now our reader executed so our data is in now in this reader so we will load that into our data table so DT Dot uh load reader and now I'll put semicolon here now I'll close the connection so con Dot close and and put semicolon now I'll go out of this uh into this method and now here I can use on this strip so it is not showing up the combo box so if we go to the design and check the name of the combo box so if I click on this and go to name so we haven't changed the name so that is why it is not showing up so this stick combo box so all control is it and now we will go down and here we will change this draw from drop down this drop down style to drop down list and now we will go to uh our code and here I'll use uh this strip combo box Dot data source equals DT semicolon now DT and we have assigned it a data table now let's run it and check this we will write some more code here but first let's check let's check this now if I click on this thing is showing up because we haven't called that method anywhere so for that I will come to this registration form and I will double click on the form to create uh to create the form load event so I'll double click on this now this is situation from node even so here I will call the populate combo method or function here now if I run this and now if I click on this and now you can see we have this data in this combo box but it is not showing as anything it is showing us system.datadata row view so I'll close this um this one and this one as well and now we will go to this method and here we will use this took combobox Dot and display member and here we will pass in the column name that we have in our database table which is this one we have to show this name Ctrl C and I'll come here and I'll control read here and now we will put semicolon here and now if I run this now it is running again so if I click on this now you can see peshava it's now about the ik DGK mardan and Nasha in our combo box now if I close this and if I make this ID so you will see that the RDS are also there it can show them now if I click on this now you can see that you can see one two three four five and six now these are the IDS District IDs but we will keep them as a value member now if we I close this close this close this so this is the stick name in which we will display in our combo box and we have a value member just check combo box dot value member and this will be our district underscore ID and we will click this value member and when we click the save button it will save this um value member into the database table which is this district ID we will see that or we will do that in the next tutorial so I think that's all for today I hope you like the video if you like the video please like subscribe and comment thank you
Info
Channel: Programming Guru
Views: 2,854
Rating: undefined out of 5
Keywords: Programming Guru, programming, c# - populate combobox control with data from database, how to populate data to combobox local database, populate combobox with database c#, populate combobox c# from database, fill combobox from database c#, how to populate combobox from database c#, populate combobox c# sql server, populate combobox c#, fill combobox c# database, c# - how do i load data into combobox from database, c# combobox, c#, combobox, database, winforms, .net, c# winforms
Id: uGMME4M5W6s
Channel Id: undefined
Length: 15min 44sec (944 seconds)
Published: Sun Oct 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.