Click here to Skip to main content
14,983,808 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i need a code for login button in c#. I HAVE 2 fields username and password and login button and back end as sql server i have created a table my name register it has login and password fields. once i click on login button it should login if the user data is correct of not it should error.

can anyone help with this in writing the code.

What I have tried:

i have tried in connecting to the database to access the existing fields i.e., username and password.
Posted
Updated 20-May-21 15:38pm
v2
Comments
Mehdi Gholam 17-Sep-16 2:42am
   
Your question makes no sense since "login" depends on what you are doing and how you built your system.
Member 12650438 17-Sep-16 3:22am
   
my code is not accepting after proving the correct details also.



private void button1_Click_1(object sender, EventArgs e)
{
string cs = @"Data Source=.;Database=master;Integrated Security=True;";


if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("Please provide UserName and Password");
return;
}
try
{
//Create SqlConnection
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("Select * from Register where UserNmae=@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)
{
MessageBox.Show("Login Successful!");
this.Hide();
Dashboard dsb = new Dashboard();
}
else
{
MessageBox.Show("Login Failed!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Patrice T 17-Sep-16 9:19am
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
OriginalGriff 17-Sep-16 3:40am
   
And? What happens that you don't expect, or does happen that you didn't?
BillWoodruff 17-Sep-16 4:52am
   
What is the exception text that appears in the MessageBox when it fails ?
Member 12479751 17-Sep-16 5:37am
   
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace First_Csharp_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
if (!(usertxt.Text == string.Empty))
{
if (!(passtxt.Text == string.Empty))
{
String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
String query = "select * from data where username = '" + usertxt.Text + "'and password = '" + this.passtxt.Text + "'";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dbr;
con.Open();
dbr = cmd.ExecuteReader();
int count = 0;
while (dbr.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("username and password is correct");
}
else if (count > 1)
{
MessageBox.Show("Duplicate username and password", "login page");
}
else
{
MessageBox.Show(" username and password incorrect", "login page");
}
}
else
{
MessageBox.Show(" password empty", "login page");
}
}

else
{
MessageBox.Show(" username empty", "login page");
}
// con.Close();

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

}
}
}
}
[no name] 17-Sep-16 9:05am
   
Learn how to use the debugger. We aren't here to do your homework for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900