SHARE
TWEET

MyProject

a guest May 7th, 2016 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10.  
  11. namespace Verisk_Pro
  12. {
  13.     public partial class UserFrm : Form
  14.     {
  15.         public UserFrm()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         public int id;
  20.         SqlConnection conn = new SqlConnection("Data Source=ASANGAM-PC\\MSSQLSERVER2008;Initial Catalog=Verisk_Pro;Integrated Security=True");
  21.         private void button4_Click(object sender, EventArgs e)
  22.         {
  23.             //add
  24.  
  25.             int x = CrudOperation(0, txtFirstName.Text, txtLastName.Text, txtEmail.Text, txtUserName.Text, txtPassword.Text, cmbLevel.Text, 1);
  26.             if (x > 0)
  27.             {
  28.                 MessageBox.Show("User Created Successfully");
  29.                 GetAllUsers();
  30.  
  31.             }
  32.             else
  33.             {
  34.                 MessageBox.Show("Error in Creating User");
  35.  
  36.             }
  37.            
  38.             }
  39.         public void GetAllUsers()
  40.     {
  41.      SqlCommand cmd=new SqlCommand("Select * from UserTable order by FirstName",conn);
  42.      DataTable dt = new DataTable();
  43.      conn.Open();
  44.      SqlDataReader dr = cmd.ExecuteReader();
  45.      dt.Load(dr);
  46.      conn.Close();
  47.      dgvUserDetails.DataSource=dt;
  48.  
  49.     }
  50.  
  51.         private void UserFrm_Load(object sender, EventArgs e)
  52.         {
  53.             GetAllUsers();
  54.             //displaying the database in the data grid view with the form load
  55.         }
  56.  
  57.         private void dgvUserDetails_CellClick(object sender, DataGridViewCellEventArgs e)
  58.         {
  59.             txtFirstName.Text = dgvUserDetails.SelectedRows[0].Cells["FirstName"].Value.ToString();
  60.             txtLastName.Text = dgvUserDetails.SelectedRows[0].Cells["LastName"].Value.ToString();
  61.             txtEmail.Text = dgvUserDetails.SelectedRows[0].Cells["Email"].Value.ToString();
  62.             txtUserName.Text = dgvUserDetails.SelectedRows[0].Cells["UserName"].Value.ToString();
  63.             cmbLevel.Text = dgvUserDetails.SelectedRows[0].Cells["Level"].Value.ToString();
  64.             txtPassword.Text = dgvUserDetails.SelectedRows[0].Cells["Password"].Value.ToString();
  65.             txtConfirmPassword.Text = dgvUserDetails.SelectedRows[0].Cells["Password"].Value.ToString();
  66.             id = Convert.ToInt32(dgvUserDetails.SelectedRows[0].Cells["UserId"].Value.ToString());
  67.         }
  68.  
  69.         private void btnUpdate_Click(object sender, EventArgs e)
  70.         {
  71.            //update
  72.             int update  = CrudOperation(id, txtFirstName.Text, txtLastName.Text, txtEmail.Text, txtUserName.Text, txtPassword.Text, cmbLevel.Text, 2);
  73.             if (update > 0)
  74.             {
  75.                 MessageBox.Show("User Updated Successfully");
  76.                 GetAllUsers();
  77.  
  78.             }
  79.             else
  80.             {
  81.                 MessageBox.Show("Error in Updating the User");
  82.  
  83.             }
  84.         }
  85.  
  86.         private void btnDelete_Click(object sender, EventArgs e)
  87.         {
  88.            //delete
  89.            
  90.         }
  91.  
  92.         private void btnClose_Click(object sender, EventArgs e)
  93.         {
  94.             this.Close();
  95.         }
  96.  
  97.         public void MakeFieldsBlank()
  98.         {
  99.  
  100.             txtFirstName.Clear();
  101.             txtLastName.Clear();
  102.             txtEmail.Clear();
  103.             txtPassword.Clear();
  104.             txtUserName.Clear();
  105.             txtConfirmPassword.Clear();
  106.             cmbLevel.Text = "";            
  107.         }
  108.         public int CrudOperation(int UserId,
  109.             String FirstName,
  110.             String LastName,
  111.             String Email,
  112.             String UserName,
  113.             String Password,
  114.             String Level,
  115.             int Mode
  116.             )
  117.         {
  118.             string txtSql="";
  119.             if (Mode == 1)
  120.  
  121.               txtSql = "Insert into UserTable (FirstName=@FirstName,LastName=@LastName,Email=@Email,UserName=@UserName,Password=@Password,Level=@Level) values(@FirstName,@LastName,@Email,@UserName,@Password,@Level)";
  122.  
  123.             if(Mode==2)
  124.              txtSql = "Update UserTable set FirstName=@FirstName,LastName=@LastName,Email=@Email,UserName=@UserName,Password=@Password,Level=@Level where UserId=@UserId";
  125.             if (Mode == 3)
  126.                 txtSql = "Delete from UserTable where UserId=@UserId";
  127.  
  128.             SqlCommand cmd = new SqlCommand(txtSql, conn);
  129.             cmd.CommandType=CommandType.Text;
  130.             cmd.Parameters.AddWithValue("@FirstName", FirstName);
  131.             cmd.Parameters.AddWithValue("@LastName", LastName);
  132.             cmd.Parameters.AddWithValue("@Email", Email);
  133.             cmd.Parameters.AddWithValue("@UserName", UserName);
  134.            // cmd.Parameters.AddWithValue("@Email", Email);
  135.             //cmd.Parameters.AddWithValue("@UserName", @UserName);
  136.             cmd.Parameters.AddWithValue("@Password", Password);
  137.             cmd.Parameters.AddWithValue("@Level", Level);
  138.             conn.Open();
  139.             int result=cmd.ExecuteNonQuery();
  140.             conn.Close();
  141.             return result;
  142.  
  143.  
  144.         }
  145.    
  146.    }
  147. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top