Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 TEST
- {
- public partial class AddUsers : Form
- {
- SqlConnection con = new SqlConnection(@"Data Source(LocalDB)MSSQLLocalDB;AttachDbFilename=mydb;Integrated Security=True;Connect Timeout=30");
- SqlCommand cmd;
- SqlDataAdapter adapt;
- int ID = 0;
- public AddUsers()
- {
- InitializeComponent();
- DisplayData();
- }
- private void selectRow(object sender, DataGridViewCellMouseEventArgs e)
- {
- ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
- textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
- textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
- }
- //Update Record
- private void button2_Click(object sender, EventArgs e)
- {
- if (textBox1.Text != "" && textBox2.Text != "")
- {
- cmd = new SqlCommand("update tblLOGIN set USERNAME=@USERNAME,PASSWORD=@PASSWORD where Id=@id", con);
- con.Open();
- cmd.Parameters.AddWithValue("@Id", ID);
- cmd.Parameters.AddWithValue("@USERNAME", textBox1.Text);
- cmd.Parameters.AddWithValue("@PASSWORD", textBox2.Text);
- cmd.ExecuteNonQuery();
- MessageBox.Show("Record Updated Successfully");
- con.Close();
- DisplayData();
- ClearData();
- }
- else
- {
- MessageBox.Show("Please Select Record to Update");
- }
- }
- //New User
- private void button1_Click(object sender, EventArgs e)
- {
- if (textBox1.Text != "" && textBox2.Text != "")
- {
- cmd = new SqlCommand("insert into tblLOGIN(USERNAME,PASSWORD) values(@USERNAME,@PASSWORD)", con);
- con.Open();
- cmd.Parameters.AddWithValue("@USERNAME", textBox1.Text);
- cmd.Parameters.AddWithValue("@PASSWORD", textBox2.Text);
- cmd.ExecuteNonQuery();
- con.Close();
- MessageBox.Show("User created successfully");
- DisplayData();
- ClearData();
- }
- else
- {
- MessageBox.Show("Please enter username and password");
- }
- }
- private void DisplayData()
- {
- con.Open();
- DataTable dt = new DataTable();
- adapt = new SqlDataAdapter("select * from tblLOGIN", con);
- adapt.Fill(dt);
- dataGridView1.DataSource = dt;
- con.Close();
- }
- private void ClearData()
- {
- textBox1.Text = "";
- textBox2.Text = "";
- ID = 0;
- }
- //Delete user
- private void button3_Click(object sender, EventArgs e)
- {
- if (ID != 0)
- {
- cmd = new SqlCommand("delete tblLOGIN where Id=@id", con);
- con.Open();
- cmd.Parameters.AddWithValue("@id", ID);
- cmd.ExecuteNonQuery();
- con.Close();
- MessageBox.Show("Record Deleted Successfully!");
- DisplayData();
- ClearData();
- }
- else
- {
- MessageBox.Show("Please Select Record to Delete");
- }
- }
- private void AddUsers_Load(object sender, EventArgs e)
- {
- this.tblLOGINTableAdapter.Fill(this.usersDataSet.tblLOGIN);
- }
- private void button4_Click(object sender, EventArgs e)
- {
- AuthenicationForm ss = new AuthenicationForm();
- this.Close();
- ss.Show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement