Advertisement
Guest User

Untitled

a guest
May 12th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.92 KB | None | 0 0
  1. //connection
  2. string cs = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Exam;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
  3.  
  4.  //checking if username and password is entered
  5.             if (txtUsername.Text == "" || txtPassoword.Text == "")
  6.             {
  7.                 MessageBox.Show("Please Enter UserName and Password");
  8.                 return;
  9.             }
  10.  
  11.             try
  12.             {
  13.                 //creating Sql Connection
  14.                 SqlConnection con = new SqlConnection(cs);
  15.                 SqlCommand cmd = new SqlCommand("SELECT * FROM Users where Username=@Username and Password=@password", con);
  16.                 string User = txtUsername.Text;
  17.                 cmd.Parameters.AddWithValue("@Username", User);
  18.                 cmd.Parameters.AddWithValue("@Password", txtPassoword.Text);
  19.                 con.Open();
  20.                 SqlDataAdapter sda = new SqlDataAdapter(cmd);
  21.                 DataSet ds = new DataSet();
  22.                 sda.Fill(ds);
  23.                 con.Close();
  24.                 int count = ds.Tables[0].Rows.Count;
  25.                 if (count == 1)
  26.                 {
  27.                     MessageBox.Show("Login Succeeded");
  28.                     this.Hide();
  29.                     Form2 fm = new Form2(User);
  30.                     fm.Show();
  31.                 }
  32.                 else
  33.                 {
  34.                     MessageBox.Show("login Failed");
  35.                 }
  36.             }
  37.  
  38.             catch (Exception ex)
  39.             {
  40.                 MessageBox.Show(ex.Message);
  41.             }
  42.  
  43.  
  44.  
  45. 2
  46.  string User;
  47.         public Form2(string Username)
  48.         {
  49.             InitializeComponent();
  50.             User = Username;
  51.         }
  52.  
  53.  
  54.             try
  55.             {
  56.                 //creating Sql Connection
  57.                 SqlConnection con = new SqlConnection(cs);
  58.                 SqlCommand cmd = new SqlCommand("SELECT * FROM UserRoles where Username=@Username and Role=1", con);
  59.                 cmd.Parameters.AddWithValue("@Username", User);
  60.                 con.Open();
  61.                 SqlDataAdapter sda = new SqlDataAdapter(cmd);
  62.                 DataSet ds = new DataSet();
  63.                 sda.Fill(ds);
  64.                 con.Close();
  65.                 int count = ds.Tables[0].Rows.Count;
  66.                 if (count == 1)
  67.                 {
  68.                     this.Hide();
  69.                     Form3 fm = new Form3(User);
  70.                     fm.Show();
  71.                 }
  72.                 else
  73.                 {
  74.                     MessageBox.Show("User Does not have access");
  75.                 }
  76.             }
  77.  
  78.             catch (Exception ex)
  79.             {
  80.                 MessageBox.Show(ex.Message);
  81.             }
  82. 3
  83.  
  84.  if (txtName.Text == "" || comboBox1.Text == "" || txtAddress.Text == "")
  85.             {
  86.                 MessageBox.Show("please fill the required fields");
  87.             }
  88.  
  89.  
  90.  
  91.             try
  92.             {
  93.                 //creating Sql Connection
  94.                 SqlConnection con = new SqlConnection(cs);
  95.                 SqlCommand cmd = new SqlCommand("INSERT INTO Employees (Name, DOB, Gender, Address, Addedby, Addeddate) values(@name, @DOB, @gender,@address,@addedby, getdate())", con);
  96.                 cmd.Parameters.AddWithValue("@Name", txtName.Text);
  97.                 cmd.Parameters.AddWithValue("@DOB", dateTimePicker1.Value.Date);
  98.                 cmd.Parameters.AddWithValue("@gender", comboBox1.Text);
  99.                 cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
  100.                 cmd.Parameters.AddWithValue("@Addedby", User);
  101.                 con.Open();
  102.                 int a = cmd.ExecuteNonQuery();
  103.                 con.Close();
  104.                 if (a == 0)
  105.                 {
  106.                     MessageBox.Show("Failed");
  107.                 }
  108.                 else
  109.                 {
  110.                     MessageBox.Show("Added");
  111.                 }
  112.  
  113.                 txtName.Clear();
  114.                 txtAddress.Clear();
  115.                
  116.             }
  117.  
  118.             catch (Exception ex)
  119.             {
  120.                 MessageBox.Show(ex.Message);
  121.             }
  122.  
  123. 4
  124.  
  125. if (btnSearch.Text == "")
  126.             {
  127.                 MessageBox.Show("Please Enter Employee Name");
  128.             }
  129.  
  130.             try
  131.             {
  132.                 //creating Sql Connection
  133.                 SqlConnection con = new SqlConnection(cs);
  134.                 SqlCommand cmd = new SqlCommand("SELECT * FROM Employees WHERE Name=@Name", con);
  135.                 cmd.Parameters.AddWithValue("@Name", txtEmpName.Text);
  136.                 con.Open();
  137.                 SqlDataAdapter sda = new SqlDataAdapter(cmd);
  138.                 DataSet ds = new DataSet();
  139.                 sda.Fill(ds);
  140.                 con.Close();
  141.                 int count = ds.Tables[0].Rows.Count;
  142.                 if (count < 1)
  143.                 {
  144.                     MessageBox.Show("Could Not find Employee");
  145.                 }
  146.                 else
  147.                 {
  148.                     MessageBox.Show("Record Found!");
  149.  
  150.                     empid = Convert.ToInt32(ds.Tables[0].Rows[0]["EmpID"].ToString());
  151.                     txtName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
  152.                     string currdate = ds.Tables[0].Rows[0]["DOB"].ToString();
  153.                     DateTime dateTime = DateTime.ParseExact(currdate, "G", null);
  154.                     dateTimePicker1.Value = dateTime;
  155.  
  156.                     string gender = ds.Tables[0].Rows[0]["Gender"].ToString();
  157.  
  158.                     comboBox1.SelectedIndex = comboBox1.FindStringExact(gender);
  159.                     txtAddress.Text = ds.Tables[0].Rows[0]["Address"].ToString();
  160.  
  161.  
  162.                 }
  163.             }
  164.  
  165.             catch (Exception ex)
  166.             {
  167.                 MessageBox.Show(ex.Message);
  168.             }
  169.            
  170.         }
  171.  
  172.         private void button2_Click(object sender, EventArgs e)
  173.         {
  174.             try
  175.             {
  176.                 //creating Sql Connection
  177.                 SqlConnection con = new SqlConnection(cs);
  178.                 SqlCommand cmd = new SqlCommand("UPDATE Employees SET Name=@Name, DOB=@DOB, Gender=@Gender, Address=@Address WHERE empID=@EmpID", con);
  179.                 cmd.Parameters.AddWithValue("@Name", txtName.Text);
  180.                 cmd.Parameters.AddWithValue("@DOB", dateTimePicker1.Value.Date);
  181.                 cmd.Parameters.AddWithValue("@gender", comboBox1.Text);
  182.                 cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
  183.                 cmd.Parameters.AddWithValue("@EmpID", empid);
  184.                 con.Open();
  185.                 int a = cmd.ExecuteNonQuery();
  186.                 con.Close();
  187.                 if (a == 0)
  188.                 {
  189.                     MessageBox.Show("Failed");
  190.                 }
  191.                 else
  192.                 {
  193.                     MessageBox.Show("Added");
  194.                 }
  195.  
  196.                 txtEmpName.Clear();
  197.                 txtName.Clear();
  198.                 txtAddress.Clear();
  199.  
  200.             }
  201.  
  202.             catch (Exception ex)
  203.             {
  204.                 MessageBox.Show(ex.Message);
  205.             }
  206.  
  207. 5
  208.  
  209.  try
  210.             {
  211.                 //creating Sql Connection
  212.                 SqlConnection con = new SqlConnection(cs);
  213.                 SqlCommand cmd = new SqlCommand("SELECT * FROM Employees", con);
  214.                 con.Open();
  215.                 SqlDataAdapter sda = new SqlDataAdapter(cmd);
  216.                 DataSet ds = new DataSet();
  217.                 sda.Fill(ds);
  218.                 con.Close();
  219.                 int count = ds.Tables[0].Rows.Count;
  220.                 if (count < 1)
  221.                 {
  222.                     MessageBox.Show("Login Succeeded");
  223.                 }
  224.                 else
  225.                 {
  226.                     dataGridView1.DataSource = ds.Tables[0];
  227.                 }
  228.             }
  229.  
  230.             catch (Exception ex)
  231.             {
  232.                 MessageBox.Show(ex.Message);
  233.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement