Login and Logout Using C#.net and SQL Server 2005

In this tutorial, I will teach you how to create Login and Logout using C#.net and SQL Server 2005 Express Edition. This method is very helpful to ensure the security of the system from invaders who wants to access your system.

Let's begin:

1. Create a database in SQL Server 2005 Express Edition and name it "userdb". 2. Make a query for creating the table in the database.
  1. /****** Object:  Table [dbo].[tbluser]    Script Date: 07/11/2016 11:58:48 ******/
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. CREATE TABLE [dbo].[tbluser](
  7.         [ID] [INT] IDENTITY(1,1) NOT NULL,
  8.         [Name] [nvarchar](50) NULL,
  9.         [UNAME] [nvarchar](50) NULL,
  10.         [PASS] [nvarchar](MAX) NULL,
  11.         [UTYPE] [NCHAR](20) NULL,
  12.  CONSTRAINT [PK_tbluser] PRIMARY KEY CLUSTERED
  13. (
  14.         [ID] ASC
  15. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  16. ) ON [PRIMARY]
3. Make a query for inserting data in the table that you have made.
  1. INSERT INTO [dbo].[tbluser]
  2.            (Name
  3.            ,UNAME
  4.            ,PASS
  5.            ,UTYPE)
  6. VALUES ('Janno Palacios',janobe,janobe,'Administrator')
4. Open Microsoft Visual Studio and create a new windows form application for C#. 5. Do the Form just like this. fig 1 6. Go to the solution explorer and hit the "View Code" to display the code editor. fig 2 7. Declare all the classes and variables that are needed.
  1.    //initialize all classes
  2.         SqlConnection conn = new SqlConnection();
  3.         SqlCommand cmd = new SqlCommand();
  4.         SqlDataAdapter da = new SqlDataAdapter();
  5.         DataTable dt = new DataTable();
  6.  
  7.         //declaring variables
  8.         string strquery;
  9.         int  max;
8. Do the following codes for establishing the connection between SQL Server and C#.net.
  1. //set a connection between SQL server and Visual C#
  2. conn.ConnectionString = "Data Source=.\\SQLEXPRESS;database=userdb;trusted_connection=true;";
9. Do the following codes for the login method in the click event handler of a "Login" button
  1.            try
  2.            {
  3.                 conn.Open();
  4.                 //create a query for retrieving data in the database.
  5.                 strquery = "SELECT * FROM tbluser WHERE UNAME='" + txtUsername.Text + "' and PASS='" + txtPassword.Text + "'";
  6.                 //initialize new Sql commands
  7.                 cmd = new SqlCommand();
  8.                 //hold the data to be executed.
  9.                 cmd.Connection = conn;
  10.                 cmd.CommandText = strquery;
  11.                 //initialize new Sql data adapter
  12.                 da = new SqlDataAdapter();
  13.                 //fetching query in the database.
  14.                 da.SelectCommand = cmd;
  15.                 //initialize new datatable
  16.                 dt = new DataTable();
  17.                 //refreshes the rows in specified range in the datasource.
  18.                 da.Fill(dt);
  19.                 //getting the total row in the table.
  20.                 max= dt.Rows.Count;
  21.                 //validating the total rows in the table
  22.                 if (max > 0)
  23.                 {
  24.                     //popup message
  25.                     MessageBox.Show("Welcome " + dt.Rows[0].Field<string>("UTYPE"));
  26.                     //disable the group box.
  27.                     groupBox1.Enabled = false;
  28.                     //store the value in the Button
  29.                     btnLogin.Text = "Logout";
  30.  
  31.                     //clearing textboxes
  32.                     txtPassword.Clear();
  33.                     txtUsername.Clear();
  34.                 }
  35.                 else
  36.                 {
  37.                     //when the account does not exist, it will display this message.
  38.                     MessageBox.Show("Account does not exist.");
  39.                 }
  40.  
  41.  
  42.             }
  43.             catch (Exception ex)
  44.             {
  45.                 //catching error
  46.                 MessageBox.Show(ex.Message);
  47.             }
  48.             finally
  49.             {
  50.                 da.Dispose();
  51.                 conn.Close();
  52.             }
10. Finally, DO the following codes for the logout method.
  1.            if (btnLogin.Text == "Logout")
  2.             {
  3.                 groupBox1.Enabled = true;
  4.                 txtUsername.Clear();
  5.                 txtPassword.Clear();
  6.                 btnLogin.Text = "Login";
  7.             }
  8.             else
  9.             {
  10.                 groupBox1.Enabled = true  ;
  11.                 txtUsername.Clear();
  12.                 txtPassword.Clear();
  13.                 btnLogin .Text = "Login";
  14.             }
For all students who need a programmer for your thesis system or anyone who needs a source code in any programming languages. You can contact me @ : Email – [email protected] Mobile No. – 09305235027 – TNT

Add new comment