Groups > Asp .Net > Getting started with ASP.NET > Is ViewState causing these problems?




Is ViewState causing these problems?

Is ViewState causing these problems?
Thu, 3 Apr 2008 14:12:33 +0000
I have a DataGrid with an editable Comment field; the desired effect is that
when the user clicks Update from the EditCommandColumn the _Update handler will
first check the Comments DB to see if a comment record already exists for the
selected record, then call the appropriate stored procedure (AddNewComment or
UpdateExistingComment), re-bind the DataGrid, and finally set the EditItemIndex
-1 (returning the DataGrid to the prte-edit state with the new comment added.)

Unfortunately what happens is, when AddNewComment is called, a record gets
created with the correct RecordID, but the CommentText does NOT get saved to the
DB.  Further, the DataGrid stays in the Edit state, and the Comment TextBox that
is created when in the Edit state reverts to its pre-update text (i.e. if the
comment was blank before the user input comments to the Comment TextBox, the
Comment TextBox is displayed blank.)  

Lastly, if comments WERE input to the Comment TextBox before Update was clicked,
when I click the Cancel link, I get an invalid postback/callback error - I'm not
as concerned about that just yet, unless it proves relevant to the reason why
the Comments are not being stored to the database.

I suspect that I am battling ViewState, but I think I need ViewState enabled to
work with the two calendar controls that build into the query string that drives
the resulting DataGrid, as well as to allow Sorting on the resulting DataGrid. 


 Any ideas would be greatly appreciated, I feel like I'm close to getting this
dang thing to work but not sure what to do next.  (Code below)

 

protected void dgShipping_Update(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {            
            try
            {
                if (SqlMethods.doesCommentRecordExist(e.Item.Cells[0].Text) ==
false)
                {
                    int insertRecord = doInsert(e.Item.Cells[0].Text,
((System.Web.UI.WebControls.TextBox)e.Item.Cells[15].Controls[0]).Text);
                    
                          
                    if (insertRecord != 1)
                    {
                        lblError.Text = "Error creating record " +
e.Item.Cells[0].Text + ".";
                    }
                    else
                    {                     
                        dgShipping.DataBind();
                        dgShipping.EditItemIndex = -1;
                    }                   
                }
                else
                {
                    int updateRecord = doUpdate(e.Item.Cells[0].Text,
((System.Web.UI.WebControls.TextBox)e.Item.Cells[15].Controls[0]).Text);        
          

                    if (updateRecord != 1)
                    {
                        lblError.Text = "Error updating record " +
e.Item.Cells[0].Text + ".";
                    }
                    else
                    {
                        dgShipping.DataBind();
                        dgShipping.EditItemIndex = -1;
                    }
                }
            }
            catch (Exception err)
            {
                lblError.Text = err.Message;
            }
        }

        protected int doUpdate(string RecordID, string CommentText)
        {
            return SqlMethods.updateExistingComment(RecordID, CommentText);
        }

        protected int doInsert(string RecordID, string CommentText)
        {
            return SqlMethods.addNewComment(RecordID, CommentText);
        }
Post Reply
about | contact