Solved

Gridview in ASP.NET VB and a label - Pretty Noob stuff.

Posted on 2009-05-18
6
411 Views
Last Modified: 2012-06-27
Hi everyone.

I have a stupid question I was hoping someone would help with. I'm tired and just can't think anymore.

I have an website with three drop down lists that are attached to an Access Database.

I have a gridview that holds an SQL Statement to query the database based on the DDL controls.

SO if you select location as Florida in the drop down, the statement reads: "SELECT * FROM table WHERE location = Florida"

Make Sense?

If you hit a button, the page refreshes, the SQL Statement Runs and a gridview of the query results is displayed.

Here's my question.

When that GridView displays I have a hidden label called "lblStatusSearch" that I want to remain blank (and therefore hidden).

BUT when the gridview DOES NOT display (ie the SQL query yields no results I want that label to say "Try Again"

I know the VB code for the label, I'm not quite THAT new (lblStatusSearch.Text = "try again")

I just don't know where to put the IF statement to cause it to appear!

Here's what I got. Let me know if you need more. Thanks
Imports System.Data.SqlClient
 
Partial Public Class frmClient
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
    End Sub
 
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReset.Click
 
        Response.Redirect("~/frmClient.aspx")
 
    End Sub
 
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
 
    End Sub
 
    Protected Sub gvResults_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gvResults.SelectedIndexChanged
 
        If gvResults.Visible Then
            lblStatusSearch.Text = ""
        Else
            lblStatusSearch.Text = "Try Again"
        End If
 
    End Sub
 
    Protected Sub tblUsersGridView_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles tblUsersGridView.Selecting
 
    End Sub
End Class

Open in new window

0
Comment
Question by:IncubusJax
6 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 125 total points
ID: 24419068
I would put in the gridview databound event something like

if GridView1.Rows.Count = 0 then
  lblStatusSearch.Visible =  true
else
  lblStatusSearch.Visible =  false
end
0
 
LVL 11

Assisted Solution

by:Muhammad Ousama Ghazali
Muhammad Ousama Ghazali earned 125 total points
ID: 24419199
Instead of using a separate label I'll recommend using the EmptyDataTemplate of the GridView. Use the Source or Design mode in VS to edit this templated as per your requirements, put a label in it, some hyperlink, upto you. But it will automatically appear when there are no records to be displayed after grid is bound.
0
 
LVL 2

Assisted Solution

by:krishrr
krishrr earned 125 total points
ID: 24419226
GridView control has default property 'EmptyDataText'. You can try the following

<asp:GridView id="grid1" runat="server" EmptyDataText="Try Again!" />
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 20

Assisted Solution

by:ddayx10
ddayx10 earned 125 total points
ID: 24419263
Yeah...

I don't even think you have to use the rowdatabound here. As long as you check the gridview.rows.count after databinding has occurred it should give you an accurrate count and you can fill the label as necessary.

As I think about it if you had a label on each line then you woiuld use rowdatabound to evaluate some value in the line, but here we are talking about the database yielding no records at all. I don't even know if rowdatabound will be called in that case.

Since your code doesn't show the databinding process Im guessing your using a sqldatasource? I think that binds in the pre_render event.. So you could just put your code in page load:

        If gvResults.Rows.Count > 0 Then
            lblStatusSearch.Text = ""
        Else
            lblStatusSearch.Text = "Try Again"
        End If

Worst case scenario put it in page_loadcomplete (I dont think that should be necessary).

dday
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24421401
i didnt say use the rowdatabound event, i said put that code in the gridview databound event. It always fires when the gridview goes through the binding cycle.  I like to place the code here because no matter what caused the bind (manual force, sorting, paging) this event will fire. But I agree with the emptyTemplate, if you can,  use that.  I based my response assuming the use of the label was part of your requirements.
0
 

Author Closing Comment

by:IncubusJax
ID: 31582884
Thanks everyone. Actually, you were all correct and led me to my solution. I ended up just using the empty data text property of the gridview but I tested and considered all.

You guys are great!

Thanks
Mark
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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