Load Records from Database in C# Windows Form for Update | Load Data From a Database Using C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to my Channel today in this video tutorial we will learn about loading data into our registration form for updating uh so we will select arrow in this data grid View and we will double click on it and it will take us to the registration form and we will get the ID from student ID from that row and we will get the data from the database with that student ID and we will show it in this uh at a decision form and then we will update it so I'll close this I'll close this as well so first we have to create the double click event on this so we I'm in this uh events tab of this data grid view so if I go down and search for double click so it is here so I'll double click on this to create this double click event now here we will get the first we will get the selected row and then we will get the student ID from the selected row so I'll use end selected oh rope equals dvg student dot rows dot get first row and then we will pass in uh data grid view state it is intellisense is showing us so data grid view State DOT selected and now we have the selected row so now we will get the ID from this row so now we will use here and STD ID equals um dvg student dot rows and we will pass in the row selected row here selected row Dot uh cells and then we will pass in the cell name so the cell name is a student ID and now uh then we will pass in the value so value and now it is saying that uh you cannot convert it to integers so I'll put semicolon here and to convert this we will use the convert method so convert dot 2 and 16 and now I'll and close it in small brackets so now we are good to go so now we have to pass this ID into our next forms but first let's let's check this if it is working so now if I double click on this now it has thrown an exception so column name student ID cannot be found now if we go here or stop this and rerun this so we have my spell this is a student ID so I'll go to austral position and fix this so for that I will come here and we will go and I will use uh T here and now I'll execute this now it is successfully executed so now if I run this again stop this and run this and now if I double click on this now it is working so I'll close it now we will create properties in our registration form so I'll go to this and to its code and now here we will create our property so first I will declare two variables private uh and student ID and I'll use underscore with it so underscore student ID and it will be equal to zero semicolon and then um private um Bowl underscore is update equal to equal to false semicolon and now we will create our property so public and this ddid and then we will use this and get uh semicolon and set semicolon now we will get our ah or use our property another property part is update so public is um update and here we will use curly braces and then uh we will use a git semicolon and set semicolon now our properties are created so we will go to our menu from [Music] dot Cs and here we will pass in the values so for that we will first create the object crop registration form so registration form this one and now equals new registration form and now I'll put semicolon here now we will show the registration forms or registration form dot show dialog so now addition so I'll put semicolon here now we will give passenger properties here so registration form dot um stdid equals to uh STD ID semicolon and registration form dot uh um is update equals to true semicolon so now we have passed in our properties now let's check it if it is working so now if I double click on this now you can see our registration form is opening and our data our ID is transferring here so now we will code here for uh getting data into this form so I'll close this and I will close this as well now we will go to our registration form so I'll come to this uh registration form dot c is now I'll go down to our form load event so here is our registration form so it is here so now here we will uh create another method which will load data into this registration form so I'll come here uh load public or private wide um load data for update and now we will put this now our method is created so now we will code here so we will create a data table here data table [Music] um student record equals to new data table and now we are getting now we are going to get the connection string so first we will be using um s cable connection uh let's give a little connection equals to new SQL connection and then we will use curly braces and we have to put small brackets here and we have to do pass the connection string connection string here now we will use the command from where the from where this connections string is coming it is we have globally in our past tutorial or the second one we have created it here now we'll go down using and is cable command uh CMD is equal to new s cable command and now we will pass in here the stored procedure as well as the connection string so first we will create the stored procedure so I'll come here and uh to this and here we will create a store procedure new store procedure and I'll select air this and backspace now we will create a store procedure so create Rock get student record by ID this is our precision and then if we have a any parameter to password user student ID which is integer and and now we will use the select query so for that we will use as here begin here and now the select query so I'll copy the query from here that we have created before so I'll copy this up to this I'll Ctrl C this and I'll Ctrl V it here now here we are getting student ID name father name uh skill in uh C sharp uh uh and we will remove this I'll remove this or I will create I will write a new one error we will come here and uh right click on this table and here is our table this is so I'll right click on this STD table and we will use select up 1000 records and from here I will copy this I'll copy this and now we will go to our query and then Ctrl V this and here I will use real uh STD underscore ID equals at student uh ID we will use n here but we have to use the select and now we are good to go so we will execute this now our store procedure is created so I'll copy this from here come to see this and now we will go here and uh I'll uh use it here double quotes and control V and now we will use the connection skill connection and now we will use this cable connection dot open and I'll put semicolon here now we will use CMD dot command type which is equal to command type dot stored procedure now we will provide the um on the parameter so parameter add am not param eters dot add with value and here we will pass in add um I'll copy the parameter from here this is the parameter it's student ID not within Without End Ctrl C this and now I'll go here and I'll control B it here as student ID comma and we will pass in the value which is uh this Dot STD ID now I'll put semicolon here now uh we will load this data or read in this data so it's cable and data reader reader uh equals to new s cable data reader and now not new schedule data editor but CMD dot execute reader and I'll put semicolon now we will load the data into our data table so uh student record dot uh dot load and now we will use reader and now I'll put semicolon here now this code is complete so data is loaded into our uh our reader so I will use here if this dot is update if this dotted update is true then do what then a load load data for update this method would be called so in this method will return as a data table so data table and at the end we will go and return um we will find the method and for that I will come here so this is the end so here I will return student a record semicolon now we have returned the student record table here so we will catch the data table here data table STD record equals now we will get the data from this data table and show it in our form as we have all the records now in this data table so now we will get uh we have only one record in this one row so we will get the row from this data table and uh we will get each column from that row so data row row equals to STD record Dot rows and as we have only one row so we'll pass in 0 and now I'll put semicolon here so now we have the row so now we will get the records from this our columns from this row so now I'll close this and now we will use a name uh underscore text box dot text equals to row and then we have a STD underscore name and then Dot two string and then now I'll put semicolon now we have the father name text box so if name artix box dot text equal to a row and STD underscore F name now we are these uh column names are coming from so as we get this uh this data from our table here so it is these column names STD name I a STD father name and this is canancy shop so uh so this data is coming in this row so now I'll copy this and go here now if I run this right now and now if I double click on this record now you can see it then we run and name and father name are coming into take its text boxes now we will ah append all the records into this registration form so I'll come here now we will we have the which record we have next we have a scale NC shop so I'll come here and is skilled and Rob Dot checked so I'll use uh equal to row and then uh scale in C sharp control V so this is the column name and now I'll put semicolon and now it is showing an error so what is the error Canada ring object to ball so we will convert it to Boolean so convert convert Dot two we have misspelled the convert more Dot to Boolean this one and I will enclose this in this now I'll backspace this and now all do this now if I execute this right now and now if I double click on this now scaled in C sharp naught now no and if I select this first one now you can see scale in C sharp is checked so now we will do the same part the rest of them as well so I'll come here I'll copy this rule see this and now enter Ctrl V8 and Ctrl width again so now and we will uh change this is killed in Java and we will copy the column name from here SQL in Java I'll copy it from here Ctrl C this and now we'll go here to our code and control V it here Ctrl V I'll backspace this one and similarly we will use this is killed in Python and we will do this and I will remove this and we will go here copy this come to see this and now we will Ctrl V8 here now the next thing we will do is uh we will convert the gender so uh the gender ID we are using is uh if I select this from this DB table STD table and now if I execute this now we have we are using gender ID one and two one and two one for male two for female so now first we will convert this to so uh gender BTN dot checked equals it is not gender ID it is a gender button it is a male BTN dot checked equal to so now we have to convert convert uh dot to Boolean and now we will use row Dot uh row and here we will use gender underscore ID and I'll put semicolon here now we will execute this now if I check this first record so now Mel is his math now foreign but female button is a now we will quote for female so female is not showing up so I'll go to design and click on this and check its name so if mail button I'll go here to registration so if map BTN dot checked and now we will use a convert Dot to Boolean I've used mod bracket but we do not need them so convert Dot uh to Boolean and here I will use row and here I will pass in the same gender underscore ID and now I'll put semicolon here now if I execute this and now if I double click this Sarah now female is checked and now if I close this and select this then veer now you can see female is checked so the problem is we have to convert this to end and we have to use uh oh we have to convert this into integer and then we have to use f else dot 2 and 16 and I will also change this to end uh 16th and then we will use uh fl's if we have used 0 1 for male female then we do not need to convert this to n16 and then use fls but we have converted it to uh we have used the one and two as IDs for uh male and female so that is why so we will go to our code so now this is showing error because uh it should return a Boolean value but it is a returning an objects or and close it in and this and we would use here equal to equal to 1 then return true question mark and two else as return false similarly we will do the same part this one as well so I'll enclose it first but here we will use equal to equal to 2 then return true question mark true and else false so now if I run this and now if I select this Sima so female and now if I select this then Veer now male so now we are good to go so now we will go for this and now I'll close this I'll close this as well now I'll go here and we will use the [Music] o B picker Dot value equals then we will use rho and then here we will pass in the value of DOB we will check the column if it is DOB yes it is DOB and we will dob and we will convert it to date time so convert convert Dot to date time and we will enclose this I'll put semicolon here and at the end I will use that Dot date and now we will check this now if I double click on this now you can see that is shown here now we will similarly we will use time so I'll close this I'll close this as well all uh copy this Crow see this and uh Ctrl V this and I'll make this uh admission time picker and then we will use at the end time of the day and now if I execute this so time of the day is showing error so we will remove this from here and now we will check this without any chain now if I double click on this now it is showing zero zero as time so if I close this and if I check the time here so it is 11 admission time is 11. 42 now if I click on this this is also showing 0 0 so now we will fix this close this close this as well so it is showing uh zero zero because we haven't changed the column name so I'll go here and copy the column name Ctrl C this and now all uh Ctrl V it here and Ctrl V and now I'll stop this and rerun this and now if I double click on this now it is showing error column and do not belong to table so I'll remove this because we have made some errors here so I'll remove this and I'll remove this as well and no Ctrl Z this Ctrl Z this Ctrl Z this and I'll remove this one only this one and now let's rerun this and let's check this and now if I double click column now you can see that the time is coming here now I'll close it down and now we get the district so now I'll close it down as well and now here we will use a so here we will use this stick combo box dot selected value what is not showing up Dot selected value this one equals to we will convert it to integer so convert dot 2 and 16 and here we will pass in the row and then we will pass in the column name which is District underscore ID and now I'll put semicolon here now let's check this so now if I double click on this now it is showing error because we have misspelled this District so I'll close this and I'll add in a t here and rerun this again now if I double click on this now push hour here we it is using the shower now if I double click on this now here we are selecting now share if I close this and if I show you and now if I click this dik column so if I double click now you can see the district is d i k so we have added all the values into our uh registration form for update so in the next tutorial we will use the update query and update this data 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,660
Rating: undefined out of 5
Keywords: Programming Guru, Guru Programming, programming, gurru, programminggurru, Load Records from Database in C# Windows Form for Update, Load Data From a Database Using C#, how to reload data from database, how to load data from database into win form, loading data to windows form from database in c#, C#, c# full tutorial, c# full course, c# database tutorial, c# database programming, c# tutorial for beginners, how to code in c#, coding in csharp, visual studio, ssms, sql server
Id: 0AchwmkiXhI
Channel Id: undefined
Length: 35min 46sec (2146 seconds)
Published: Thu Oct 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.