locked
Row not being added to dataTable RRS feed

  • Question

  • Part of the code for adding a blank line to a gridview involves adding a blank row to a dataTable, then binding it to the gridview. It works fine the first time I call it for the first gridview. But the second time I call it for a second gridview, the code after the line that adds the row to the dataTable never executes and no errors are thrown (and the add doesn't happen either). It has me baffled. It's like it hits the add statement and just exits instead of doing anything. Here's the code:

     

        Public Sub drawEmptyGridview(ByVal gridView As GridView, ByVal dt As DataTable)
            Try
                If dt.Rows.Count = 0 Then
                    'Add a blank row to the datatable
                    Dim newGridRow As DataRow = dt.NewRow()
                    newGridRow("cifid") = 0  ' this can't be null
                    MessageBox("3gridview: " & gridView.ID.ToString & " " & dt.Rows.Count.ToString)
                    'The above message box shows correct results
                    dt.Rows.Add(newGridRow)
                    'Never hit code below
                    MessageBox("4gridview: " & gridView.ID.ToString & " " & dt.Rows.Count.ToString)
    
                    'Bind the DataSet to the GridView
                    gridView.DataSource = dt
                    gridView.DataBind()
                End If
            Catch ex As Exception
                Me.lblErrMsg.Text = "Error creating empty gridview: " & ex.Message
                Me.lblErrMsg.ForeColor = Drawing.Color.Red
            End Try
        End Sub
    
    
    What am I doing wrong?

     


    • Edited by GariusLupus Wednesday, September 21, 2011 2:37 PM
    • Moved by Mike Feng Friday, September 23, 2011 12:51 PM ASP.net (From:Visual Basic General)
    Wednesday, September 21, 2011 2:33 PM

Answers

  • I found the problem. Turns out one of the fields in the second gridview was a "not null" field, so the add wasn't working with the blank row, which had a null for that field. It's still a mystery why the try-catch didn't trap the error and print it. It may be that the label where the error message should have been written was overwritten.

    Anyway, thanks for all your help. :-D

    • Marked as answer by GariusLupus Friday, September 23, 2011 3:15 PM
    Friday, September 23, 2011 3:15 PM

All replies

  • Do you use the same instance of the data table for the second call? Then it will have one row and it will not try to add another row. 

    You can't reuse the data table if you want to setup the second grid with a new empty data table, you need to instantiate a new data table in that case, like how you did for first call.

    Wednesday, September 21, 2011 2:46 PM
  • Thanks for the quick response!

     

    The dataTable that gets passed in is a new one each time.

     

    The check for zero rows is to handle the case where the data table that is to be used to fill the gridview is empty. Then I want to display an empty gridview, showing the headers, with a blank row for editing.

    Wednesday, September 21, 2011 2:50 PM
  • Just a guess, you could try to issue a BeginEdit() on the new DataRow and the .AcceptChanges() on the DataTable -- you say that this is a new DataTable each time anyway, right?

     

    If dt.Rows.Count = 0 Then
         'Add a blank row to the datatable
          Dim newGridRow As DataRow = dt.NewRow()
          newGridRow.BeginEdit()
          newGridRow("cifid") = 0  ' this can't be null
          MessageBox("3gridview: " & gridView.ID.ToString & " " & dt.Rows.Count.ToString)
         'The above message box shows correct results
          dt.Rows.Add(newGridRow)
          dt.AcceptChanges()
          'Never hit code below
          MessageBox("4gridview: " & gridView.ID.ToString & " " & dt.Rows.Count.ToString)
    
         'Bind the DataSet to the GridView
          gridView.DataSource = dt
          gridView.DataBind()
    End If
    

     

     

     


    James Crandall ~ http://javitechnologies.com Spatial Database Solutions
    Wednesday, September 21, 2011 3:02 PM
  • I don't know that adding blank rows through data binding will work properly. You may have to add the data directly to the DataGridView.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Wednesday, September 21, 2011 3:07 PM
  • I just tried what you suggested, jamesfreddyc, and it still doesn't get past the dt.Rows.Add(newGridRow) statement, so it never gets to the acceptChanges.

    Paul - it works fine the first time, and where it is having trouble doesn't really have anything to do with the data binding - it's the dataTable stuff that is having trouble.

    Wednesday, September 21, 2011 3:22 PM
  • I just tried what you suggested, jamesfreddyc, and it still doesn't get past the dt.Rows.Add(newGridRow) statement, so it never gets to the acceptChanges.

    Paul - it works fine the first time, and where it is having trouble doesn't really have anything to do with the data binding - it's the dataTable stuff that is having trouble.


    That's kind of why I suggested working directly with the DataGridView.

    I'm not sure why you need the blank row. This isn't something that would normally be saved to a database, which the DataTable is designed to support.


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Wednesday, September 21, 2011 3:47 PM
  • The blank row is there so that the gridview actually displays when there is no data in the table that is meant to fill it.

    To explain, the gridview is on a form that may be initially blank for a new customer. The form is then filled out and the data saved in a database table. When the form is reloaded for that customer, then the existing data is filled in. So, for the new customer, if there is no existing data, the gridview wouldn't show up on the page at all. This is a work-around that I have seen in several places now on the web. Basically, I check if there is data for the customer, and if not, then I display a gridview with one blank row. To do that, I create a table with one blank row, then bind it to the gridview. Works like a charm for the first gridview on the form, but has this problem when I try to do it for the second gridview on the form.

    Wednesday, September 21, 2011 3:59 PM
  • I do you nowhere get the state of the datatable back. Be aware a Web page has no state, as soon as it is posted the part of the user which was handled is released.

    However, you can save your datatable in a session direct after the databind and get it back at IsPosBack

    Take a look at this page with samples

    http://msdn.microsoft.com/en-us/library/ms178581.aspx


    Success
    Cor
    Wednesday, September 21, 2011 4:08 PM
  • I don't understand how that applies here, Cor. All I'm trying to do is add a row to a datatable, but the second time the sub is called, it exits when it hits the add statement (the messagebox before that statement pops up with correct data, but the messagebox after that statement doesn't appear).
    Wednesday, September 21, 2011 4:32 PM
  • OK, well I didn't know it was an ASP.NET app. I think the only instance where I've had this issue I decided to display a message indicating that no information was available, rather than display an empty DataGridView.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Wednesday, September 21, 2011 4:39 PM
  • Yea there is something strange.

    While the method databind belongs to the GridView ASP.Net GridView you also using msgbox wich is for windows forms. In a webpage you have to make a response.Write to do the same . 

    http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx

     


    Success
    Cor
    Wednesday, September 21, 2011 4:48 PM
  • Paul - I could do that (display the "no info available" message), but then the user wouldn't be able to enter any data - there would be no footer row for entering, or no blank row for editing. What's frustrating is that it works perfectly once.

    Cor - the MessageBox call I make is to my own sub. For interest, here it is:

     

        Private Sub MessageBox(ByVal msg As String)
            ' creates a javascript alertbox containing the string passed in as argument
    
            Dim lbl As New Label()
            lbl.Text = "<script language='javascript'>" & Environment.NewLine & _
                               "window.alert('" + msg + "')</script>"
    
            ' add the label to the page. This fires the alertbox.
            Page.Controls.Add(lbl)
    
        End Sub
    
    

    Wednesday, September 21, 2011 5:01 PM
  • I see. If you don't get a solution here, you may want to post your question to the below ASP.NET forum:

    http://forums.asp.net/24.aspx/1?Web+Forms+Data+Controls

     


    Paul ~~~~ Microsoft MVP (Visual Basic)
    • Marked as answer by Mike Feng Friday, September 23, 2011 12:51 PM
    • Unmarked as answer by GariusLupus Friday, September 23, 2011 3:15 PM
    Wednesday, September 21, 2011 5:57 PM
  • Thanks Paul. I'll do that.
    Wednesday, September 21, 2011 6:00 PM
  • I found the problem. Turns out one of the fields in the second gridview was a "not null" field, so the add wasn't working with the blank row, which had a null for that field. It's still a mystery why the try-catch didn't trap the error and print it. It may be that the label where the error message should have been written was overwritten.

    Anyway, thanks for all your help. :-D

    • Marked as answer by GariusLupus Friday, September 23, 2011 3:15 PM
    Friday, September 23, 2011 3:15 PM