1

This code seems simple enough on the surface but I am trying to display a message when no records are present in datalist.

I have this on markup:

<asp:DataList ID="DataList1" runat="server" CellPadding="4"
   DataSourceID="SqlDataSource1"
   Font-Bold="False" OnSelected="SqlDataSource1_Selected" Font-Names="Verdana"
   Font-Size="Small" RepeatColumns="2"
   RepeatDirection="Horizontal" Width="100%" ForeColor="#333333">
   <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
...
...
</asp:DataList>
<asp:label CssClass="Treb10Blue" ID="lblMsg" runat="server"></asp:Label> 

Then on codebehind, I have this:

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

  If e.AffectedRows = 0 Then
      lblMsg.Visible = True
      lblMsg.Text = "No records found"
  Else
      lblMsg.Text = ""
  End If

End Sub

I am not getting any errors but the message is not displaying.

Any ideas what could be wrong?

Csharp
  • 2,916
  • 16
  • 50
  • 77
Kenny
  • 1,058
  • 4
  • 20
  • 49

3 Answers3

2

This issue has been resolved. A much easier solution than I would have imagined.

   <FooterTemplate>
     <asp:Label forecolor="#9ACD32" Visible='<%# IIF(DataList1.Items.Count=0 And ddlLocation.SelectedItem.Value<>"0", "True", "False")%>' runat="server" ID="lblMsg" Text="No records found"></asp:Label>
    </FooterTemplate>
Kenny
  • 1,058
  • 4
  • 20
  • 49
0

This is an excerpt from MSDN, maybe it's what you're seeing:

All operations return the number of rows affected by the operation. The AffectedRows property has the same value as the return value of the Update, Insert, and Delete methods.

When the Select method is called and the data source is set to DataReader mode, the return value is 0 in all cases.

If that's the case, then maybe you can reverse your logic:

  • by default, display the label with the 'No records found'
  • create a handler for the the DataList's ItemDataBound event, and hide the label there (because in that case you have at least one record)
M4N
  • 94,805
  • 45
  • 217
  • 260
0

You probably want to check the number of items in your DataList. Try this:

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

  If DataList1.Items.Count = 0 Then
      lblMsg.Visible = True
      lblMsg.Text = "No records found"
  Else
      lblMsg.Text = ""
  End If

End Sub
Douglas Barbin
  • 3,595
  • 2
  • 14
  • 34
  • How can I tie your solution to dropdownlist control? It works, sort of. It displays that message whether there is value or not. So, we have dropdownlist control with 4 options. Perhaps if I can tie it to dropdownlist control, then if the value for the dropdownlist is 0 then then show the message. Can you help with this? – Kenny Oct 09 '13 at 23:34
  • Tie it to whatever your dropdown is called then. – Douglas Barbin Oct 10 '13 at 00:13
  • I asked, can you please help? That means that I don't know how. It is ok if you don't know how yourself but that was a non-answer. – Kenny Oct 10 '13 at 01:41
  • How on earth would I know what you named your dropdown? – Douglas Barbin Oct 10 '13 at 03:06
  • Just say you don't know how. Like I said, that's ok too. You don't need to know the name. A sample of how to tie your code to any a dropdown would suffice. My dropdown is called ddlLocation. – Kenny Oct 10 '13 at 12:44