Showing posts with label WinForm. Show all posts
Showing posts with label WinForm. Show all posts

Thursday, July 1, 2021

Winform Registration Form basic code





Register.cs

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace WinFormsAppx1
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }
        private void Register_Load(object sender, EventArgs e)
        {
            listBoxHobbies.Items.Add("Cricket");
            listBoxHobbies.Items.Add("Net Surfing");
            listBoxHobbies.Items.Add("Science");
            listBoxHobbies.Items.Add("Playing Cards");
            listBoxHobbies.Items.Add("Poetry");
            listBoxHobbies.Items.Add("Painting");
            listBoxHobbies.Items.Add("Dance");
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox comboBox1 = (ComboBox)sender;
            int comboIndex = comboBox1.SelectedIndex;
            string comboItem = comboBox1.SelectedItem.ToString();
            //MessageBox.Show(comboItem);
        }

        private void btnPicture_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFiledlg = new OpenFileDialog();
            openFiledlg.Title = "Select Image";
            string strInitialDir = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            openFiledlg.InitialDirectory = strInitialDir;
            openFiledlg.Filter = "Image files (*.png)|*.png|All files (*.*)|*.*";
            DialogResult dlgResult =  openFiledlg.ShowDialog();
            if (dlgResult.Equals(DialogResult.OK))
            {
                string filename = openFiledlg.FileName;
                pictureBox1.ImageLocation = filename;
                pictureBox1.Image = new Bitmap(filename);
            }

        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
             ListBox listBox1 = (ListBox)sender;
            string curItem = listBox1.SelectedItem.ToString();
            int indexSelected = listBox1.SelectedIndex;
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
                string username = txtUsername.Text;
                string email = txtEmail.Text;
                string password = txtPassword.Text;
                string confirmPassword = txtConfirmPassword.Text;
            try
            {
                string qualification = cboQualification.SelectedItem.ToString();
                string gender = groupBox1.Controls.OfType().FirstOrDefault(r => r.Checked).Text;
                string hobbies = listBoxHobbies.SelectedItem.ToString();
                string imgpath = pictureBox1.ImageLocation;
            }
            catch (NullReferenceException ex)
            {

                MessageBox.Show("Fill or select all controls.",ex.Message);
            }
        }


    }
}
Register.Designer.cs

namespace WinFormsAppx1
{
    partial class Register
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.txtUsername = new System.Windows.Forms.TextBox();
            this.txtEmail = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.txtPassword = new System.Windows.Forms.TextBox();
            this.txtConfirmPassword = new System.Windows.Forms.TextBox();
            this.optionMale = new System.Windows.Forms.RadioButton();
            this.optionFemale = new System.Windows.Forms.RadioButton();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.listBoxHobbies = new System.Windows.Forms.ListBox();
            this.cboQualification = new System.Windows.Forms.ComboBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.btnPicture = new System.Windows.Forms.Button();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.btnSave = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(30, 62);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(67, 17);
            this.label1.TabIndex = 0;
            this.label1.Text = "Username";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(30, 128);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(64, 17);
            this.label2.TabIndex = 0;
            this.label2.Text = "Password";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(30, 95);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(39, 17);
            this.label3.TabIndex = 0;
            this.label3.Text = "Email";
            // 
            // txtUsername
            // 
            this.txtUsername.Location = new System.Drawing.Point(124, 62);
            this.txtUsername.Name = "txtUsername";
            this.txtUsername.Size = new System.Drawing.Size(200, 25);
            this.txtUsername.TabIndex = 1;
            // 
            // txtEmail
            // 
            this.txtEmail.Location = new System.Drawing.Point(124, 94);
            this.txtEmail.Name = "txtEmail";
            this.txtEmail.Size = new System.Drawing.Size(200, 25);
            this.txtEmail.TabIndex = 2;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(30, 161);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(64, 17);
            this.label4.TabIndex = 0;
            this.label4.Text = "Password";
            // 
            // txtPassword
            // 
            this.txtPassword.Location = new System.Drawing.Point(124, 126);
            this.txtPassword.MaxLength = 20;
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.PasswordChar = '*';
            this.txtPassword.Size = new System.Drawing.Size(200, 25);
            this.txtPassword.TabIndex = 3;
            // 
            // txtConfirmPassword
            // 
            this.txtConfirmPassword.Location = new System.Drawing.Point(124, 158);
            this.txtConfirmPassword.MaxLength = 20;
            this.txtConfirmPassword.Name = "txtConfirmPassword";
            this.txtConfirmPassword.PasswordChar = '*';
            this.txtConfirmPassword.Size = new System.Drawing.Size(200, 25);
            this.txtConfirmPassword.TabIndex = 4;
            // 
            // optionMale
            // 
            this.optionMale.AutoSize = true;
            this.optionMale.Location = new System.Drawing.Point(94, 14);
            this.optionMale.Name = "optionMale";
            this.optionMale.Size = new System.Drawing.Size(55, 21);
            this.optionMale.TabIndex = 5;
            this.optionMale.TabStop = true;
            this.optionMale.Text = "Male";
            this.optionMale.UseVisualStyleBackColor = true;
            // 
            // optionFemale
            // 
            this.optionFemale.AutoSize = true;
            this.optionFemale.Location = new System.Drawing.Point(165, 14);
            this.optionFemale.Name = "optionFemale";
            this.optionFemale.Size = new System.Drawing.Size(67, 21);
            this.optionFemale.TabIndex = 6;
            this.optionFemale.TabStop = true;
            this.optionFemale.Text = "Female";
            this.optionFemale.UseVisualStyleBackColor = true;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.optionFemale);
            this.groupBox1.Controls.Add(this.optionMale);
            this.groupBox1.Location = new System.Drawing.Point(30, 220);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(294, 43);
            this.groupBox1.TabIndex = 7;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Gender";
            // 
            // listBoxHobbies
            // 
            this.listBoxHobbies.FormattingEnabled = true;
            this.listBoxHobbies.ItemHeight = 17;
            this.listBoxHobbies.Location = new System.Drawing.Point(124, 269);
            this.listBoxHobbies.Name = "listBoxHobbies";
            this.listBoxHobbies.Size = new System.Drawing.Size(200, 89);
            this.listBoxHobbies.TabIndex = 8;
            this.listBoxHobbies.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
            // 
            // cboQualification
            // 
            this.cboQualification.FormattingEnabled = true;
            this.cboQualification.Items.AddRange(new object[] {
            "B.A.",
            "B.Sc.",
            "M.A.",
            "Ph.D."});
            this.cboQualification.Location = new System.Drawing.Point(124, 189);
            this.cboQualification.Name = "cboQualification";
            this.cboQualification.Size = new System.Drawing.Size(200, 25);
            this.cboQualification.TabIndex = 9;
            this.cboQualification.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.SystemColors.ButtonHighlight;
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox1.Location = new System.Drawing.Point(364, 62);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(165, 121);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.pictureBox1.TabIndex = 10;
            this.pictureBox1.TabStop = false;
            // 
            // btnPicture
            // 
            this.btnPicture.Location = new System.Drawing.Point(411, 203);
            this.btnPicture.Name = "btnPicture";
            this.btnPicture.Size = new System.Drawing.Size(118, 28);
            this.btnPicture.TabIndex = 8;
            this.btnPicture.Text = "Select Image";
            this.btnPicture.UseVisualStyleBackColor = true;
            this.btnPicture.Click += new System.EventHandler(this.btnPicture_Click);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(30, 192);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(80, 17);
            this.label5.TabIndex = 11;
            this.label5.Text = "Qualification";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Font = new System.Drawing.Font("Segoe UI", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.label6.Location = new System.Drawing.Point(183, 15);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(203, 28);
            this.label6.TabIndex = 8;
            this.label6.Text = "REGISTRATION FORM";
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(421, 324);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(108, 34);
            this.btnSave.TabIndex = 12;
            this.btnSave.Text = "Save";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(421, 284);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(108, 34);
            this.btnCancel.TabIndex = 12;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // Register
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(555, 432);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.listBoxHobbies);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.btnPicture);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.cboQualification);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.txtConfirmPassword);
            this.Controls.Add(this.txtPassword);
            this.Controls.Add(this.txtEmail);
            this.Controls.Add(this.txtUsername);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label1);
            this.Name = "Register";
            this.Text = "Save";
            this.Load += new System.EventHandler(this.Register_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox txtUsername;
        private System.Windows.Forms.TextBox txtEmail;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox txtPassword;
        private System.Windows.Forms.TextBox txtConfirmPassword;
        private System.Windows.Forms.RadioButton optionMale;
        private System.Windows.Forms.RadioButton optionFemale;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.ListBox listBoxHobbies;
        private System.Windows.Forms.ComboBox cboQualification;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Button btnPicture;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.Button btnCancel;
    }
}

C# Winform Changing startup form




Inside the Program.cs file, chnage the form name in
Application.Run(new frmLogin()); to
Application.Run(new Register());

using System;
using System.Windows.Forms;

namespace WinFormsAppx1
{
    static class Program
    {
        /// 
        ///  The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmLogin());
        }
    }
}



Edited on 6th July 2023

Wednesday, June 30, 2021

Winform - Login Form and ConnectionString in app.config file





Learning Points
  • Hard code connection string or,
  • Add connection string in App.config file and read it using ConnectionManager of System.Configuration namespace. If System.Configuration is missing in your application, download it from NuGet package.
  • If App.config is missing, create it and name it App.config.

loginForm.cs
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WinFormsAppx1
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();

        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            txtUsername.TabIndex = 0;
            txtPassword.TabIndex = 1;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            validateTextBoxes();
        }

        private void validateTextBoxes()
        {
            if (String.IsNullOrEmpty(txtUsername.Text))
            {
                MessageBox.Show("Username is required.");
            }
            else if (String.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show("Password is required.");
            }
            else
            {
                
            }
            //check username and password
            //store logging time
            int success = StoreDataIntoTable(txtUsername.Text, txtPassword.Text);
            if (success==1)
            {
                MessageBox.Show("Data Inserted.");
                Application.Exit();
            }
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }

App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="cs" connectionString="Data Source=IN-AJEET-LT\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True" providerName="system.Data.SqlClient"/>
  </connectionStrings>
</configuration>

private int StoreDataIntoTable(string user, string passwd)
        {
            // 1st Approach, ConnectionString: Hard coded
            //string connstring = @"Data Source=IN-AJEET-LT\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True";
            // 2nd Approach, ConnectionString: inside App.config file
            string connstring = ConfigurationManager.ConnectionStrings["cs"].ConnectionString;
            using (SqlConnection sqlConnection = new SqlConnection(connstring))
            {
                sqlConnection.Open();
                string commandText = "INSERT INTO logger values('" + user + "', '" + passwd + "', '" + DateTime.Now + "')";
                SqlCommand command = new SqlCommand(commandText, sqlConnection);
                command.ExecuteNonQuery();
                return 1;
            }

        }

3rd Approach, Stored Procedure: Replace the above method by following method. Instead of using the CommandText, we use the stored procedure created inside the SQL Server database. Stored procedures provides better security as the database code is stored separately in the SQL Server instead of the C# code.

Create the following procedure as SQL query and execute it to create stored procedure object.

CREATE procedure [dbo].[Logg] (
@username nvarchar(30), 
@password nvarchar(30),
@logDate nvarchar(30)
)
As
INSERT INTO logger values(@username, @password, @logDate)
NOTE: The stored procedure variables are used inside the C# code. They are called Parameters of Command.


private int StoreDataIntoTable(string user, string passwd)
        {
            // 1st Approach, ConnectionString: Hard coded
            //string connstring = @"Data Source=IN-AJEET-LT\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True";
            // 2nd Approach, ConnectionString: inside App.config file
            string connstring = ConfigurationManager.ConnectionStrings["cs"].ConnectionString;
            using (SqlConnection sqlConnection = new SqlConnection(connstring))
            {
                //sqlConnection.Open();
                //string commandText = "INSERT INTO logger values('" + user + "', '" + passwd + "', '" + DateTime.Now + "')";
            //Stored procedure instead of hard coded command inside C# code
                
                SqlCommand command = new SqlCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "Logg";
                command.Parameters.AddWithValue("@username", user);
                command.Parameters.AddWithValue("@password", passwd);
                command.Parameters.AddWithValue("@logDate", DateTime.Now);
                sqlConnection.Open();
                command.Connection = sqlConnection;
                command.ExecuteNonQuery();
                return 1;
            }

        }

loginForm.Designer.cs
namespace WinFormsAppx1
{
    partial class frmLogin
    {
        /// 
        ///  Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        ///  Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmLogin));
            this.pictureBoxLogin = new System.Windows.Forms.PictureBox();
            this.txtUsername = new System.Windows.Forms.TextBox();
            this.txtPassword = new System.Windows.Forms.TextBox();
            this.btnLogin = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.btnCancel = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxLogin)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBoxLogin
            // 
            this.pictureBoxLogin.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxLogin.Image")));
            this.pictureBoxLogin.Location = new System.Drawing.Point(12, 40);
            this.pictureBoxLogin.Name = "pictureBoxLogin";
            this.pictureBoxLogin.Size = new System.Drawing.Size(100, 109);
            this.pictureBoxLogin.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.pictureBoxLogin.TabIndex = 0;
            this.pictureBoxLogin.TabStop = false;
            // 
            // txtUsername
            // 
            this.txtUsername.Location = new System.Drawing.Point(237, 40);
            this.txtUsername.Name = "txtUsername";
            this.txtUsername.Size = new System.Drawing.Size(171, 25);
            this.txtUsername.TabIndex = 1;
            // 
            // txtPassword
            // 
            this.txtPassword.Location = new System.Drawing.Point(237, 71);
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.Size = new System.Drawing.Size(171, 25);
            this.txtPassword.TabIndex = 1;
            // 
            // btnLogin
            // 
            this.btnLogin.Location = new System.Drawing.Point(237, 112);
            this.btnLogin.Name = "btnLogin";
            this.btnLogin.Size = new System.Drawing.Size(75, 26);
            this.btnLogin.TabIndex = 5;
            this.btnLogin.Text = "Login";
            this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(149, 43);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(67, 17);
            this.label1.TabIndex = 3;
            this.label1.Text = "Username";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(149, 75);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(64, 17);
            this.label2.TabIndex = 3;
            this.label2.Text = "Password";
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(333, 112);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 26);
            this.btnCancel.TabIndex = 4;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // frmLogin
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(432, 205);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnLogin);
            this.Controls.Add(this.txtPassword);
            this.Controls.Add(this.txtUsername);
            this.Controls.Add(this.pictureBoxLogin);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmLogin";
            this.Text = "Login Form";
            this.Load += new System.EventHandler(this.frmLogin_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxLogin)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBoxLogin;
        private System.Windows.Forms.TextBox txtUsername;
        private System.Windows.Forms.TextBox txtPassword;
        private System.Windows.Forms.Button btnLogin;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button btnCancel;
    }
}

What to do if the connection fails to Database server?
if you try to connect with the SQL Server using SSMS, and connection fails as shown below.
step1: Try to connect 


step2: Connection fail message


step3: Connection Restart: Open the "Services" windows. Search Services by pressing Winkey plus S in Windows10 and type Services. Note that Search bar appears in the bottom above the windows task bar, when Winkey+S is pressed.

step4: When the connection is restarted, run the C# code.



Hot Topics