Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace login_form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=database6;Integrated Security=True");
public static string user_v;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked)
{
textBox1.UseSystemPasswordChar = false;
}
else
{
textBox1.UseSystemPasswordChar = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = new SqlCommand("Select username,password From users Where username='Admin' And password=@pass", conn);
cmd.Parameters.AddWithValue("pass", textBox1.Text);
SqlDataReader myreader;
myreader = cmd.ExecuteReader();
if(myreader.Read())
{
int resultcomp = String.Compare(textBox1.Text, myreader.GetValue(1).ToString());
if(resultcomp==0)
{
user_v = myreader["username"].ToString();
Form2 gg = new Form2();
gg.Show();
this.Hide();
conn.Close();
}
else
{
conn.Close();
MessageBox.Show("Error username or password");
}
}
else
{
conn.Close();
MessageBox.Show("Error username or password");
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
{
textBox2.UseSystemPasswordChar = false;
}
else
{
textBox2.UseSystemPasswordChar = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
fill_combo();
}
private void fill_combo()
{
SqlCommand cmd2 = new SqlCommand("Select id,username From users Where username<>'Admin'", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd2);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "username";
comboBox1.ValueMember = "id";
comboBox1.SelectedIndex = -1;
comboBox1.Text = "Select user";
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button4_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedValue==null)
{
MessageBox.Show("Please Select user");
}
else
{
conn.Open();
SqlCommand cmd = new SqlCommand("Select * From users Where id=@id And password=@pass", conn);
cmd.Parameters.AddWithValue("id", comboBox1.SelectedValue);
cmd.Parameters.AddWithValue("pass", textBox2.Text);
SqlDataReader myreader;
myreader = cmd.ExecuteReader();
if (myreader.Read())
{
int resultcomp = String.Compare(textBox2.Text, myreader.GetValue(2).ToString());
if (resultcomp == 0)
{
user_v = myreader["username"].ToString();
string add_v, update_v, delete_v;
add_v = Convert.ToString(myreader["add_data"].ToString());
update_v = Convert.ToString(myreader["update_data"].ToString());
delete_v = Convert.ToString(myreader["delete_data"].ToString());
Form2 gg = new Form2();
if (add_v == "True")
gg.add_btn.Visible = true;
else
gg.add_btn.Visible = false;
if (update_v == "True")
gg.update_btn.Visible = true;
else
gg.update_btn.Visible = false;
if (delete_v == "True")
gg.delete_btn.Visible = true;
else
gg.delete_btn.Visible = false;
gg.Show();
this.Hide();
conn.Close();
}
else
{
conn.Close();
MessageBox.Show("Error username or password");
}
}
else
{
conn.Close();
MessageBox.Show("Error username or password");
}
}
}
}
}
No comments:
Post a Comment