love code

love code

  • NA
  • 33
  • 2.4k

How do i get the Update operation working in this C# CRUD ?

Feb 28 2018 5:51 PM
In this C#  CRUD. Only the Update function is not working at all while the Add, Clear and Delete are working as planed.   
 
What do i need to edit to the code to get the Update class to work as planed? 
 
Source code see the .zipfile or see below. 
  1. using AContact.AContactClasses;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.ComponentModel;  
  5. using System.Data;  
  6. using System.Drawing;  
  7. using System.Linq;  
  8. using System.Text;  
  9. using System.Threading.Tasks;  
  10. using System.Windows.Forms;  
  11.   
  12. namespace AContact  
  13. {  
  14.     public partial class AContact : Form  
  15.     {  
  16.         public AContact()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         ContactClass c = new ContactClass();  
  22.   
  23.         private void BtnAdd_Click(object sender, EventArgs e)  
  24.         {  
  25.   
  26.             // Get the value from the input fields   
  27.             c.FirstName = tbFirstName.Text;  
  28.             c.LastName = tbLastName.Text;  
  29.             c.Phone = tbPhone.Text;  
  30.             c.Email = tbEmail.Text;  
  31.             c.Note = tbNote.Text;  
  32.             c.Status = cmbStatus.Text;  
  33.   
  34.             // Inserting datat into database  
  35.             bool success = c.Insert(c);  
  36.             if (success == true)  
  37.             {  
  38.                 // succesfully Inserted   
  39.                 MessageBox.Show("New contact succesfully inserted");  
  40.                 // Call the Clear method here   
  41.                 Clear();   
  42.             }  
  43.             else  
  44.             {  
  45.                 // Faild to add contact   
  46.                 MessageBox.Show("Failed to add new contact. Try again.");   
  47.             }  
  48.   
  49.         // load data on Gridview   
  50.         DataTable dt = c.Select();  
  51.         dgvContactList.DataSource = dt;   
  52.           
  53.         }  
  54.   
  55.         private void AContact_Load(object sender, EventArgs e)  
  56.         {  
  57.             // load data on Gridview   
  58.             DataTable dt = c.Select();  
  59.             dgvContactList.DataSource = dt;  
  60.         }  
  61.   
  62.         // Method to clear fields   
  63.         public void Clear()  
  64.         {  
  65.             tbFirstName.Text = "";  
  66.             tbLastName.Text = "";  
  67.             tbPhone.Text = "";  
  68.             tbEmail.Text = "";  
  69.             tbNote.Text = "";  
  70.             cmbStatus.Text = "";    
  71.         }  
  72.   
  73.   
  74.         private void BtnUpdate_Click(object sender, EventArgs e)  
  75.         {  
  76.             // Get the data from textboxes   
  77.             c.ContactID = int.Parse(tbContactID.Text);  
  78.             c.FirstName = tbFirstName.Text;  
  79.             c.LastName = tbLastName.Text;  
  80.             c.Phone = tbPhone.Text;  
  81.             c.Email = tbEmail.Text;  
  82.             c.Note = tbNote.Text;  
  83.             c.Status = cmbStatus.Text;  
  84.             // Update data in database  
  85.             bool success = c.Update(c);  
  86.             if (success==true)  
  87.             {  
  88.                 // Updated succesfully  
  89.                 MessageBox.Show("Contact has been succesfully Updated");  
  90.                 // Load data on Data Gridview   
  91.                 DataTable dt = c.Select();  
  92.                 dgvContactList.DataSource = dt;  
  93.                 // Call Clear Method   
  94.                 Clear();  
  95.             }  
  96.             else  
  97.             {  
  98.                 // Failed to Update   
  99.                 MessageBox.Show("Failed to update. Try again.");  
  100.             }  
  101.         }  
  102.   
  103.         private void BtnDelete_Click(object sender, EventArgs e)  
  104.         {  
  105.             c.ContactID = Convert.ToInt32(tbContactID.Text);   
  106.             bool success = c.Delete(c);  
  107.             if (success == true)  
  108.             {  
  109.                 // Succesfully deleted   
  110.                 MessageBox.Show("Contact succesfully deleted.");  
  111.                 // Refresh data GridView  
  112.                 // Load Data on Data GridView  
  113.                 DataTable dt = c.Select();  
  114.                 dgvContactList.DataSource = dt;  
  115.                 // Call the  Clear methode here   
  116.                 Clear();  
  117.             }  
  118.             else  
  119.             {  
  120.                 // Failed to delete   
  121.                 MessageBox.Show("Failed to delete contact. Try again.");   
  122.             }  
  123.                  
  124.         }  
  125.   
  126.         private void BtnClear_Click(object sender, EventArgs e)  
  127.         {  
  128.             // Call Clear method here   
  129.             Clear();   
  130.         }  
  131.   
  132.         private void dgvContactList_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)  
  133.         {  
  134.             // Get data from Data Grid View and load it to the textboxes respectively   
  135.             // Identify the row on witch mouse is clicked   
  136.             int rowIndex = e.RowIndex;  
  137.             tbContactID.Text = dgvContactList.Rows[rowIndex].Cells[0].Value.ToString();  
  138.             tbFirstName.Text = dgvContactList.Rows[rowIndex].Cells[1].Value.ToString();  
  139.             tbLastName.Text = dgvContactList.Rows[rowIndex].Cells[2].Value.ToString();  
  140.             tbPhone.Text = dgvContactList.Rows[rowIndex].Cells[3].Value.ToString();  
  141.             tbEmail.Text = dgvContactList.Rows[rowIndex].Cells[4].Value.ToString();  
  142.             tbNote.Text = dgvContactList.Rows[rowIndex].Cells[5].Value.ToString();  
  143.             cmbStatus.Text = dgvContactList.Rows[rowIndex].Cells[6].Value.ToString();  
  144.         }  
  145.     }  
  146.     }  
  147.       
ContactClass.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Configuration;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9.   
  10. namespace AContact.AContactClasses  
  11. {  
  12.     class ContactClass  
  13.     {  
  14.         // Getter and Setters properties   
  15.         // Acts as a data carrier in this application   
  16.         public int ContactID { getset; }  
  17.         public string FirstName { getset; }  
  18.         public string LastName { getset; }  
  19.         public string Phone { getset; }  
  20.         public string Email { getset; }  
  21.         public string Note { getset; }   public string Status { getset; }  
  22.          
  23.         static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;  
  24.   
  25.         // Selecting data from database   
  26.         public DataTable Select()  
  27.         {  
  28.             // step 1: Database connection   
  29.             SqlConnection conn = new SqlConnection(myconnstrng);  
  30.             DataTable dt = new DataTable();  
  31.             try  
  32.             {  
  33.                 // step 2: Writing sql query  
  34.                 string sql = "SELECT * FROM tbl_contact";  
  35.                 // Creating cmd using sql and conn   
  36.                 SqlCommand cmd = new SqlCommand(sql, conn);  
  37.                 // Creating sql DataAdappter using cmd   
  38.                 SqlDataAdapter adapter = new SqlDataAdapter(cmd);  
  39.                 conn.Open();  
  40.                 adapter.Fill(dt);  
  41.             }  
  42.             catch (Exception ex)  
  43.             {  
  44.   
  45.             }  
  46.             finally  
  47.             {  
  48.                 conn.Close();  
  49.             }  
  50.             return dt;  
  51.         }  
  52.         // Inserting into database   
  53.         public bool Insert(ContactClass c)  
  54.         {  
  55.             // Creating a default returntype and setting its value to false   
  56.             bool isSuccess = false;  
  57.   
  58.             // Step 1. Connect database   
  59.             SqlConnection conn = new SqlConnection(myconnstrng);  
  60.             try  
  61.             {  
  62.                 // Step 2: Create a SQL query to insert data  
  63.           string sql = "INSERT INTO tbl_contact (FirstName, LastName, Phone, Email, Note, Status) VALUES(@FirstName, @LastName, @Phone, @Email, @Note, @Status)";  
  64.                 // Creating SQL Command using sql and conn   
  65.                 SqlCommand cmd = new SqlCommand(sql, conn);  
  66.                 // Create Parameters to add data   
  67.                 cmd.Parameters.AddWithValue("@FirstName", c.FirstName);  
  68.                 cmd.Parameters.AddWithValue("@LastName", c.LastName);  
  69.                 cmd.Parameters.AddWithValue("@Phone", c.Phone);  
  70.                 cmd.Parameters.AddWithValue("@Email", c.Email);  
  71.                 cmd.Parameters.AddWithValue("@Note", c.Note);  
  72.                 cmd.Parameters.AddWithValue("@Status", c.Status);  
  73.   
  74.                 // Connection Open here  
  75.                 conn.Open();  
  76.                 int rows = cmd.ExecuteNonQuery();  
  77.                 // If the query runs succesfully then the value is > 0, else the value will be < 0   
  78.                 if (rows > 0)  
  79.                 {  
  80.                     isSuccess = true;  
  81.                 }  
  82.                 else  
  83.                 {  
  84.                     isSuccess = false;  
  85.                 }  
  86.   
  87.             }  
  88.             catch (Exception ex)  
  89.             {  
  90.          
  91.             }  
  92.             finally  
  93.             {  
  94.                 conn.Close();  
  95.             }  
  96.             return isSuccess;  
  97.         }  
  98.   
  99.         // Method to update data in database   
  100.         public bool Update(ContactClass c)  
  101.         {  
  102.             // Create a default return type and set its default value to false   
  103.             bool isSuccess = false;  
  104.             SqlConnection conn = new SqlConnection(myconnstrng);  
  105.             try  
  106.             {  
  107.                 // SQL to update data in hour database   
  108. string sql = "UPDATE tbl_contact SET FirstName=@FirstName, LastName=@LastName, Phone=@Phone, Email=@Email, Note=@Note, Status=@Status WHERE ContactID=@ContactID";  
  109.   
  110.                 // Creating SQL command  
  111.                 SqlCommand cmd = new SqlCommand(sql, conn);  
  112.                 // Create Parameters to add value  
  113.                 cmd.Parameters.AddWithValue("@FirstName",   c.FirstName);  
  114.                 cmd.Parameters.AddWithValue("@FirstName",   c.LastName);  
  115.                 cmd.Parameters.AddWithValue("@Phone",       c.Phone);  
  116.                 cmd.Parameters.AddWithValue("@Email",       c.Email);  
  117.                 cmd.Parameters.AddWithValue("@Note",        c.Note);  
  118.                 cmd.Parameters.AddWithValue("@Status",      c.Status);  
  119.                 cmd.Parameters.AddWithValue("@ContactID",   c.ContactID);  
  120.   
  121.                 // Open database connection   
  122.                 conn.Open();  
  123.   
  124.                 int rows = cmd.ExecuteNonQuery();  
  125.                 // If the query runs succesfully then the value of rows > 0 else its value will be 0   
  126.                 if (rows > 0)  
  127.                 {  
  128.                     isSuccess = true;  
  129.                 }  
  130.                 else  
  131.                 {  
  132.                     isSuccess = false;  
  133.                 }  
  134.             }  
  135.             catch (Exception ex)  
  136.             {
  137.             }
  138.             finally  
  139.             {  
  140.                 conn.Close();   
  141.             }  
  142.             return isSuccess;   
  143.         }
  144.         // Method to delete data from database   
  145.         public bool Delete(ContactClass c)  
  146.         {  
  147.             // create a default return value and set its value to false   
  148.             bool isSuccess = false;  
  149.             // Create SQL Connection  
  150.             SqlConnection conn = new SqlConnection(myconnstrng);  
  151.             try  
  152.             {  
  153.                 //  Method to delete data   
  154.                 string sql = "DELETE FROM tbl_contact WHERE ContactID=@ContactID";  
  155.                 // Creating SQL command     
  156.                 SqlCommand cmd = new SqlCommand(sql, conn);  
  157.                 cmd.Parameters.AddWithValue("@ContactID", c.ContactID);  
  158.                 // Open Connection   
  159.                 conn.Open();  
  160.                 int rows = cmd.ExecuteNonQuery();  
  161.                 // if the query runs succesfully then the value of rows will be > 0 , else the value will be 0   
  162.                 if (rows > 0)  
  163.                 {  
  164.                     isSuccess = true;
  165.                 }  
  166.                 else  
  167.                 {  
  168.                     isSuccess = false;  
  169.                 }  
  170.             }  
  171.             catch (Exception ex)  
  172.             {
  173.             }  
  174.             finally  
  175.             {  
  176.                 // Close connection   
  177.                 conn.Close();  
  178.             }  
  179.             return isSuccess;  
  180.         }
  181.     }
  182. }

Attachment: AContact.zip

Answers (2)