public bool exist(Catagoriesbll u) { //Create a boolean variable and set its value to false and return it bool issuccess = false; //MEthod to connect Database connclass c = new connclass(); SqlConnection conn = new SqlConnection(c.connection); try { //SQL Query to selecte Data from DAtabase string query = "select * from tbl_catagories where catagory=@catagory AND supplier=@supplier AND company=@company"; //For Executing Command SqlCommand cmd = new SqlCommand(query, conn); //Passing Values to the Variables cmd.Parameters.AddWithValue("@catagory", u.catagory); cmd.Parameters.AddWithValue("@supplier", u.supplier); //Getting DAta from dAtabase SqlDataAdapter adapter = new SqlDataAdapter(cmd); //TO hold the data from database DataTable dt = new DataTable(); //Database Connection Open conn.Open(); //Fill Data in our DataTable adapter.Fill(dt); //Checking The rows in DataTable if (dt.Rows.Count > 0) { //Data Exist issuccess = true; } else { //Data not exist issuccess = false; } } catch (Exception ex) { //Throw Message if any error occurs MessageBox.Show(ex.Message); } finally { //Closing Connection conn.Close(); } return(issuccess); }
public bool update(Catagoriesbll u) { //Create a boolean variable and set its value to false and return it bool issucess = false; //MEthod to connect Database connclass c = new connclass(); SqlConnection conn = new SqlConnection(c.connection); try { //SQL Query to update Data in DAtabase string query = "UPDATE tbl_catagories set catagory=@catagory,supplier=@supplier,added_by=@added_by,added_date=@added_date WHERE id=@id"; //For Executing Command SqlCommand cmd = new SqlCommand(query, conn); //Passing Values to the Variables cmd.Parameters.AddWithValue("@id", u.id); cmd.Parameters.AddWithValue("@catagory", u.catagory); cmd.Parameters.AddWithValue("@supplier", u.supplier); //Database Connection Open conn.Open(); //To execute non query int row = cmd.ExecuteNonQuery(); //If the query is executed Successfully then the value to rows will be greater than 0 else it will be less than 0 if (row > 0) { //Query Sucessfull issucess = true; } else { //Query Failed issucess = false; } } catch (Exception ex) { //Throw Message if any error occurs MessageBox.Show(ex.Message); } finally { //Closing Connection conn.Close(); } return(issucess); }