• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

when datagrid returns no results

i know how to return the number of records in a datagrid using sql server, but i'm not sure with microsoft access.

i need to display a message if no records are returned.

here's the sql server code:

if( myDataSet.Tables(0).Rows.Count = 0 )
            response.write ("No results")
      else
            'Bind the DataSet to Datagrid
            dgSearchResults.DataSource = myDataSet
            dgSearchResults.DataBind()
      end if

how would i do this using access?

thanks, all.
0
vbnewbie01
Asked:
vbnewbie01
  • 4
  • 2
  • 2
1 Solution
 
b1xml2Commented:
it's the same.

DataTables and DataSets are another layer and have nothing to do with whether the data source is Access or SQL Server or MySQL or even Oracle(heaven forbid!)


Dim visible As Boolean = (myDataSet.Tables(0).Rows.Count > 0)
dgSearchResults.Visible = visible
'use a label to hold the text that you want to display
'when no records are returned.
lblNoResults.Visible = Not visible


'even if the datatable has no rows
'binding it to the grid will clear any existing item rows
'this will clear off any item rows inside the grid

dgSearchResults.DataSource = myDataSet.Tables(0)
dgSearchResults.DataBind()
0
 
vbnewbie01Author Commented:
hi, b1xml2 ... the thing is, i'm not using a dataset.

here's the full procedure below:


sub DisplayShowGenreSearchResults(byVal intSelectedGenre as integer)
      

      dim strConnection As Oledb.OledbConnection = New Oledb.OledbConnection(ConfigurationSettings.Appsettings("myDB"))
      
      strConnection.open
      
      Dim strSQL as String = "SELECT tblShows.fldShowID, tblShows.fldShowTitle " _
      & "FROM tblShowGenres INNER JOIN (tblShows " _
      & "INNER JOIN tblShowsGenresLink " _
      & "ON tblShows.fldShowID = tblShowsGenresLink.fldShowID) " _
      & "ON tblShowGenres.fldShowGenreID = tblShowsGenresLink.fldShowGenreID " _
      & "WHERE (((tblShowGenres.fldShowGenreID)=" & intSelectedGenre & ")) " _
      & "ORDER BY tblShows.fldShowTitle ASC"
      


Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, strConnection)
      

dgShowSearchResults.dataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
dgShowSearchResults.DataBind()

   
end sub
0
 
b1xml2Commented:
well then, you would have to change that behaviour:


Private Sub DisplayShowGenreSearchResults(ByVal intSelectedGenre As Integer)
      Dim con As New OleDbConnection(ConfigurationSettings.AppSettings("myDB"))
      Dim cmd As OleDbCommand = con.CreateCommand()
      Dim sql as String = "SELECT tblShows.fldShowID, tblShows.fldShowTitle " _
            & "FROM tblShowGenres INNER JOIN (tblShows " _
            & "INNER JOIN tblShowsGenresLink " _
            & "ON tblShows.fldShowID = tblShowsGenresLink.fldShowID) " _
            & "ON tblShowGenres.fldShowGenreID = tblShowsGenresLink.fldShowGenreID " _
            & "WHERE (((tblShowGenres.fldShowGenreID)=" & intSelectedGenre & ")) " _
            & "ORDER BY tblShows.fldShowTitle ASC"
      cmd.CommandText = sql
      Dim da As New OleDbDataAdapter(cmd)
      Dim ds As New DataSet()
      da.Fill(ds,"Genres")
      Dim dt As DataTable = ds.Tables("Genres")
      Dim visible As Boolean = (dt.Rows.Count > 0)
      dgSearchResults.Visible = visible
      'use a label to hold the text that you want to display
      'when no records are returned.
      lblNoResults.Visible = Not visible


      'even if the datatable has no rows
      'binding it to the grid will clear any existing item rows
      'this will clear off any item rows inside the grid

      dgSearchResults.DataSource = dt.DefaultView
      dgSearchResults.DataBind()

End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Hamed ZaghaghiProgrammerCommented:
after binding data to datagrid, use this code:

If dgsearchResults.Items.Count < 0 then
          dgSearchResult.Visible = false
          response.write("No result")
end if
0
 
Hamed ZaghaghiProgrammerCommented:
sub DisplayShowGenreSearchResults(byVal intSelectedGenre as integer)
     

     dim strConnection As Oledb.OledbConnection = New Oledb.OledbConnection(ConfigurationSettings.Appsettings("myDB"))
     
     strConnection.open
     
     Dim strSQL as String = "SELECT tblShows.fldShowID, tblShows.fldShowTitle " _
     & "FROM tblShowGenres INNER JOIN (tblShows " _
     & "INNER JOIN tblShowsGenresLink " _
     & "ON tblShows.fldShowID = tblShowsGenresLink.fldShowID) " _
     & "ON tblShowGenres.fldShowGenreID = tblShowsGenresLink.fldShowGenreID " _
     & "WHERE (((tblShowGenres.fldShowGenreID)=" & intSelectedGenre & ")) " _
     & "ORDER BY tblShows.fldShowTitle ASC"
     


Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, strConnection)
     

dgShowSearchResults.dataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
dgShowSearchResults.DataBind()

If dgsearchResults.Items.Count < 0 then
          dgSearchResult.Visible = false
          response.write("No result")
end if
   
end sub
0
 
Hamed ZaghaghiProgrammerCommented:
Oooops, my previous code is incorrect
0
 
Hamed ZaghaghiProgrammerCommented:
Ooops, my previous code must be correct ;)
0
 
vbnewbie01Author Commented:
thanks guys.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now