protected void btnAddUpdate_Click(object sender, EventArgs e)
        {
            //instantiate a new class
            se256_Dobachesky.Section aSection;
            //if the routedata information exists, finish the class by passing in the primary key id
            if (RouteData.Values["sectionID"] != null)
            {
                aSection = new se256_Dobachesky.Section(Convert.ToInt32(RouteData.Values["sectionID"].ToString()));
            }
            //if the routedata information does not exist, finish the class with a blank class
            else
            {
                aSection = new se256_Dobachesky.Section();
            }

            //set the object's properties to be equal to the information on the form
            aSection.sect_name   = txtName.Text.Trim();
            aSection.sect_desc   = txtDesc.Text.Trim();
            aSection.sect_active = Convert.ToBoolean(chkIsActive.Checked.ToString());

            //if the primary key exists go forward with the update
            if (aSection.sect_id > 0)
            {
                //run the update method, and if it fails display an error message in a label
                if (se256_Dobachesky.Section.UpdateSection(aSection))
                {
                    Response.Redirect("/Admin/Sections");
                }
                else
                {
                    lblMessage.Text = "Section 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.Section.InsertSection(aSection))
                {
                    Response.Redirect("/Admin/Sections");
                }
                else
                {
                    lblMessage.Text = "Section insert failed!";
                }
            }
        }
Beispiel #2
0
        public static bool UpdateSection(se256_Dobachesky.Section aSection)
        {
            //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("sections_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("@sect_id", SqlDbType.Int).Value       = aSection.sect_id;
            cmd.Parameters.Add("@sect_name", SqlDbType.VarChar).Value = aSection.sect_name;
            cmd.Parameters.Add("@sect_desc", SqlDbType.VarChar).Value = aSection.sect_desc;
            cmd.Parameters.Add("@sect_active", SqlDbType.Bit).Value   = aSection.sect_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);
        }
Beispiel #3
0
        public static bool InsertSection(se256_Dobachesky.Section aSection)
        {
            //start out with the boolean equal to 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("sections_insert", conn);

            //set the command as a stored procedure
            cmd.CommandType = CommandType.StoredProcedure;

            //fill the parameters of the stored procedure
            cmd.Parameters.Add("@sect_name", SqlDbType.VarChar).Value = aSection.sect_name;
            cmd.Parameters.Add("@sect_desc", SqlDbType.VarChar).Value = aSection.sect_desc;
            cmd.Parameters.Add("@sect_active", SqlDbType.Bit).Value   = aSection.sect_active;

            //try to open the connection and run the update, then set the boolean to true
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                blnSuccess = true;
            }
            //prepare the error as a string and set boolean to false
            catch (Exception e)
            {
                e.ToString();
                blnSuccess = false;
            }
            //close the connection
            finally
            {
                conn.Close();
            }

            //return whether or not the method was successful
            return(blnSuccess);
        }
 //function that sets the form to the information of the selected primary key
 private void BindData(int sectionID)
 {
     //if the class if doesn't equal -1, set the page up as an update
     if (sectionID != -1)
     {
         //make the button say update
         btnAddUpdate.Text = "Update";
         //instantiate a new class of the form type, passing in the routeData primary
         se256_Dobachesky.Section aSection = new se256_Dobachesky.Section(sectionID);
         //if the data exists, fill the form with it's information
         if (aSection != null)
         {
             txtName.Text        = aSection.sect_name;
             txtDesc.Text        = aSection.sect_desc;
             chkIsActive.Checked = aSection.sect_active;
         }
     }
     //if the class does equal -1, set the form up as an add
     else
     {
         btnAddUpdate.Text = "Add";
     }
 }