Reputation: 363
How can I add an alternative item to dropdownlist when no items are found in mysql database.
Here's what I've tried
Sub getstudent()
Try
Dim val As String = System.Configuration.ConfigurationManager.ConnectionStrings("val").ConnectionString
Using mycon As New MySqlConnection(val)
Using ds As New DataTable
mycon.Open()
Dim SQL As String = "SELECT reg_no, name FROM tbl_students where class='" & Dpclass.SelectedItem.Text & "' and sub_class='" & Dpsub_class.SelectedItem.Text & "' and acad_session='" & acad_session.SelectedItem.Text & "'"
Dim myCommand As New MySqlCommand(SQL, mycon)
Dim myDA As New MySqlDataAdapter
myDA.SelectCommand = myCommand
myDA.Fill(ds)
mycon.Close()
Dpname.DataSource = ds
Dpname.DataTextField = "name"
Dpname.DataValueField = "reg_no"
Dpname.DataBind()
If Dpname.DataTextField < 1 Then
Dpname.Text = "Nothing Found"
End If
Dpname.Items.Insert(0, New ListItem("-select-", "0"))
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I'm getting an error: conversion from string "name" to type 'Double' is not valid.
Upvotes: 1
Views: 107
Reputation: 363
I finally got it, thanks to Mahdy Aslamy
If ds.rows.count = 0 then
Dpname.items.insert(0, New ListItem("No records Found", "0"))
Else
Dpname.items.insert(0, New ListItem("-Select-, "0""))
End If
Upvotes: 0
Reputation: 1228
I cannot be sure this is the answer since I do not know the structure of the table you are using but from what I see and the error given it appears you are adding 0 as a string and it is expecting a double. try the following:
Dpname.Items.Insert(0, New ListItem("-select-", 0))
Upvotes: 1
Reputation: 1985
check your DataTable is empty fill it with another value:
If ds.Rows.Count = 0 Then
Dpname.DataSource = new DataTable()
End If
Upvotes: 2