Solved

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

Posted on 2009-05-18
6
409 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 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

23 Experts available now in Live!

Get 1:1 Help Now