473,378 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Gridview displays old data

hi,
I have a gridview that is based on parameters. When the select statement
does not return any rows, I want to display a label that says 'nothing
found'. It works the first time there is not data, but after that, it
displays what the previous select statement retrieved. How do I fix it so
that it doesn't do that? I am using a datareader. This is my code:
[code]
cn.Open();
System.Data.SqlClient.SqlDataReader dr ;
dr = cmd.ExecuteReader();

if (dr.HasRows)
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
lblNoData.Visible = (GridView1.Rows.Count == 0);
dr.Close();
cn.Close();
}
Jun 27 '08 #1
2 1942
maggie wrote:
I have a gridview that is based on parameters. When the select statement
does not return any rows, I want to display a label that says 'nothing
found'. It works the first time there is not data, but after that, it
displays what the previous select statement retrieved. How do I fix it so
that it doesn't do that? I am using a datareader.
Your gridview is retaining the data from the previous time data was bound to
it. To clear this, set the gridview's datasource to null and rebind.

if (dr.HasRows)
{
GridView1.DataSource = dr;
GridView1.DataBind();
} else {
GridView1.DataSource = null;
GridView1.DataBind();
}

Jun 27 '08 #2
Thanks Ben. I will try it.

"Ben Amada" wrote:
maggie wrote:
I have a gridview that is based on parameters. When the select statement
does not return any rows, I want to display a label that says 'nothing
found'. It works the first time there is not data, but after that, it
displays what the previous select statement retrieved. How do I fix it so
that it doesn't do that? I am using a datareader.

Your gridview is retaining the data from the previous time data was bound to
it. To clear this, set the gridview's datasource to null and rebind.

if (dr.HasRows)
{
GridView1.DataSource = dr;
GridView1.DataBind();
} else {
GridView1.DataSource = null;
GridView1.DataBind();
}
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: hammad.awan_nospam | last post by:
Hello, I am using ASP.NET 2.0. What I have done is nested a gridview inside another column of a gridview using a template data field column declaritively in my web form. Inside this child...
5
by: yefei | last post by:
In my web design, I display records from a SQL DataBase according to some filters the GridView is defined with select commands and selest parameters however, I also want to display all records...
1
by: Giovanni | last post by:
Dear Friends/Gurus, I have exhausted myself and have yet no solution to the following: I have an ASP.NET 2.0 Survey type application. On a page, I have placed a GridView which is bound to an...
2
by: MRW | last post by:
Hello! I don't know if this is possible, but I have two tables in a one-to-many relationship. I want one record to be displayed in a GridView from table1 with a blue background, followed by all...
4
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have a vb.net 2.0 app that is loading a GridView with a DataSource that is returned from a function. The definitions in the function are: Dim ReportDS As DataSet = New DataSet Dim...
0
by: jaz215 | last post by:
Hi i have an update page where i update data when a user select a row in the gridview, i also have a search function that displays the data in the gridview all those data that matched the search. my...
4
by: mohaaron | last post by:
This seems like it should be simple to do but for some reason I have been unable to make it work. I would like to databind a SqlDataSource to a GridView during the click event of a button. This...
3
by: RobertTheProgrammer | last post by:
Hi folks, I've got another problem. Basically, I'm trying to use a nested GridView, however the nexted GridView displays no values (even though in debug I'm getting valid values into my DataSet. ...
0
by: =?Utf-8?B?ZWdzZGFy?= | last post by:
Hi, I have created a second gridview when customer clicks on select link with this code: Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.