Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-05-03
3
Medium Priority
?
471 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

Technology Partners: 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!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

610 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