Solved

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

Posted on 2009-05-18
6
413 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
[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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

622 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