Solved

how to check if datasource is empty before binding it to gridview in asp.net

Posted on 2009-05-03
3
453 Views
Last Modified: 2012-06-27
Hi

I am using the following code to get the data from database and bind it to gridview.

dim  mySqlString = "Select columname from table where xyz = 1"
Me.Gridview1.DataSource = CType(DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), myParam), IDataReader)
Me.Gridview1.DataBind()

How can check if the query returned no rows before binding it to database.

Thanks



0
Comment
Question by:Shiva-Kumar
3 Comments
 
LVL 9

Expert Comment

by:Deathrace
ID: 24290101
try to use a datareader, before binding the datasource of gridview1,hope the below might might give some hint for you.
http://www.adefwebserver.com/DotNetNukeHELP/Misc/code/DynamicForms_DynamicForms_DAL.vb.htm
0
 
LVL 6

Accepted Solution

by:
M3mph15 earned 400 total points
ID: 24292168
Hi,

Yes i agree. Use a datareader first to test. Below is an example connecting to a Oracle database but is easily modified for Sql Server and Access databases (the methods are pretty much the same it's just the control names that will cahnge).

Dim sql as String = "Select columname from table where xyz = 1"
 
Dim _source As String = "Data Source=[name of database];Persist Security Info=True;Password=[password];User ID=[username]" 'Replace [*] with relevant data
 
Dim _conn as new OracleConnection(_source)
Dim command As OracleCommand = _conn.CreateCommand()
command.CommandText = sql
 
Dim reader As OracleDataReader
 
Try
                _conn.Open()
                reader = command.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As Exception
              _conn.Close()
End Try
 
If reader IsNot Nothing Then
                If reader.HasRows() Then
                              'If it reaches this point then it does return data
 
                Else
                         'Code for record doesn't exist Insert/Update
                End If
                reader.Close()
Else
  'Error Handling for failure to create reader
End If


HTH
-M3mph15
0
 

Author Closing Comment

by:Shiva-Kumar
ID: 31577328
Thanks that worked.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now