Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //connection
- string cs = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Exam;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
- //checking if username and password is entered
- if (txtUsername.Text == "" || txtPassoword.Text == "")
- {
- MessageBox.Show("Please Enter UserName and Password");
- return;
- }
- try
- {
- //creating Sql Connection
- SqlConnection con = new SqlConnection(cs);
- SqlCommand cmd = new SqlCommand("SELECT * FROM Users where Username=@Username and Password=@password", con);
- string User = txtUsername.Text;
- cmd.Parameters.AddWithValue("@Username", User);
- cmd.Parameters.AddWithValue("@Password", txtPassoword.Text);
- con.Open();
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- sda.Fill(ds);
- con.Close();
- int count = ds.Tables[0].Rows.Count;
- if (count == 1)
- {
- MessageBox.Show("Login Succeeded");
- this.Hide();
- Form2 fm = new Form2(User);
- fm.Show();
- }
- else
- {
- MessageBox.Show("login Failed");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- 2
- string User;
- public Form2(string Username)
- {
- InitializeComponent();
- User = Username;
- }
- try
- {
- //creating Sql Connection
- SqlConnection con = new SqlConnection(cs);
- SqlCommand cmd = new SqlCommand("SELECT * FROM UserRoles where Username=@Username and Role=1", con);
- cmd.Parameters.AddWithValue("@Username", User);
- con.Open();
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- sda.Fill(ds);
- con.Close();
- int count = ds.Tables[0].Rows.Count;
- if (count == 1)
- {
- this.Hide();
- Form3 fm = new Form3(User);
- fm.Show();
- }
- else
- {
- MessageBox.Show("User Does not have access");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- 3
- if (txtName.Text == "" || comboBox1.Text == "" || txtAddress.Text == "")
- {
- MessageBox.Show("please fill the required fields");
- }
- try
- {
- //creating Sql Connection
- SqlConnection con = new SqlConnection(cs);
- SqlCommand cmd = new SqlCommand("INSERT INTO Employees (Name, DOB, Gender, Address, Addedby, Addeddate) values(@name, @DOB, @gender,@address,@addedby, getdate())", con);
- cmd.Parameters.AddWithValue("@Name", txtName.Text);
- cmd.Parameters.AddWithValue("@DOB", dateTimePicker1.Value.Date);
- cmd.Parameters.AddWithValue("@gender", comboBox1.Text);
- cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
- cmd.Parameters.AddWithValue("@Addedby", User);
- con.Open();
- int a = cmd.ExecuteNonQuery();
- con.Close();
- if (a == 0)
- {
- MessageBox.Show("Failed");
- }
- else
- {
- MessageBox.Show("Added");
- }
- txtName.Clear();
- txtAddress.Clear();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- 4
- if (btnSearch.Text == "")
- {
- MessageBox.Show("Please Enter Employee Name");
- }
- try
- {
- //creating Sql Connection
- SqlConnection con = new SqlConnection(cs);
- SqlCommand cmd = new SqlCommand("SELECT * FROM Employees WHERE Name=@Name", con);
- cmd.Parameters.AddWithValue("@Name", txtEmpName.Text);
- con.Open();
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- sda.Fill(ds);
- con.Close();
- int count = ds.Tables[0].Rows.Count;
- if (count < 1)
- {
- MessageBox.Show("Could Not find Employee");
- }
- else
- {
- MessageBox.Show("Record Found!");
- empid = Convert.ToInt32(ds.Tables[0].Rows[0]["EmpID"].ToString());
- txtName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
- string currdate = ds.Tables[0].Rows[0]["DOB"].ToString();
- DateTime dateTime = DateTime.ParseExact(currdate, "G", null);
- dateTimePicker1.Value = dateTime;
- string gender = ds.Tables[0].Rows[0]["Gender"].ToString();
- comboBox1.SelectedIndex = comboBox1.FindStringExact(gender);
- txtAddress.Text = ds.Tables[0].Rows[0]["Address"].ToString();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- //creating Sql Connection
- SqlConnection con = new SqlConnection(cs);
- SqlCommand cmd = new SqlCommand("UPDATE Employees SET Name=@Name, DOB=@DOB, Gender=@Gender, Address=@Address WHERE empID=@EmpID", con);
- cmd.Parameters.AddWithValue("@Name", txtName.Text);
- cmd.Parameters.AddWithValue("@DOB", dateTimePicker1.Value.Date);
- cmd.Parameters.AddWithValue("@gender", comboBox1.Text);
- cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
- cmd.Parameters.AddWithValue("@EmpID", empid);
- con.Open();
- int a = cmd.ExecuteNonQuery();
- con.Close();
- if (a == 0)
- {
- MessageBox.Show("Failed");
- }
- else
- {
- MessageBox.Show("Added");
- }
- txtEmpName.Clear();
- txtName.Clear();
- txtAddress.Clear();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- 5
- try
- {
- //creating Sql Connection
- SqlConnection con = new SqlConnection(cs);
- SqlCommand cmd = new SqlCommand("SELECT * FROM Employees", con);
- con.Open();
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- sda.Fill(ds);
- con.Close();
- int count = ds.Tables[0].Rows.Count;
- if (count < 1)
- {
- MessageBox.Show("Login Succeeded");
- }
- else
- {
- dataGridView1.DataSource = ds.Tables[0];
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement