displaying a value for dropdownlist if no records found

celinehgl

New member
Joined
Apr 5, 2010
Messages
3
Programming Experience
1-3
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.

InvoiceFunctions.class
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").ConnectionString

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

AddJobOrder.aspx.vb
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
 
Here's how I handle this situation:
VB.NET:
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()
        ddl_Invoice.Items.Insert(0, New ListItem("Select an invoice"), SomeDefaultValue, true))
    End if
End Sub
That way they're told to select and invoice but if that's the only item in the drop down then they very well can't select one.

Thread moved to Asp WebForms.
 
Here's how I handle this situation:
VB.NET:
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()
        ddl_Invoice.Items.Insert(0, New ListItem("Select an invoice"), SomeDefaultValue, true))
    End if
End Sub
That way they're told to select and invoice but if that's the only item in the drop down then they very well can't select one.

Thread moved to Asp WebForms.

Hi JuggaloBrotha,

I added Dim SomeDefaultValue = "No records found" and ddl_Invoice.Items.Insert(0, New ListItem("Select an invoice"), SomeDefaultValue, true)) in my codes.

I got an error which is overload resolution failed because no accessible 'Insert' accepts this number of arguments.
 
Hi JuggaloBrotha,

I added Dim SomeDefaultValue = "No records found" and ddl_Invoice.Items.Insert(0, New ListItem("Select an invoice"), SomeDefaultValue, true)) in my codes.

I got an error which is overload resolution failed because no accessible 'Insert' accepts this number of arguments.
Try this instead:
VB.NET:
ddl_Invoice.Items.Insert(0, New ListItem("Select an invoice"))
 

Latest posts

Back
Top