public bool update(ContactClass c) { bool isSuccess = false; int numRows = 0; conn = new NpgsqlConnection(myconnstrng); try { sql = "Update ContactsInfo SET FirstName=@FirstName, LastName=@LastName, ContactNo=@ContactNo, Address=@Address, Gender=@Gender WHERE ContactID=@ContactID"; cmd = new NpgsqlCommand(sql, conn); cmd.Parameters.AddWithValue("@FirstName", c.FirstName); cmd.Parameters.AddWithValue("@LastName", c.LastName); cmd.Parameters.AddWithValue("@ContactNo", c.ContactNo); cmd.Parameters.AddWithValue("@Address", c.Address); cmd.Parameters.AddWithValue("@Gender", c.Gender); cmd.Parameters.AddWithValue("@ContactID", c.ContactID); conn.Open(); numRows = cmd.ExecuteNonQuery(); if (numRows > 0) { isSuccess = true; } else { isSuccess = false; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { conn.Close(); } return(isSuccess); }
public bool Insert(ContactClass c) { bool isSuccess = false; int numRows = 0; conn = new NpgsqlConnection(myconnstrng); try { sql = "INSERT INTO ContactsInfo (FirstName, LastName, ContactNo, Address, Gender) VALUES (@FirstName, @LastName, @ContactNo, @Address, @Gender)"; //SqlCommand cmd = new SqlCommand(sql, conn); cmd = new NpgsqlCommand(sql, conn); cmd.Parameters.AddWithValue("@FirstName", c.FirstName); cmd.Parameters.AddWithValue("@LastName", c.LastName); cmd.Parameters.AddWithValue("@ContactNo", c.ContactNo); cmd.Parameters.AddWithValue("@Address", c.Address); cmd.Parameters.AddWithValue("@Gender", c.Gender); conn.Open(); numRows = cmd.ExecuteNonQuery(); //int rows = cmd.ExecuteNonQuery(); if (numRows > 0) { isSuccess = true; } else { isSuccess = false; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { conn.Close(); } return(isSuccess); }
//inserting data into db public bool Insert(ContactClass model) { bool isSuccess = false; //step 1 connect DB SqlConnection con = new SqlConnection(myconnectionstring); try { //Create SQL Query to insert DATA string sql = "INSERT INTO tblContact (FirstName, LastName, Mobile, Email, Gender, Address) " + "VALUES (@FirstName, @LastName, @Mobile, @Email, @Gender, @Address)"; //Creat Sql Command using cmd and sql SqlCommand cmd = new SqlCommand(sql, con); //Create parameter to add Data cmd.Parameters.AddWithValue("@FirstName", model.FirstName); cmd.Parameters.AddWithValue("@LastName", model.LastName); cmd.Parameters.AddWithValue("@Mobile", model.Mobile); cmd.Parameters.AddWithValue("@Email", model.Email); cmd.Parameters.AddWithValue("@Gender", model.Gender); cmd.Parameters.AddWithValue("@Address", model.Address); // open Connection con.Open(); int rows = cmd.ExecuteNonQuery(); if (rows > 0) { isSuccess = true; } else { isSuccess = false; } } catch (Exception ex) { } finally { con.Close(); } return(isSuccess); }
// method to update DATA public bool Update(ContactClass model) { bool isSccess = false; SqlConnection con = new SqlConnection(myconnectionstring); try { // SQL Qery to update data in DB string sql = "UPDATE tblContact SET FirstName=@FirstName, LastName=@LastName, Mobile=@Mobile, " + "Email=@Email, Gender=@Gender, Address=@Address WHERE ContactId=@ContactId"; //Crate Sql Command SqlCommand cmd = new SqlCommand(sql, con); //create parameter to update value cmd.Parameters.AddWithValue("@FirstName", model.FirstName); cmd.Parameters.AddWithValue("@LastName", model.LastName); cmd.Parameters.AddWithValue("@Mobile", model.Mobile); cmd.Parameters.AddWithValue("@Email", model.Email); cmd.Parameters.AddWithValue("@Gender", model.Gender); cmd.Parameters.AddWithValue("@Address", model.Address); cmd.Parameters.AddWithValue("@ContactId", model.ContactId); //open db conncetion con.Open(); int rows = cmd.ExecuteNonQuery(); if (rows > 0) { isSccess = true; } else { isSccess = false; } } catch (Exception ex) { } finally { con.Close(); } return(isSccess); }
//method to delete data frm DB public bool Delete(ContactClass model) { //create a default retun value and set its value to false bool isSuccess = false; //Create SQL connection SqlConnection con = new SqlConnection(myconnectionstring); try { //Sql to Delte DATA from DB string sql = "DELETE FROM tblContact WHERE ContactId = @ContactId"; //createSql command SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.AddWithValue("@ContactId", model.ContactId); con.Open(); int rows = cmd.ExecuteNonQuery(); if (rows > 0) { isSuccess = true; } else { isSuccess = false; } } catch (Exception ex) { } finally { con.Close(); } return(isSuccess); }