473,382 Members | 1,814 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,382 software developers and data experts.

Prompt no data when there no data in gridview

Hi All,

Im using VWD 2008 and C#. Im using the code for search data. The seaching is success and no error. But here I want to add is, when the gridview return null value (which means there is no data from search query)...
I want to show label "No Data".

I tried to add this code to the code below:

Expand|Select|Wrap|Line Numbers
  1. if (gvTicket.DataSource == null)
  2.         //{if (GridView1.DataSource == null)
  3.         {
  4.             lbNodata.Visible = true;
  5.             Label6.Visible = false;
  6.             gvTicket.Visible = false;
  7.  
  8.         }
  9.        else
  10.        {
  11.            gvTicket.Visible = true;
  12.            Label6.Visible = true;
  13.        }

Expand|Select|Wrap|Line Numbers
  1. string connectionString = "Data Source=(local);Initial Catalog=DBhelpdesk;  User ID=;Password=";
  2.         SqlConnection myConnection = new SqlConnection(connectionString);
  3.         // SELECT * FROM Ticket where " + strSqlParam
  4.         string strSqlParam = "";
  5.         if (CbExactWord.Checked)
  6.         {
  7.             strSqlParam = ddlistColumn.SelectedValue + "='" + txtSearchBox.Text.Trim() + "'";
  8.         }
  9.         else
  10.         {
  11.             strSqlParam = ddlistColumn.SelectedValue + " like '%" + txtSearchBox.Text.Trim() + "%'";
  12.         }
  13.  
  14.         SqlCommand sql = new SqlCommand("SelectTicketSearch", myConnection);
  15.         sql.CommandType = CommandType.StoredProcedure;
  16.         sql.Parameters.AddWithValue("@strSqlParam", strSqlParam);
  17.         //Response.Write(strSqlParam);
  18.  
  19.         SqlDataAdapter ad = new SqlDataAdapter(sql);
  20.  
  21.         DataSet ds = new DataSet();
  22.         ad.Fill(ds, "Ticket");
  23.  
  24.         gvTicket.DataSource = ds;
  25.         gvTicket.DataBind();
  26.  
  27.         if (gvTicket.DataSource == null)
  28.         //{if (GridView1.DataSource == null)
  29.         {
  30.             lbNodata.Visible = true;
  31.             Label6.Visible = false;
  32.             gvTicket.Visible = false;
  33.  
  34.         }
  35.        else
  36.        {
  37.            gvTicket.Visible = true;
  38.            Label6.Visible = true;
  39.        }
Pls help me. Thanks in advance.

-Meshack
Nov 4 '08 #1
3 1266
r035198x
13,262 8TB
Testing for the DataSouce itself being null is not the correct way to do it. If the search returns no data the DataSource will not be null.
You need to test the data returned itself.
Nov 4 '08 #2
kenobewan
4,871 Expert 4TB
An alternative is to user a data reader and then check whether it has rows. If it does bind the reader to the gridview, else display message; maybe JS using attributes add. As r035198x says, the key is to check whether data is returned.
Nov 4 '08 #3
Thanks r035198x and kenobewan,

Can you tell me how to check whether data is returned to gridview? Im still a newbie and learning. Can you help me?

Thanks again.

-Meshack
Nov 5 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
4
by: Hans Merkl | last post by:
Hi, Is there a way to show the column headers of a GridView control even if there is no data? The only thing I see is the EmptyDataTemplate but I would also like to display the column headers. ...
4
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
1
by: Siva | last post by:
Hi, I have a 3 tier ASP.Net app for a handheld which needs to fetch orders from database via a DAL and populate it in a gridview using objectdatasource. In the search page I have a few parameters...
5
by: sutphinwb | last post by:
Hi - This could be a simple question. When I relate two tables in a datasetet, how do I get that relation to show up in a GridView? The only way I've done it, is to create a separate table in the...
13
by: Tomasz Jastrzebski | last post by:
Helo All, The problem: GridView control does not render at all (header/footer) when the data source is empty. I have seen a similar question posted already, but I just can not believe there is...
6
by: rcoco | last post by:
Hi, I have a datagrid that is ment to insert data. But when I run the form only the header appears. I would like some advise from you all and solve this problem I'm using visual studio 2003. My...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.