Solved

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

Posted on 2009-05-03
3
456 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 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