There is code in the project I am working on that increments. Basically the user selects from a drop down list and than there is a button that increments the number for that option, to record how many times they've done something. Now I need to write code to decrement incase they press the increment to many times. The team member that has done the increment side has used a stored proc. The code they have placed behind the increment is

   protected void lbIncrementOccur_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                pnlValidation.Visible = false;

                //build object to insert occurrance
                Advert a = new Advert();

                a.AdvertTypeID = Convert.ToInt32(ddlAdvertType.SelectedValue);
                a.AdvertAppRef = Session["SessAppRef"].ToString();
                a.AdvertOccurrance = 1;

                try
                {
                    //insert advert object into db
                    AdvertManager.InsertAdvertOccurranceByAppRef(a);

                    //rebind the gridview
                    gvAdvertHistory.DataBind();

                    //reset the advert occurance label
                    lblAdvertOccurrances.Text = CountAdvertsByAppRef();

                }
                catch (Exception ex)
                { 

                }
            }
            else
            {
                pnlValidation.Visible = true;
            }
        }

        //method to count the occurances of an advert type
        protected string CountAdvertsByAppRef()
        {
            String ret = "None";

            //get count of advertisements by advert type
            LinkedList<Advert> adList = new LinkedList<Advert>();
            adList = (LinkedList<Advert>)AdvertManager.GetAdvertCountByAppRef(Session["SessAppRef"].ToString());

            String AdvertType = ddlAdvertType.SelectedItem.Text;

            //loop through linked list and retrieve count related to selected advert type
            foreach (Advert a in adList)
            {
                if (a.AdvertTypeDescription.ToString() == AdvertType)
                {
                    ret = a.AdvertOccurrance.ToString();
                }
            }

            return ret;

For decrement will the code be similar? If not what do I need to do differently.

You could alsways just disable the control when clicked (MyButton.Enabled=false;)
or try something like

int targetValue = -1; //The value you do not want to exceed.

if (targetValue <= a.AdvertOccurrence)
{
    try
    {
        //insert advert object into db
        AdvertManager.InsertAdvertOccurranceByAppRef(a);
        //rebind the gridview
        gvAdvertHistory.DataBind();
        //reset the advert occurance label
        lblAdvertOccurrances.Text = CountAdvertsByAppRef();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.