【发布时间】:2019-05-16 17:22:30
【问题描述】:
当在 mysql 数据库中找不到任何项目时,如何向下拉列表添加替代项目。
这是我尝试过的
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
我收到一个错误:从字符串“name”到类型“Double”的转换无效。
【问题讨论】:
-
我不明白为什么人们坚持以这种方式使用 Try 块,你所做的只是掩盖更多可以用于调试的信息。至少使用
MsgBox(ex.ToString)如果DropDownList 类似于组合框(winforms),那么您应该设置文本字段、值字段,然后是数据源。如果要添加项目,则需要删除数据源或向当前数据表添加一行