0

I have looked extensively to find an answer to this question but I only get extremely close. I have a web form that I use to add and edit records. When a record is selected in the gridview, a session variable is set and then used on page load to populate the text fields. If the session variable is not set, the form will be blank and the logic run as a new record. My problem is that I can add a new record successfully - I debugged and checked to make sure the asp controls passed the proper values to the code behind - but I cannot edit a record successfully. For some reason, the code behind file does not retrieve the proper values from the text boxes. Instead, it keeps the original populated values thus defeating the purpose of the edit. I imagine it is a binding issue but I am unsure and have searched upon end. Here is my code behind file:

protected void Page_Load(object sender, EventArgs e)
{
    resultOutput.Visible = false;//Output results as to whether or not a record was added successfully is automatically hidden at page load

    //Checking to see if session variable has been created
    if (Session["editID"] != null)
    {               
        //Create objects to get recipe data
        dbCRUD db = new dbCRUD();
        Recipe editRecipe = new Recipe();

        //Grabbing session ID 
        var id = Convert.ToInt32(Session["editID"]);

        //Call method to retrieve db data
        editRecipe = db.SelectRecord(id);

        //Populate results to text boxes
        recordID.Text = editRecipe.Recipe_ID.ToString();
        addName.Text = editRecipe.Name;
        addTypeDDL.SelectedValue = editRecipe.Meal;
        addDifficultyDDL.SelectedValue = editRecipe.Difficulty;
        addCookTime.Text = editRecipe.Cook_Time.ToString();
        addDirections.Text = editRecipe.Directions;

        //Change Button Text
        submitRecord.Text = "Edit Record";

        //Change Title Text
        addEditTitle.Text = "Edit Recipe";

    }
}

protected void submitRecord_Click(object sender, EventArgs e) 
{
    Recipe recipe = new Recipe();
    dbCRUD newRecord = new dbCRUD();

    //Variables for execution results
    var modified = "";
    int returned = 0;

    //Creating the recipe Object to pull the values from the form and 
    //send the recipe object as a parameter to the method containing insert stored procedure
    //depending on Add or Edit
    //recipe.Recipe_ID = int.Parse(recordID.Text);
    recipe.Name = addName.Text.ToString();
    recipe.Meal = addTypeDDL.SelectedValue.ToString();
    recipe.Difficulty = addDifficultyDDL.SelectedValue.ToString();
    recipe.Cook_Time = int.Parse(addCookTime.Text);
    recipe.Directions = addDirections.Text.ToString();


    //Checking to see if the page is loaded for edit or new addition
    if (Session["editID"] != null)
    {
        recipe.Recipe_ID = Convert.ToInt32(Session["editID"]);
        //If recordID exists, recipe will be passed to UpdateRecord method
        returned = newRecord.UpdateRecord(recipe);
        modified = "has been edited.";
        Session.Remove("editID");
    }
    else
    {
        //If recordID does not exist, record will be passed to InsertRecord method (new recipe)
        returned = newRecord.InsertRecord(recipe);
        modified = "added";
    }

    //Method returns 0 if successful, 1 if sql error, 2 if other error
    if (returned == 1)
    {
        resultOutput.Text = "There was an sql exception";
        resultOutput.Visible = true;
    }
    else if (returned == 2)
    {
        resultOutput.Text = "There was a non sql exception";
        resultOutput.Visible = true;
    }
    else
    {
        resultOutput.Text = "\"" + addName.Text + "\" recipe " + modified;
        resultOutput.Visible = true;
    }
}

Any object passed to my edit method is successful, however, as I mentioned, it does not grab the newly updated text box values.

1
  • So, is db.SelectRecord(id); called when the page reloads? Commented Jan 22, 2014 at 16:00

2 Answers 2

1

Did you try checking PostBack property , Your code is loading the data everytime the page is posted back. So when you update the values in the form and hit update button. The Page_Load method is called first and it reloads all the data (replaces your updated values on the form) and then hit the update button event handler. So everytime your old values are being saved.

You may remove the code from page_load method and put it where you are setting the Session["EditId"] value. This will solve your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

You got it!! Foolish mistake but yes - I put all the code firing on Page Load into a if(!IsPostBack){} control statement and all is good!
1

I would suggest using a static dataset and bind it to the recordsource of the gridview control. Whenever you wanna edit a record update the dataset simultaneously and rebind it to the gridview control....hope that helps:)

1 Comment

Thanks for the tip, Irfan. I am going to try the other methods posted above as it will require the least amount of code changes, but if I cannot get the expected results, I'll go with the static dataset option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.