?
Solved

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

Posted on 2009-05-03
3
Medium Priority
?
467 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

771 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