Solved

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

Posted on 2009-05-18
6
408 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

21 Experts available now in Live!

Get 1:1 Help Now