public bool Insert(categoryBLL c) { //Creating A boolean variable and set it default value to false bool isSuccess = false; //connecting to Database SqlConnection conn = new SqlConnection(myconnstrng); try { //sql Quary to add new category string sql = "INSERT INTO tbl_category (title, description, added_date, added_by) VALUES (@title, @description, @added_date, @added_by)"; //Creating SQL commond to pass our Quary SqlCommand cmd = new SqlCommand(sql, conn); //Passing Value through Parameter cmd.Parameters.AddWithValue("@title", c.title); cmd.Parameters.AddWithValue("@description", c.description); cmd.Parameters.AddWithValue("@added_date", c.added_date); cmd.Parameters.AddWithValue("@added_by", c.adde_by); //Opening Database connection conn.Open(); //Creating the int Variable to excute the Quary int rows = cmd.ExecuteNonQuery(); //If the quary is excuted succesfully then it value will be greater than 0 else it less than 0. if (rows > 0) { //Quary excuted Successfully isSuccess = true; } else { //Failed to excute the Quary isSuccess = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //closing database conn.Close(); } return(isSuccess); }
public bool update(categoryBLL c) { //create a boolean variable and set it default value to false; bool isSuccess = false; //creating SQL connection SqlConnection conn = new SqlConnection(myconnstrng); try { //Quary to update Category string sql = "UPDATE tbl_category SET title=@title, description=@description, added_date=@added_date, added_by=@added_by WHERE id=@id"; //Sql command to pass value to Sql Quary SqlCommand cmd = new SqlCommand(sql, conn); //passing value using cmd cmd.Parameters.AddWithValue("@title", c.title); cmd.Parameters.AddWithValue("@description", c.description); cmd.Parameters.AddWithValue("@added_date", c.added_date); cmd.Parameters.AddWithValue("@added_by", c.adde_by); cmd.Parameters.AddWithValue("@id", c.id); //Open Database connection conn.Open(); //Create int variable to Execute the Quary int rows = cmd.ExecuteNonQuery(); //if the quary is successfully executed the value will be greater than 0 if (rows > 0) { //Quary Executed succefully isSuccess = true; } else { //Failed to Execute Quary isSuccess = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); } return(isSuccess); }
public bool Delete(categoryBLL c) { //create a boolean variable and set it value to false bool isSuccess = false; SqlConnection conn = new SqlConnection(myconnstrng); try { //SQL quary to delete form database string sql = "DELETE FROM tbl_category WHERE id=@id "; SqlCommand cmd = new SqlCommand(sql, conn); //passing the value using cmd cmd.Parameters.AddWithValue("@id", c.id); //open SQL connection conn.Open(); int rows = cmd.ExecuteNonQuery(); //if the quary executed successfully then the value of rows is greater than 0 else it will less than 0; if (rows > 0) { //quary Executed successfully isSuccess = true; } else { //failed to execute the quary isSuccess = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); } return(isSuccess); }
public bool Insert(categoryBLL category) { bool isSuccess = false; SqlConnection connection = new SqlConnection(myconnectionstring); try { string sql = "INSERT INTO tbl_category (title, description, added_date, added_by) VALUES " + "(@title, @description, @added_date, @added_by)"; SqlCommand command = new SqlCommand(sql, connection); command.Parameters.AddWithValue("@title", category.title); command.Parameters.AddWithValue("@description", category.description); command.Parameters.AddWithValue("@added_date", category.added_date); command.Parameters.AddWithValue("@added_by", category.added_by); connection.Open(); int rows = command.ExecuteNonQuery(); // if the query is executed successfully then the value of rows will be greater than 0 else it will be less than 0 if (rows > 0) { //query is successfull isSuccess = true; } else { //query is failed isSuccess = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { connection.Close(); } return(isSuccess); }
public bool Update(categoryBLL category) { bool isSuccess = false; SqlConnection connection = new SqlConnection(myconnectionstring); try { string sql = "UPDATE tbl_category SET title = @title, description = @description, added_date = @added_date, added_by = @added_by where id = @id"; SqlCommand command = new SqlCommand(sql, connection); command.Parameters.AddWithValue("@title", category.title); command.Parameters.AddWithValue("@description", category.description); command.Parameters.AddWithValue("@added_date", category.added_date); command.Parameters.AddWithValue("@added_by", category.added_by); command.Parameters.AddWithValue("@id", category.id); connection.Open(); int rows = command.ExecuteNonQuery(); if (rows > 0) { //Query is successfull isSuccess = true; } else { //Query is failed isSuccess = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { connection.Close(); } return(isSuccess); }
public bool Delete(categoryBLL category) { bool isSuccess = false; SqlConnection connection = new SqlConnection(myconnectionstring); try { string sql = "DELETE from tbl_category where id = @id"; SqlCommand command = new SqlCommand(sql, connection); command.Parameters.AddWithValue("@id", category.id); connection.Open(); int rows = command.ExecuteNonQuery(); if (rows > 0) { //Query is successfull isSuccess = true; } else { //Query is failed isSuccess = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { connection.Close(); } return(isSuccess); }