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.
ContactClass.cs
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace AContact.AContactClasses
- {
- class ContactClass
- {
-
-
- public int ContactID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Phone { get; set; }
- public string Email { get; set; }
- public string Note { get; set; } public string Status { get; set; }
-
- static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;
-
-
- public DataTable Select()
- {
-
- SqlConnection conn = new SqlConnection(myconnstrng);
- DataTable dt = new DataTable();
- try
- {
-
- string sql = "SELECT * FROM tbl_contact";
-
- SqlCommand cmd = new SqlCommand(sql, conn);
-
- SqlDataAdapter adapter = new SqlDataAdapter(cmd);
- conn.Open();
- adapter.Fill(dt);
- }
- catch (Exception ex)
- {
-
- }
- finally
- {
- conn.Close();
- }
- return dt;
- }
-
- public bool Insert(ContactClass c)
- {
-
- bool isSuccess = false;
-
-
- SqlConnection conn = new SqlConnection(myconnstrng);
- try
- {
-
- string sql = "INSERT INTO tbl_contact (FirstName, LastName, Phone, Email, Note, Status) VALUES(@FirstName, @LastName, @Phone, @Email, @Note, @Status)";
-
- SqlCommand cmd = new SqlCommand(sql, conn);
-
- cmd.Parameters.AddWithValue("@FirstName", c.FirstName);
- cmd.Parameters.AddWithValue("@LastName", c.LastName);
- cmd.Parameters.AddWithValue("@Phone", c.Phone);
- cmd.Parameters.AddWithValue("@Email", c.Email);
- cmd.Parameters.AddWithValue("@Note", c.Note);
- cmd.Parameters.AddWithValue("@Status", c.Status);
-
-
- conn.Open();
- int rows = cmd.ExecuteNonQuery();
-
- if (rows > 0)
- {
- isSuccess = true;
- }
- else
- {
- isSuccess = false;
- }
-
- }
- catch (Exception ex)
- {
-
- }
- finally
- {
- conn.Close();
- }
- return isSuccess;
- }
-
-
- public bool Update(ContactClass c)
- {
-
- bool isSuccess = false;
- SqlConnection conn = new SqlConnection(myconnstrng);
- try
- {
-
- string sql = "UPDATE tbl_contact SET FirstName=@FirstName, LastName=@LastName, Phone=@Phone, Email=@Email, Note=@Note, Status=@Status WHERE ContactID=@ContactID";
-
-
- SqlCommand cmd = new SqlCommand(sql, conn);
-
- cmd.Parameters.AddWithValue("@FirstName", c.FirstName);
- cmd.Parameters.AddWithValue("@FirstName", c.LastName);
- cmd.Parameters.AddWithValue("@Phone", c.Phone);
- cmd.Parameters.AddWithValue("@Email", c.Email);
- cmd.Parameters.AddWithValue("@Note", c.Note);
- cmd.Parameters.AddWithValue("@Status", c.Status);
- cmd.Parameters.AddWithValue("@ContactID", c.ContactID);
-
-
- conn.Open();
-
- int rows = cmd.ExecuteNonQuery();
-
- if (rows > 0)
- {
- isSuccess = true;
- }
- else
- {
- isSuccess = false;
- }
- }
- catch (Exception ex)
- {
- }
- finally
- {
- conn.Close();
- }
- return isSuccess;
- }
-
- public bool Delete(ContactClass c)
- {
-
- bool isSuccess = false;
-
- SqlConnection conn = new SqlConnection(myconnstrng);
- try
- {
-
- string sql = "DELETE FROM tbl_contact WHERE ContactID=@ContactID";
-
- SqlCommand cmd = new SqlCommand(sql, conn);
- cmd.Parameters.AddWithValue("@ContactID", c.ContactID);
-
- conn.Open();
- int rows = cmd.ExecuteNonQuery();
-
- if (rows > 0)
- {
- isSuccess = true;
- }
- else
- {
- isSuccess = false;
- }
- }
- catch (Exception ex)
- {
- }
- finally
- {
-
- conn.Close();
- }
- return isSuccess;
- }
- }
- }