displaying a default value for dropdownlist if no records found

[color=Black]Hi guys,

The codes below is running perfectly fine.

But I have a problem - if there are no records found in the database, my dropdownlist will not display nothing.

However, I want it to display something like "no records found" instead of havng it blank

Can someone tell me where should I place it to get the results i wanted?
Here are my codes.
[u]
[b]InvoiceFunctions.class[/b][/u]
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class InvoiceFunctions
Private strConnectionString As String
Private objDataAdapter As SqlDataAdapter
Private objInsertCommand As SqlCommand
Private objLoadCommand As SqlCommand
Private objUpdateCommand As SqlCommand
Private objValidCommand As SqlCommand
Private objDeleteCommand As SqlCommand

Public Sub New()

MyBase.New()

objDataAdapter = New SqlDataAdapter

strConnectionString = ConfigurationManager.ConnectionStrings("Echino").C onnectionString

End Sub
Public Function GetInvoice() As DataSet

Dim query As String = "select * from Invoice"
Dim cmd As New SqlCommand(query)

Return FillDataSet(cmd, "invoice")
End Function

Private Function FillDataSet(ByVal cmd As SqlCommand, ByVal tableName As String) As DataSet
Dim con As New SqlConnection(strConnectionString)
cmd.Connection = con

Dim adapter As New SqlDataAdapter(cmd)

Dim ds As New DataSet()
Try
con.Open()
adapter.Fill(ds, tableName)

Catch ex As Exception
Finally
con.Close()
End Try

Return ds

End Function

[u][b]AddJobOrder.aspx.vb[/b][/u]
Imports [Class]
Imports System.Configuration
Imports System.Data.SqlClient
Partial Public Class AddJobOrder
Inherits System.Web.UI.Page

Dim inv As New InvoiceFunctions

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then

ddl_Invoice.DataSource = inv.GetInvoice
ddl_Invoice.DataTextField = "invoiceNo"
ddl_Invoice.DataValueField = "invoiceNo"
ddl_Invoice.DataBind()

End if
End Sub

End Class
[/color]

Comments

  • seancampbellseancampbell Pennsylvania, USA
    Firstly, this is an ASP.Net questions, not a VB.Net question.
    In case you have any confusion in the future, VB.Net WindowsFormsApplications use the same name for many controls that are in different namespaces and have different functionality because they are made for the Web or made for a Application (for instance, Windows.Forms.TreeView and System.Web.UI.WebControls.TreeView). That said, you should try to post your ASP questions in ASP.Net forums, many people who submit solutions wouldn't catch that and may give you bad solutions or suggest things that you cannot do at all...

    Luckily, I spend the majority of my day writing ASP.Net apps, so heres my suggestion:
    [code]
    'This goes in the Aspx load code.
    ddl_Invoice.DataSource = inv.GetInvoice
    ddl_Invoice.DataTextField = "invoiceNo"
    ddl_Invoice.DataValueField = "invoiceNo"
    ddl_Invoice.DataBind()
    ddl_Invoice.Items.Insert(0, New ListItem("No Record Found", ""))
    [/code]

    I haven't actually tried this method, but it should insert a line with a blank value at the top of the list. When you supply ddl_Invoice.selectedValue with a blank string it will select No Record Found.

    Let me know if theres a problem
    -Sean Campbell
  • hi, thank you.

    The code works, however, my worry is if there is records inside the field. Wouldn't the default value still be 'No records found'?
  • seancampbellseancampbell Pennsylvania, USA
    I didn't understand your question entirely when you first asked it, add an if statement to that Insert line so it only inserts it when no items are in ddl_Invoice... try this instead:

    [code]
    If ddl_Invoice.Items.Count = 0 Then
    ddl_Invoice.Items.Insert(0, New ListItem("No Record Found", ""))
    End If
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories