//inserting data in to datbase public bool Insert(SubgroupID sgid) { //Creating a default reture type and setting its value to false bool isSuccess = false; //step 1 Connect databse SqlConnection conn = new SqlConnection(myconnstrng); try { // step 2 creat a sql query to insert data string sql = "INSERT INTO subgroupidtb (sGroupIds) VALUES (@sGroupIds) "; // creat sql Command using sql conn SqlCommand cmd = new SqlCommand(sql, conn); //Creating to parameter to add data cmd.Parameters.AddWithValue("@sGroupIds", sgid.sGroupIds); //connection open here conn.Open(); int rows = cmd.ExecuteNonQuery(); //if the query runs successfuly then the value of row will be greater then zero els its value will be 0 if (rows > 0) { isSuccess = true; } else { isSuccess = false; } } catch (Exception ex) { } finally { conn.Close(); } return(isSuccess); }
//creat method to delete data from database public bool Delete(SubgroupID sgid) { //creat a default return value and set its value to false bool isSuccess = false; //creat sql Connection SqlConnection conn = new SqlConnection(myconnstrng); try { //sql to delete data string sql = "DELETE FROM subgroupidtb WHERE sSubgroupIdt=@sSubgroupIdt"; //creating sql command SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@sSubgroupIdt", sgid.sSubgroupIdt); //open connection conn.Open(); int rows = cmd.ExecuteNonQuery(); //if the query runs successfuly then the value of row will be greater then zero els its value will be 0 if (rows > 0) { isSuccess = true; } else { isSuccess = false; } } catch (Exception ex) { } finally { //close connection conn.Close(); } return(isSuccess); }