create registration form in c# windows application with database

Updated : Oct 19, 2019 in Articles

how to create registration form in c# windows application with database

In this article, we will learn about how to create registration form in c# windows application with the database and you will create a user table in the database

Go to visual studio create a window Application Project then After you will design the two forms one is for login and another one is for registration

create registration form in c# windows application with database
create registration form in c# windows application with database

Note: Property name is required for Textboxes, buttons for calling purpose and generating events, property text name for a visual purpose (label, buttons…)

Before submitting the values first check the user name, password is empty if it is empty show the MessageBox Please provide username and password

Go to the App.config set the username and password for static under the <appsettings> key, values following the given code

<appSettings>
    <add key="AdminUserName" value="Admin"></add>
    <add key="AdminPassword" value="Admin"></add>
  </appSettings>

Static username and password read from app.config by using configurationManger AppSettings or you will handle from database

Double click on Submit button Add following give code inside the btnSubmit_Click event for Login form

The Following Given Code Admin login credentials are saved at app.config file statically and user credentials are validating through database

private void btnSubmit_Click(object sender, EventArgs e)
        {
            UserBO objUserBO = new UserBO();
            if (txtUserName.Text.Trim() != "" &amp;&amp; txtPassword.Text.Trim() != "")
            {
                //Checking the Admin and username
                string adminUserName = System.Configuration.ConfigurationManager.AppSettings["AdminUserName"];
                string adminPassword = System.Configuration.ConfigurationManager.AppSettings["AdminPassword"];

                if (txtUserName.Text.ToLower() == adminUserName.ToLower() &amp;&amp; txtPassword.Text.Trim() == adminPassword)
                {
                    Helper.Role = "Admin";
                    Helper.UserId = -1;
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    int pkuserid = objUserBO.AuthenticateUser(txtUserName.Text.Trim(), txtPassword.Text.Trim());
                    if (pkuserid != 0)
                    {
                        Helper.Role = "user";
                        Helper.UserId = pkuserid;
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("Please provide the valid credentials");
                    }
                }
            }
            else
            {
                MessageBox.Show("Please provide username and password");
            }
        }

AuthenticateUser() method will check the user is authenticated or not

     public int AuthenticateUser(string userName, string password)
        {
            return UserDB.AuthenticateUser(userName, password);
        }
public static int AuthenticateUser(string userName, string password)
        {
            try
            {
                string spName = "spAuthenticateUser";
                SqlParameter pPKUserId = new SqlParameter("@PKUserId", SqlDbType.Int);
                SqlParameter pUserName = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
                SqlParameter pPassword = new SqlParameter("@Password", SqlDbType.VarChar, 50);
                pPKUserId.Direction = ParameterDirection.Output;
                pUserName.Value = userName;
                pPassword.Value = password;
                SqlHelper.ExecuteNonQuery(Helper.ConnectionString, CommandType.StoredProcedure, spName, pUserName, pPassword, pPKUserId);
                if (pPKUserId.Value == DBNull.Value)
                    return 0;
                return Convert.ToInt32(pPKUserId.Value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Double click on Registration Here Label add the following code for create registration form in c# windows application

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            User objUser = new User();
            UserBO objUserBo = new UserBO();
            UserDialog objUserDialog = new UserDialog();
            if (objUserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                UserBO objUserBO = new UserBO();
                //if (objUserBO.IsUserExist(objUserDialog.UserName))
                //MessageBox.Show("UserName is Already Exist");
                objUserBo.InsertUser(objUserDialog.UserName, objUserDialog.Password, objUserDialog.FirstName, objUserDialog.Lastname, objUserDialog.EmailId, objUserDialog.PhoneNo, objUserDialog.IsActive);
                MessageBox.Show("Registred Sucessfully");
                Helper.Role = "Visitor";
            }
        }

In registration ok button event Add the given below code, we will validate all the fields with the help of error provider

  private void btnOk_Click(object sender, EventArgs e)
        {

            if (txtUserName.Text.Trim() == "")
            {
                epUserDialog.SetError(txtUserName, "Please Provide The UserName");
            }
            else if (txtPassword.Text.Trim() == "")
            {
                epUserDialog.SetError(txtPassword, "Please Provide The Password ");
            }
            else if (txtFirstName.Text.Trim() == "")
            {
                epUserDialog.SetError(txtFirstName, "Please Provide The FirstName");
            }
            else if (txtEmailId.Text.Trim() == "")
            {
                epUserDialog.SetError(txtEmailId, "Please Provide The EmailId");
            }
            else if (!Regex.IsMatch(txtEmailId.Text.Trim(), "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
            {
                epUserDialog.SetError(txtEmailId, "Enter correct Email Id ex:[email protected]");
            }

            else if (txtPhoneNo.Text.Trim() == "")
            {
                epUserDialog.SetError(txtPhoneNo, "Please Provide The PhoneNumber");
            }
            else
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

insertUser() method for registration new user Add given below code

public void InsertUser(string userName, string password, string firstName, string lastName, string emailId, string phoneNo, bool isActive)
        {
            try
            {
            User objUser = new User(-1, userName, password, firstName, lastName, emailId, phoneNo, isActive);
            objUser.Validate();
            UserDB.ManageUser(objUser, ActionType.Add);
           }
            catch (System.Data.SqlClient.SqlException )
            {
                throw ;
            }
        }

pass the user object and actiontypes(Enum type) like insert and update delete

 public static void ManageUser(User objUser, ActionType action)
        {
            try
            {
                SqlParameter pActionType = new SqlParameter("@ActionType", SqlDbType.TinyInt);
                SqlParameter pPKUserId = new SqlParameter("@PKUserId", SqlDbType.Int);
                SqlParameter pUserName = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
                SqlParameter pPassword = new SqlParameter("@Password", SqlDbType.VarChar, 50);
                SqlParameter pFirstName = new SqlParameter("@FirstName", SqlDbType.VarChar, 50);
                SqlParameter pLastName = new SqlParameter("@LastName", SqlDbType.VarChar, 50);
                SqlParameter pEmailId = new SqlParameter("@EmailId", SqlDbType.VarChar, 50);
                SqlParameter pPhoneNo = new SqlParameter("@PhoneNo", SqlDbType.VarChar, 50);
                SqlParameter pIsActive = new SqlParameter("@IsActive", SqlDbType.Bit);
                pPKUserId.Value = objUser.UserId;
                pUserName.Value = objUser.UserName;
                pPassword.Value = objUser.Password;
                pFirstName.Value = objUser.FirstName;
                pLastName.Value = objUser.LastName;
                pEmailId.Value = objUser.EmailId;
                pPhoneNo.Value = objUser.PhoneNo;
                pIsActive.Value = objUser.IsActive;
                pActionType.Value = Convert.ToInt32(action);
                if (action == ActionType.Add)
                {
                    pPKUserId.Direction = ParameterDirection.Output;
                    pPKUserId.Value = -1;
                }
                SqlHelper.ExecuteNonQuery(Helper.ConnectionString, CommandType.StoredProcedure, "spManageUserDetails", pActionType, pPKUserId, pUserName, pPassword, pFirstName, pLastName, pEmailId, pPhoneNo, pIsActive);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Latest posts by DuttaluruVijayakumar (see all)
Like
Like Love Haha Wow Sad Angry
16

Subscribe
Notify of
guest
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Cody Bellomo
Cody Bellomo
April 16, 2020 9:40 pm

First time visiting your website, I enjoy your web site!

Suraj
May 10, 2020 7:11 pm
Reply to  Cody Bellomo

Ya nice articles

Arturo Gabrial
April 19, 2020 10:54 pm

Bookmarked!, I like your web site!

Manuel Dolle
Manuel Dolle
April 21, 2020 6:41 pm

First time visiting your website, I enjoy it!

3M 3200
April 24, 2020 2:52 pm

I’m really pleased to say it was an interesting post to read.

Best regards,
Mead Zacho

Caitlin Dacunha
Caitlin Dacunha
April 28, 2020 4:16 am

Bookmarked!, I like your website!

6
0
Would love your thoughts, please comment.x
()
x