TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
love code
NA
33
2.6k
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.
using
AContact.AContactClasses;
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;
namespace
AContact
{
public
partial
class
AContact : Form
{
public
AContact()
{
InitializeComponent();
}
ContactClass c =
new
ContactClass();
private
void
BtnAdd_Click(
object
sender, EventArgs e)
{
// Get the value from the input fields
c.FirstName = tbFirstName.Text;
c.LastName = tbLastName.Text;
c.Phone = tbPhone.Text;
c.Email = tbEmail.Text;
c.Note = tbNote.Text;
c.Status = cmbStatus.Text;
// Inserting datat into database
bool
success = c.Insert(c);
if
(success ==
true
)
{
// succesfully Inserted
MessageBox.Show(
"New contact succesfully inserted"
);
// Call the Clear method here
Clear();
}
else
{
// Faild to add contact
MessageBox.Show(
"Failed to add new contact. Try again."
);
}
// load data on Gridview
DataTable dt = c.Select();
dgvContactList.DataSource = dt;
}
private
void
AContact_Load(
object
sender, EventArgs e)
{
// load data on Gridview
DataTable dt = c.Select();
dgvContactList.DataSource = dt;
}
// Method to clear fields
public
void
Clear()
{
tbFirstName.Text =
""
;
tbLastName.Text =
""
;
tbPhone.Text =
""
;
tbEmail.Text =
""
;
tbNote.Text =
""
;
cmbStatus.Text =
""
;
}
private
void
BtnUpdate_Click(
object
sender, EventArgs e)
{
// Get the data from textboxes
c.ContactID =
int
.Parse(tbContactID.Text);
c.FirstName = tbFirstName.Text;
c.LastName = tbLastName.Text;
c.Phone = tbPhone.Text;
c.Email = tbEmail.Text;
c.Note = tbNote.Text;
c.Status = cmbStatus.Text;
// Update data in database
bool
success = c.Update(c);
if
(success==
true
)
{
// Updated succesfully
MessageBox.Show(
"Contact has been succesfully Updated"
);
// Load data on Data Gridview
DataTable dt = c.Select();
dgvContactList.DataSource = dt;
// Call Clear Method
Clear();
}
else
{
// Failed to Update
MessageBox.Show(
"Failed to update. Try again."
);
}
}
private
void
BtnDelete_Click(
object
sender, EventArgs e)
{
c.ContactID = Convert.ToInt32(tbContactID.Text);
bool
success = c.Delete(c);
if
(success ==
true
)
{
// Succesfully deleted
MessageBox.Show(
"Contact succesfully deleted."
);
// Refresh data GridView
// Load Data on Data GridView
DataTable dt = c.Select();
dgvContactList.DataSource = dt;
// Call the Clear methode here
Clear();
}
else
{
// Failed to delete
MessageBox.Show(
"Failed to delete contact. Try again."
);
}
}
private
void
BtnClear_Click(
object
sender, EventArgs e)
{
// Call Clear method here
Clear();
}
private
void
dgvContactList_RowHeaderMouseClick(
object
sender, DataGridViewCellMouseEventArgs e)
{
// Get data from Data Grid View and load it to the textboxes respectively
// Identify the row on witch mouse is clicked
int
rowIndex = e.RowIndex;
tbContactID.Text = dgvContactList.Rows[rowIndex].Cells[0].Value.ToString();
tbFirstName.Text = dgvContactList.Rows[rowIndex].Cells[1].Value.ToString();
tbLastName.Text = dgvContactList.Rows[rowIndex].Cells[2].Value.ToString();
tbPhone.Text = dgvContactList.Rows[rowIndex].Cells[3].Value.ToString();
tbEmail.Text = dgvContactList.Rows[rowIndex].Cells[4].Value.ToString();
tbNote.Text = dgvContactList.Rows[rowIndex].Cells[5].Value.ToString();
cmbStatus.Text = dgvContactList.Rows[rowIndex].Cells[6].Value.ToString();
}
}
}
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
{
// Getter and Setters properties
// Acts as a data carrier in this application
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;
// Selecting data from database
public
DataTable Select()
{
// step 1: Database connection
SqlConnection conn =
new
SqlConnection(myconnstrng);
DataTable dt =
new
DataTable();
try
{
// step 2: Writing sql query
string
sql =
"SELECT * FROM tbl_contact"
;
// Creating cmd using sql and conn
SqlCommand cmd =
new
SqlCommand(sql, conn);
// Creating sql DataAdappter using cmd
SqlDataAdapter adapter =
new
SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch
(Exception ex)
{
}
finally
{
conn.Close();
}
return
dt;
}
// Inserting into database
public
bool
Insert(ContactClass c)
{
// Creating a default returntype and setting its value to false
bool
isSuccess =
false
;
// Step 1. Connect database
SqlConnection conn =
new
SqlConnection(myconnstrng);
try
{
// Step 2: Create a SQL query to insert data
string
sql =
"INSERT INTO tbl_contact (FirstName, LastName, Phone, Email, Note, Status) VALUES(@FirstName, @LastName, @Phone, @Email, @Note, @Status)"
;
// Creating SQL Command using sql and conn
SqlCommand cmd =
new
SqlCommand(sql, conn);
// Create Parameters to add data
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);
// Connection Open here
conn.Open();
int
rows = cmd.ExecuteNonQuery();
// If the query runs succesfully then the value is > 0, else the value will be < 0
if
(rows > 0)
{
isSuccess =
true
;
}
else
{
isSuccess =
false
;
}
}
catch
(Exception ex)
{
}
finally
{
conn.Close();
}
return
isSuccess;
}
// Method to update data in database
public
bool
Update(ContactClass c)
{
// Create a default return type and set its default value to false
bool
isSuccess =
false
;
SqlConnection conn =
new
SqlConnection(myconnstrng);
try
{
// SQL to update data in hour database
string
sql =
"UPDATE tbl_contact SET FirstName=@FirstName, LastName=@LastName, Phone=@Phone, Email=@Email, Note=@Note, Status=@Status WHERE ContactID=@ContactID"
;
// Creating SQL command
SqlCommand cmd =
new
SqlCommand(sql, conn);
// Create Parameters to add value
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);
// Open database connection
conn.Open();
int
rows = cmd.ExecuteNonQuery();
// If the query runs succesfully then the value of rows > 0 else its value will be 0
if
(rows > 0)
{
isSuccess =
true
;
}
else
{
isSuccess =
false
;
}
}
catch
(Exception ex)
{
}
finally
{
conn.Close();
}
return
isSuccess;
}
// Method to delete data from database
public
bool
Delete(ContactClass c)
{
// create a default return value and set its value to false
bool
isSuccess =
false
;
// Create SQL Connection
SqlConnection conn =
new
SqlConnection(myconnstrng);
try
{
// Method to delete data
string
sql =
"DELETE FROM tbl_contact WHERE ContactID=@ContactID"
;
// Creating SQL command
SqlCommand cmd =
new
SqlCommand(sql, conn);
cmd.Parameters.AddWithValue(
"@ContactID"
, c.ContactID);
// Open Connection
conn.Open();
int
rows = cmd.ExecuteNonQuery();
// if the query runs succesfully then the value of rows will be > 0 , else the value will be 0
if
(rows > 0)
{
isSuccess =
true
;
}
else
{
isSuccess =
false
;
}
}
catch
(Exception ex)
{
}
finally
{
// Close connection
conn.Close();
}
return
isSuccess;
}
}
}
Attachment:
AContact.zip
Reply
Answers (
2
)
Ddisplay of a List into a ListBox does not work
Print inverted pyramid