C# C# free Source codes C# Open source codes

Create Login , Update , change Password in C# Desktop application with Source code

In This tutorial you will Learn about how to Create Login in C# , How to Update your Password in C# Desktop application , How to Change Password in C# Desktop application step by Step . we are sharing Source code with you Guys as well 

Create Login 

  • Open Your Visual Studio with new App
  • Select C# Windows application 
  • Write Name of application 
  • Add application form 
  • Draw two text Boxes 
  • one button for Login 
  •  

  • Create Database in MSSQL 

  • make  connection with Database using App.Config Like This 
<connectionStrings>
<add name=”abc” connectionString=”Data Source=DESKTOP-3V7KQ4B\SQLEXPRESS;Initial Catalog=shop; Integrated Security=True”
providerName=”System.Data.SqlClient”/>
</connectionStrings>

Detail of connection string 

Add Name is  used to save connection in string 

Data Source  is used to save your SQL server information 

Intial catalog is used to save your Database name 

  • Now write Login Code 
  • write this code Behind the Button 
if(textBox1.Text ==”” ||textBox2.Text ==””)
{
MessageBox.Show(“Please provide UserName and Password”);
return;
}
try
{
//Create SqlConnection
string cs = ConfigurationManager.ConnectionStrings[“abc”].ConnectionString;
SqlConnection con = new SqlConnection(cs);

SqlCommand cmd = new SqlCommand(“Select * from login where username=@username and Password=@password”,con);
cmd.Parameters.AddWithValue(“@username”, textBox1.Text);
cmd.Parameters.AddWithValue(“@password”, textBox2.Text);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();

int count = ds.Tables[0].Rows.Count;
//If count is equal to 1, than show frmMain form
if (count == 1)
{

this.Hide();
shop_Detail f1 = new shop_Detail();
f1.Show();
}
else
{
MessageBox.Show(“Login Failed!”);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Now Update password 

  • Update Password Form will be like this 

  • Draw three text boxes one for Old Password , New Password , confirm password and one Update button 
  • database will be like this 

  • Now write code Behind the Update button 

{
try{

string cs = ConfigurationManager.ConnectionStrings[“abc”].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter sda = new SqlDataAdapter(“select password from login Where Password='”+textBox1.Text+”‘”, con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count.ToString() == “1”)
{
if (textBox2.Text == textBox3.Text)
{
con.Open();
SqlCommand cmd= new SqlCommand(“UPDATE login SET Password='” + textBox3.Text + “‘ where password = ‘” + textBox1.Text + “‘”, con);

cmd.ExecuteNonQuery();
con.Close();
label1.ForeColor = System.Drawing.Color.Green;
label1.Text = “Your Password Is Successfully Updated….”;
}
else
{ }

}
else
{
label1.ForeColor = System.Drawing.Color.Red;
label1.Text = “Your Password Is Not Update Please Try Again….”;

}
} catch (Exception f)
{
MessageBox.Show(f.Message);
}
}

You can download Complete Source code .sln file 

Download Now

Leave a Comment