Windows form
Windows form is used to build rich, interactive user interfaces in Windows applications.
Here I am creating a login page, the user will successfully login if user details are stored in database otherwise it will give an error message. I have used Visual Studio 2013 and sql server 2012 to build this application.
Here are the steps:
Step 1
First create the table and insert user login credentials.
Query for creating the table
- CREATE TABLE [dbo].[UserLogins](
- [id] [int] IDENTITY(1,1) NOT NULL,
- [UserName] [varchar](100) NULL,
- [Password] [varchar](50) NULL,
- CONSTRAINT [PK_UserLogins] PRIMARY KEY CLUSTERED
- (
- [id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
And then insert some values: username and password fields
Step 2
Create a Windows form application, using label, textbox and button from Toolbox .
Step 3 - on click Login button it will go in .cs file.
Here code for sql connection string and check for valid user. If log in is successful it will go to another page otherwise it will show an error message in the message box.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsServiceSchedularApp {
- public partial class Form1: Form {
- public Form1() {
- InitializeComponent();
- }
- private void label1_Click(object sender, EventArgs e) {}
- private void button1_Click(object sender, EventArgs e) {
-
- SqlConnection con = new SqlConnection(@ "Data Source=STL-4110;Initial Catalog=Practice;Integrated Security=True");
- SqlCommand cmd = new SqlCommand("select * from Userlogins where UserName=@UserName and Password =@Password", con);
- cmd.Parameters.AddWithValue("@UserName", textBox1.Text);
- cmd.Parameters.AddWithValue("@Password", textBox2.Text);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
-
- con.Open();
- int i = cmd.ExecuteNonQuery();
- con.Close();
- if (dt.Rows.Count > 0) {
- MessageBox.Show("Successfully loged in");
-
- WelcomePage settingsForm = new WelcomePage();
- settingsForm.Show();
- } else {
- MessageBox.Show("Please enter Correct Username and Password");
- }
- }
- }
- }
I have uploaded application as an attachment.