//function that sets the form to the information of the selected primary key private void BindData(int tableID) { //if the class if doesn't equal -1, set the page up as an update if (tableID != -1) { //make the button say update btnAddUpdate.Text = "Update"; //instantiate a new class of the form type, passing in the routeData primary key se256_Dobachesky.Table aTable = new se256_Dobachesky.Table(tableID); //if the data exists, fill the form with it's information if (aTable != null) { txtName.Text = aTable.tbl_name; txtDesc.Text = aTable.tbl_desc; ddlSection.SelectedValue = aTable.sect_id.ToString(); txtSeatCount.Text = aTable.tbl_seat_cnt.ToString(); chkIsActive.Checked = aTable.tbl_active; } } //if the class does equal -1, set the form up as an add else { btnAddUpdate.Text = "Add"; } }
protected void btnAddUpdate_Click(object sender, EventArgs e) { //instantiate a new class se256_Dobachesky.Table aTable; //if the routedata information exists, finish the class by passing in the primary key id if (RouteData.Values["tableID"] != null) { aTable = new se256_Dobachesky.Table(Convert.ToInt32(RouteData.Values["tableID"].ToString())); } //if the routedata information does not exist, finish the class with a blank class else { aTable = new se256_Dobachesky.Table(); } //set the object's properties to be equal to the information on the form aTable.sect_id = Convert.ToInt32(ddlSection.SelectedValue.ToString()); aTable.tbl_name = txtName.Text.Trim(); aTable.tbl_desc = txtDesc.Text.Trim(); aTable.tbl_seat_cnt = Convert.ToInt32(txtSeatCount.Text.Trim()); aTable.tbl_active = Convert.ToBoolean(chkIsActive.Checked.ToString()); //if the primary key exists go forward with the update if (aTable.tbl_id > 0) { //run the update method, and if it fails display an error message in a label if (se256_Dobachesky.Table.UpdateTable(aTable)) { Response.Redirect("/Admin/Tables"); } else { lblMessage.Text = "Table update failed!"; } } //if the primary key does not exist it is because the record does not exist yet, go forward with the add else { //run the insert method, and if it fails display an error message in a label if (se256_Dobachesky.Table.InsertTable(aTable)) { Response.Redirect("/Admin/Tables"); } else { lblMessage.Text = "Table insert failed!"; } } }
public static bool UpdateTable(se256_Dobachesky.Table aTable) { //start out with the boolean value at false bool blnSuccess = false; //create sql connection object that gets connection string from web.config SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SE256_CS"].ConnectionString); //create sql command as stored procedure SqlCommand cmd = new SqlCommand("tables_update", conn); //set the command as a stored procedure cmd.CommandType = CommandType.StoredProcedure; //add the parameters to be used in the update method cmd.Parameters.Add("@tbl_id", SqlDbType.Int).Value = aTable.tbl_id; cmd.Parameters.Add("@sect_id", SqlDbType.Int).Value = aTable.sect_id; cmd.Parameters.Add("@tbl_name", SqlDbType.VarChar).Value = aTable.tbl_name; cmd.Parameters.Add("@tbl_desc", SqlDbType.VarChar).Value = aTable.tbl_desc; cmd.Parameters.Add("@tbl_seat_cnt", SqlDbType.Int).Value = aTable.tbl_seat_cnt; cmd.Parameters.Add("@tbl_active", SqlDbType.Bit).Value = aTable.tbl_active; //try to open the connection and run the command, then set the boolean value to true try { conn.Open(); cmd.ExecuteNonQuery(); blnSuccess = true; } //prepare the error as a string and set boolean value to false catch (Exception e) { e.ToString(); blnSuccess = false; } //close the connection finally { conn.Close(); } //return the boolean containing information as to of if the operation was a success or not return(blnSuccess); }