Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If Datalist returns no data, then ...

Status
Not open for further replies.

MojoZig

Technical User
Sep 27, 2005
61
US
Got a simple question, if I have this datalist below that gets populated via a sql database.

Code:
            <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black">
                <ItemTemplate>
                    <br />
                    Account:
                    <asp:Label ID="AccountLabel" runat="server" Text='<%# Eval("Account") %>'></asp:Label><br />
                    Name:
                    <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'></asp:Label><br />
                    Amount:
                    <asp:Label ID="AmountLabel" runat="server" Text='<%# Eval("Amount", "{0:C}") %>'></asp:Label><br />
                    &nbsp;<br />
                    <asp:HiddenField ID="HFAccount" runat="server" Value='<%# Eval("Account") %>' />
                    <asp:HiddenField ID="HFName" runat="server" Value='<%# Eval("Name") %>' />
                    <asp:HiddenField ID="HFAmount" runat="server" Value='<%# Eval("Amount") %>' />
                </ItemTemplate>
                <FooterStyle BackColor="Tan" />
                <SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                <AlternatingItemStyle BackColor="PaleGoldenrod" />
                <HeaderStyle BackColor="Tan" Font-Bold="True" />
            </asp:DataList>

How do I write the codebehind to response.write("That account number is invalid!") if no data is returned?

Would I use ItemCreated or ItemDatabound?

Would it be something like (which I know is incorrect):

Code:
    Protected Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemCreated

        If DataList1 Is Nothing Then
        Response.Write("That Account Number was not found, please check it and try again!")

        End If
    End Sub



 
If you don't get any items back then ItemCreated or ItemDatabound won't run (or at least not run with ItemType Item or Alternating Item?).

In any case, I guess the easiest way would be to check the item count after you bind the datalist, so something like

Code:
Datalist1.DataSource = ...
Datalist1.Databind

If DataList1.Items.Count = 0 then
   Response.Write("That Account Number was not found!")
End If
 
In your case, you probably want to add a handler for a SqlDataSource event, namely "Selected", and do the above in there.
 
Thanks for the replies. I really appreciate your time. I have a few questions, please excuse the lack of knowledge on my part.

My SqlDataSource is "SqlDataSource1" for the DataSourceID for DataList1.

So, knowing that: when you say DataList.Datasource = ... does "..." represent my datasource of SqlDataSource1?

If so, It gets underlined with an error saying it can't converted to a string and then the page error states that both DataSource and DataSourceID are both present. so I changed DataSource to DataSourceID ... is this incorrect? It does gets rid of that error but then gets the other about can't be converted to a string.

I did created the handler for the SqlDataSource1 and put the code there.

Code:
    Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected

        'DataList1.DataSourceID = SqlDataSource1
        'DataList1.DataBind()

        If DataList1.Items.Count = 0 Then
            lblNoAccount.Text = ("That Account Number was not found!")

        End If
    End Sub


If I rem out the first two lines, the label gets the text value whether it returns data or not ...

Do you see the error, cause I certainly don't!? Thanks again, Most Appreciative! Suggestions? Thoughts? :eek:)
 
I think I figured it out! Let me know if you know why this couldn't be done this way:

Code:
    Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected
        If e.AffectedRows = 0 Then
        lblNoAccount.Text = ("That Account Number was not found!")

        End If
    End Sub

It seems to work properly ... and I totally stumbled upon it by accident using what you both told me to do. Do either of you see any proplems with e.AffectedRows = 0??

Let me know your thoughts and I really appreciate your time on helping me out!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top