My code fills a DataGrid control (vb.net 3.5 framework) using sqldataadapter and dataset, filtered by selections from a combination of other controls. Everything works fine except RadioButtonList1 with 3 rbtns under one condition:

  1. The issue arises when radio button 1 @ index 0 is selected AFTER rbtn 3 @ index 2 was previously selected AND it correctly returned NO DATA.

  2. This does not happen when rbtn 3 @ index 2 was previously selected and it correctly returned SOME DATA. All other combinations of radiobuttonlist selections behave correctly

I have tried filling datagrid using sqldataadapter differently-see code, including using stored procedures (but I can't custom sort if I do). Nothing seems to change this odd behavior.

I may have to resort to change radio button 1 to a checkbox control and use a separate binding method if no one can help me see the light. Can anyone see anything obvious I am missing?

ASPX (datagrid is showing only 2 columns for brevity)

<asp:radiobuttonlist   id="RadioButtonList1" runat="server" AutoPostBack="True" 
                            Font-Names="Arial" 
                            Font-Size="X-Small"   
                            BorderColor="Black" 
                            ForeColor="Black" 
                            RepeatDirection="Vertical" 
                            TextAlign="Right" CssClass="radioWithProperWrap" RepeatColumns="1"> 
                        <asp:ListItem Value="0" >All Projects</asp:ListItem> 
                        <asp:ListItem Value="1" Selected="True" >Sort By Project#</asp:ListItem> 
                        <asp:ListItem Value="2">Sort By Project Name</asp:ListItem>
</asp:radiobuttonlist>

<!--------------------
DataGrid1 
-------------------->            
<asp:datagrid  id="DataGrid1" runat="server"
                Font-Names="Tahoma,Sans Serif,arial" 
                Font-Size="Small" 
                ShowFooter="True" 
                DataMember="vwPersonSummary" 
                AllowSorting="True"
                OnPageIndexChanged="DataGrid1_Paging" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
         UseAccessibleHeader="True" AutoGenerateColumns="False" 
         CssClass="nowrap" AllowPaging="True" PageSize="110" Visible="False"   >

                <FooterStyle HorizontalAlign="Right" Font-Bold="True" Font-Italic="False" 
                    Font-Overline="False" Font-Strikeout="False" Font-Underline="False"></FooterStyle>
                <SelectedItemStyle HorizontalAlign="Left" />
                <PagerStyle HorizontalAlign="Left" Mode="NumericPages" 
                    Position="TopAndBottom" PageButtonCount="75" />
                <AlternatingItemStyle BackColor="#EBF5FF"></AlternatingItemStyle>
                <headerStyle Font-Bold="True" Font-Italic="False" Font-Overline="False" 
                    Font-Size="Small" Font-Strikeout="False" Font-Underline="False" 
                    Width="12em" BackColor="#333333" ForeColor="White"  ></headerStyle>

                <ItemStyle Font-Size="Smaller" Width="12em" />

<Columns>

<asp:TemplateColumn HeaderText="Line#" FooterText="Line#">
                        <FooterStyle Font-Bold="True" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" 
                            Font-Names="Tahoma" Font-Size="Small" ForeColor="Black"  />
<headerStyle HorizontalAlign="Center" Font-Bold="True" Font-Italic="False" 
                            Font-Names="Tahoma" Font-Overline="False" Font-Size="Small" 
                            Font-Strikeout="False" Font-Underline="False" ForeColor="White"></headerStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<%#(DataGrid1.PageSize * DataGrid1.CurrentPageIndex) + Container.ItemIndex + 1%> <!--generate row numbers-->
</ItemTemplate>
</asp:TemplateColumn>

<asp:BoundColumn DataField="Person" SortExpression="Employee" HeaderText="Person" >
  <FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" 
Font-Strikeout="False" Font-Underline="False" Wrap="False" />
  <headerStyle HorizontalAlign="Center" Font-Bold="True" Font-Italic="False" 
                            Font-Overline="False" Font-Size="Small" Font-Strikeout="False" 
                            Font-Underline="False" Wrap="False" Font-Names="Tahoma" 
                            ForeColor="White"  > </headerStyle>
  <ItemStyle   Font-Bold="False" Font-Italic="False" 
                            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                            HorizontalAlign="Left" Wrap="False"></ItemStyle>
</asp:BoundColumn>

<asp:BoundColumn DataField="Project#" SortExpression="Project#" HeaderText="Proj#">
  <FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" 
Font-Strikeout="False" Font-Underline="False" Wrap="False" />
  <headerStyle HorizontalAlign="Center" Font-Bold="True" 
Font-Italic="False" Font-Overline="False" Font-Strikeout="False" 
Font-Underline="False" Wrap="False" Font-Names="Tahoma" Font-Size="Small" 
ForeColor="White"  ></headerStyle>
  <ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" 
                            Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Left" 
                            Wrap="False" />
</asp:BoundColumn>
</Columns>              
</asp:datagrid>

ASPX.VB (only the pertinent code)

  Sub BindData(ByVal strSortField As String)

        ''clear grid
        DataGrid1.DataSource = Nothing 'original

        ' TimeCodesSelected variable is populated based on Checkbox selections here. 
 ' Code removed for brevity.   

        Try
            ' Selected project 
            Dim ProjectSelected As String = Left(ddwnProjList.SelectedItem.Text, 7) 'Project#

            ' convert string dates to DateTime data types
            PPBegDate = Convert.ToDateTime(txtBegDate.Text)
            PPEndDate = Convert.ToDateTime(txtEndDate.Text)

            ' Check which radio button is selected 
            If RadioButtonList1.SelectedValue = 0 Then '0 = All projects

                ProjectSelected = String.Empty

'inline sql used to provide sort parameters to allow retaining sort order when paging through grid. sqlQuery is a global variable
                sqlQuery = "SELECT Person, Project# WHERE dtmDate BETWEEN '" & PPBegDate & "' AND '" & PPEndDate & "' " & TimeCodesSelected & " ORDER BY " & ViewState("sortField").ToString() & " " & ViewState("sortDirection").ToString()


                ''1=by number, or 2=by name
            Else
                sqlQuery = "SELECT Person, Project#, WHERE Project#='" & ProjectSelected & "' AND dtmDate BETWEEN '" & PPBegDate & "' AND '" & PPEndDate & "' " & TimeCodesSelected & " ORDER BY " & ViewState("sortField").ToString() & " " & ViewState("sortDirection").ToString()

                'Verify selected project exists, if not hide grid
                Dim blnRecordExists As Boolean

                Dim oRecord As DataAccessLib.SqlConn = New DataAccessLib.SqlConn()
                blnRecordExists = oRecord.VerifyProjectExists(ProjectSelected, PPBegDate, PPEndDate)

                If (blnRecordExists = True) Then
                    DataGrid1.Visible = True
                    lblErrorMsg.Text = Nothing
                Else
                    DataGrid1.Visible = False
                    lblErrorMsg.Text = "No records found for this project number during selected dates."
                End If
            End If

            Using conn As SqlConnection = New SqlConnection(conStr)
                Using cmd As New SqlCommand(sqlQuery, conn)
                    cmd.CommandType = CommandType.Text
                    Using sda As New SqlDataAdapter(cmd)
                        Using ds As New DataSet()
                            sda.Fill(ds)
                            DataGrid1.DataSource = ds.Tables(0)
                        End Using
                    End Using
                End Using

''Orignal code that works as well and also has the switch from index 2 to index 0 issue 
'SqlDataAdapter1.Fill(DsDetailByProj1, "vwPersonSummary") 
'Dim objDataView As DataView = DsDetailByProj1.Tables("vwPersonSummary").DefaultView
'DataGrid1.DataSource = objDataView
                DataGrid1.DataBind()

            End Using 'end using closes conn and goes before 'Catch' 

        Catch ex As SqlException
            sqlErrorMsg = sqlErrorMsg + ": " + ex.Message.ToString
            lblErrorMsg.Visible = True
            lblErrorMsg.Text = sqlErrorMsg

        Catch e As Exception
            'reset grid to 1st page after grid refresh (databind)
            DataGrid1.CurrentPageIndex = 0


        Finally 'the using statement closes objects.  Kept here for reference only.
            ''close and dispose of database connection
            'SqlConnection1.Close()
            'SqlConnection1.Dispose()

            ''close sqldatadapter if applicable  
            'SqlDataAdapter1.Dispose()
        End Try

    End Sub
Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButtonList1.SelectedIndexChanged

        If RadioButtonList1.SelectedValue = 0 Then
            ddwnProjList.Enabled = False 

        Else 'if 1 or 2
            ddwnProjList.Enabled = True

        End If

        GetProjectList()'fills ddwnProjList by Project Number or Project Name

        BindData("dtmDate")  

    End Sub
Private Sub ddwnProjList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddwnProjList.SelectedIndexChanged

        If (ViewState("sortField") <> Nothing) Then
            BindData(ViewState("sortField").ToString())
        Else
            BindData("dtmDate")
        End If

    End Sub

enter code here
1

There are 1 best solutions below

0
On BEST ANSWER

As I mentioned in my comment, setting visibility property of the DataGrid to true was 1st step to solving this.

Then I used Scott Mitchell's answer here to verify if the grid was empty. This seems more efficient than the way I was doing it by eliminating the need for another round-trip to the database.

       If (DataGrid1.Items.Count = 0) Then
            lblNoRecordsMessage.Text = "Yo Adrian, there are no records for the selected dates."
            lblNoRecordsMessage.Visible = True
            DataGrid1.Visible = False
        Else
            lblNoRecordsMessage.Text = Nothing
            lblNoRecordsMessage.Visible = False
            DataGrid1.Visible = True
        End If

I also made sure the DataGrid and Label were set to visible at the beginning of the DataBind() method, since there is the possibility it will be left in the unvisible state after the last databinding.

        lblNoRecordsMessage.Text = Nothing
        lblNoRecordsMessage.Visible = False
        DataGrid1.Visible = True