Create Login and Register Forms Using C++ and Visual Studio 2022 with SQL Server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video i will show you how to create login and register forms using visual studio 2022 c plus plus the.net framework and sql server so it is necessary to download and install the sql server now let's click on create a new project and here let's select c now here we need to find the clr c project so this is the option that we need to select it is clr empty project with the net framework so if you cannot find this option you need to scroll down and you need to click on install more tools and features then in this window we need to select this component it is called desktop development with c plus and here we need to expand desktop development with c plus and we need to select this option it is called c plus plus cle support then we need to click on modify but in my case everything is installed correctly so i will just close this window so this is the option that you need to select it is clr empty project weather.net framework so let's select it and let's click on next in this window we need to provide the project name so i will call it my restaurant project now let's click on create now the project has been created so let's configure it so let's click on project then properties and here let's select all configurations and for the platform let's select x64 now let's expand linker and let's select system then in the subsystem let's select windows then let's select advanced and in the entry point let's write main now let's click on ok now we need to create the database so let's click on view then server explorer and here let's create a new connection so let's click on this button so here i will select microsoft sql server also it is possible to uncheck this box now let's click on continue so i will connect to the server that is installed on my computer so first let's write localhost then we need to provide the instance name in my case it is sql express now we can test the connection using this button and here we can see that the connection is successful now we can select the database using this button and here we have the list of the available databases but i want to create a new database called my restaurant so here let's write my restaurant now let's click on okay so here we can confirm that we want to create the database so let's click on yes now the connection has been established so let's expand it and here we don't have any table so let's create a new one so let's make a right click then add the new table so i can modify the table name and i will call it users by default it contains the id which is a primary key so let's add a new column and i will call it name it will be of type varchar and you can modify the length so let's write 100. let's add a new column and let's call it email and let's add the password so for the name it should not be null the same for the email and the password should not be null also i want that the id be auto incremented so let's select it and here in the property i have to find identity so this is identity let's expand it and let's modify it to true also for the email i want it to be unique so this is email let's add unique now to create this table we have to click on update and here let's click on update database and here we can see that the table has been created so we can close this page and you can refresh the table's note so here we can see that we have this table now let's add a row into this table so let's make a right click then show table data and here we can add a new row so we don't need to provide the id because it is auto incremental so let's provide the name and the email let's hit enter and this row has been added so here we can see that the first id is one now let's close this page we can also hide the server now let's create the login form so we can make a right click on the project then add the new item and in this window we have to select ui for user interface then windows form and i will call it login for let's click on add so here we have this error we need just to close this page and we need to restart visual studio so let's open the previous project now let's open this form so this is the user interface we can increase the font so let's select this window and let's go to font so here i will select 16 as the size now let's add a label so let's click on toolbox and also we can make this toolbox visible so this is the label and let's modify the text of this label so let's modify the text property and let's write login also i want to provide this label with the full width so let's disable the auto size of this label and let's put this text in the middle so let's modify the text alignment let's select center we can also modify the font of this label so let's select font and let's select this value now let's add a new label so let's modify the text and let's write email then let's add a text box so i want to modify the object name of this text box i want to call it tb like textbox email so let's find the name property and let's call it tb email now let's add a new label then let's add a new text box then let's modify the object name so in the name property i will modify the name of the object and i will call it tb password also this text box is used for the password so i need to hide the characters so in the password car i will use asterisk now let's add two buttons now let's modify the text of the two buttons so let's select this one and let's modify the text let's write ok and here let's write cancel also let's modify the object name so in the name property i will call this button btn okay and for this one it will be btn console we can also modify the background color so let's select the window and let's modify the back color [Music] so i can select this color for example we can also modify the high a little bit we can also modify the title now let's add a listener to these two buttons let's start with the cancel button so let's make double click on this button so this method will be executed when we click on the cancel button what i want to do here is to close the window now let's go back to the designer and let's add a listener to this ok button so this method will be executed when we click on the ok button what i want to do here is to read the text of the email and the password and to check if we have a user in the database or not so first let's read the email and the password then let's check if any of these two fields is empty so if the email is empty or the password is empty we have to display an error message and also we need to exit this function otherwise we can connect to the database to check if the email and the password are correct or not so in the try we will try to connect to the database and if we have any error when we try to connect to the database then we have to display an error message so first we need the connection string we can obtain the connection string from the server explorer so let's select this connection and here we can copy this connection string and let's paste it here of course here we need to add second slash then let's establish the connection so here we have an error because we need to add a new namespace so we need to add this namespace and here we can see that we don't have the error anymore now let's create the sql query so in this query we will select all the rows from this table where the email is this value and the password is this value now we need to replace this value by the email and this value by the password so here i will replace this variable in this query by the email and also i will replace this variable in this query by the password now let's execute this query so if we find the user in the database then we can read the data of this user and then we can close this window otherwise we need to display an error message so this error message is email or password is incorrect now let's create a new class called user which will store the data of the authenticated user so let's make a right click on the project name then add the new item and i will create this new class into a header file so let's select header file and let's call it user.h so this class has these public variables which correspond to the column names of the users table of the database now let's add this header file to the login form so if we find the user in the database then we can store the data of this authenticated user into an object of type user so i will create a global variable called user so i want it to be public and it is of type user so i will call it user and also i want to initialize it with the value null so this is a global variable which is public which means that we can access it from the outside of this class now let's initialize it if we find the user in the database now to test the application we need to create the main method so let's create a new c plus file that contains the main method let's select c plus plus file and let's call it program.cpp so in this file first we will include the login form then we will initialize the application and we will create the login form then we will show the login form as a dialog this means that we will wait till the end of the authentication when the login form is closed we can read the user object if the user object is not null this means that the authentication is successful in this case we will show the message successful authentication of and the name of the authenticated user otherwise if the user object is null then we will show the message authentication cancelled now let's run the application so here we obtain this window let's click on ok and here we have this error message please enter email and password so let's provide wrong email and let's provide the password let's click on ok and here we have email or password is incorrect so let's correct the email and let's click on ok and here we have the message successful authentication of bill gates and this message has been shown from the file program.cpp let's click on ok and let's run the application again this time let's click on cancel and here we have authentication cancelled from the file program.cpp now let's click on ok now let's create the dashboard so here let's select user interface and let's select windows 4 and here i will call it mainform so let's close this page and let's restart visual studio let's select the previous project now let's open this main form so let's increase the size and let's add two labels now let's disable the auto size of this label so in the auto size i will select false now i can provide this label with the full width and also let's increase the font of this label also let's show this label in the middle of the window so let's modify the text line property and let's select this area now we can modify the text so let's write welcome to dashboard also we can modify the anchor property of this label so its width will be modified when we modify the width of the window so this is the anchor property we need to select this area now let's test the behavior of this label so we can see that the width of this label will be modified when we modify the width of the window now let's modify the font of the second label also i want to modify the object name of this label so let's find the name property so let's call it lb like label user info now let's go to the source code of this form so let's make a right click then view code so here we have this constructor i will modify it to provide it with a parameter of type user and also i need to include the header file now i will use this object to initialize the label so after the initialize method i can initialize the label just here so this label will show the information of the authenticated user now let's go to program.cpp and let's modify it so if the user object is not null then we need to create the main form so let's remove this and let's create the main form but here we have this error because we need to include the header file now let's run the application so let's login using the correct email and password and let's click on ok and we obtain this dashboard we can increase its width and you can see in this label that we have the information of the authenticated user now let's create the register form so in this window let's select ui then windows 4 and i will call it register form now let's modify the font size here let's select 16 we can also modify the size and let's add some components so first let's add a label now let's disable the auto size of this label so in the auto size let's select false and now we can modify the size also let's set this text in the center so in the text line i will select center also it is possible to increase the font size now let's modify the text of this label so let's write register we can also modify the title so let's select the window then let's go to the text property now let's add other labels and text boxes let's modify the text of this label and let's write name now let's add a text box now we can copy this label and this text box so we have just to select both of them then we need to click on the control button and we need to drag these two components let's do the same thing again now let's modify the text of these labels then let's modify the name of these different text boxes so let's select this one and let's call it tb like text box name so we have to find the name property this one let's call it tbemail then tb phone this is tb address this one is tb password and finally this is tb confirm password so these two text boxes will be used for the password so i need to hide the password so let's select this first text box then in the password car we have to write the character that will be used instead of the password so i will write star let's do the same thing with this second text box and in the password car let's write star now let's add two buttons then let's modify the text and the name of these two buttons so for the text of the first button let's write ok and for the second button let's write cancel then let's modify the name so for the name of the console button it will be btn console and for the ok button i will call it btn ok now i need to add a link that allows the user to switch to the login form so i need to add this component it is called link label and let's modify the text to write login also i need to modify the name of this component so i will call it ll like link label login we can also change the background color so let's select this window then let's go to the back color property let's select custom and let's select this color for example we can also disable resizing the window to do this we have to go to form border style and here let's select fixed single also i want to show this window in the middle of the screen so let's go to the source code so to go to the source code i have to make a right click then view code and in the constructor i have to call the following function so it is called center to screen now let's go back to the designer and let's add a listener to this cancel button so let's make double click on this button so this method will be executed when we click on the cancel button what i want to do here is to close the window so let's call the close method now let's go back to the designer and let's add a listener to this login link so let's make double click on this link so this method will be executed when we click on the login link what i want to do here is to inform the application that the user wants to login so let's create a global variable called switch to login so i want it to be public because i want to access it from the outside of this class it is of type boolean and i will call it switch to login and i will initialize it with the value false so by default this variable is initialized to false because by default the user does not like to switch to login but if the user clicks on the login link this means that the user wants to switch to login so in this function we have to set this variable to true then we have to close the register for now let's go back to the designer and let's add a listener to this ok button so let's make double click on this ok button so this method will be executed when we click on the ok button what i want to do here is to read the content of the different text boxes and to add a new user to the database so first let's read the content of the different text boxes so here i will read the content of the different text boxes if any field is empty i have to show an error message and i have to exit this function so if any field is empty i will show this message please enter all the fields now i need to check that the password and the confirm password are identical so if the password and the confirm password does not match i have to show this error message and i have to exit this function else i can connect to the database so if i fail to connect to the database then i have to show this error message and here in the try i need to connect to the database so first let's create the connection string so i can obtain the connection string from the server explorer so i have to select this connection and in the properties i have to copy this connection string so let's copy it and let's paste it just here of course i need to add a second slash here now let's open the connection so here we have an error because we need to add a new namespace so we need to use this namespace which is called sqlclient and here we can see that we don't have the error anymore now let's create the sql query so this query allows us to add a new user into the table users so i will add these variables into these columns now i need to replace these variables by the corresponding values so here i will replace these variables of this sql query by these strings that i obtained from the different text boxes now let's execute this sql query then we need to save the data of the user into an object of type user which will be returned to the main method so let's create a global variable of type user so i will create it just here and it will be public because i need to access it from the outside of this class so it is of type user and i will call it user of course i will initialize it with null so here we have an error because i did not include the header file so let's include it now let's initialize this object so i will initialize it after executing the sql query and finally let's close this register form now let's modify the login form so let's go to login form dot h and here let's add a link that allows the user to switch to the register form so let's add this link label and let's modify the text of this label let's write register also let's modify the variable name so let's select it and let's find the name property so here i will call it ll register now let's add a listener to this link so let's make doubly click on it so this method will be executed when we click on the register link what i want to do here is to notify the application that the user wants to register so let's create a global variable of type boolean that is called switch to register so i want it to be public because i need to access it from the outside of this class and it is of type boolean i will call it switch to register and i will initialize it with false so when the user clicks on the register link i will modify the value of this boolean then i need to close this login form now let's update the main method so let's go to program.cpp and here we can remove all of this and we can create an object of type user now let's create a while loop so first of all in this while loop we need to show the login for so when the login form is closed we need to check if the user wants to switch to register so if the user wants to switch to register then we have to show the register for and of course we need to include the header file of the register form so if the user wants to switch to the register form we have to show the register form else we have to read the user object from the login form and we have to exit this while loop so when we show the register form to the user we have to check if the user wants to switch back to the login for so if the user wants to switch back to the login form then we have to call continue which allows us to go back to the beginning of this while loop else we have to read the user object and we have to exit this while loop now let's run the application so let's click on a register and we obtain this register form let's click on login then register then login and you can see that it works fine now let's register a new user so if i click on ok i will obtain this error message so let's provide all the other fields so if i provide 2 passwords which do not match then i click on ok i obtain this error let's click on ok and here we can see that the new user has been registered and you obtain this dashboard now let's close this application and let's run it again now let's login using the new account and we obtain this dashboard
Info
Channel: BoostMyTool
Views: 81,144
Rating: undefined out of 5
Keywords:
Id: huCyyUVYk3g
Channel Id: undefined
Length: 39min 47sec (2387 seconds)
Published: Fri Jan 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.