以下代码有什么问题。我不知道如何将sqldatabase表内容添加到下拉列表中请帮助
Protected Sub DropDownList3_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList3.SelectedIndexChanged
Dim sqlcon As New SqlConnection("Data Source=SOf-22\SQLEXPRESS;Initial Catalog=Sales_oct_3;Persist Security Info=True;User ID=s;Password=121")
If sqlcon.State = ConnectionState.Open Then
sqlcon.Close()
End If
sqlcon.Open()
Dim strcommand As String
strcommand = "select item from tgnitem"
Dim sqlcomm As New SqlCommand(strcommand, sqlcon)
Dim o As String = sqlcomm.ExecuteNonQuery()
End Sub
End Class
答案 0 :(得分:0)
你在做很多错事。对于简单的事情,您应该更好地坚持使用强类型的DataSet。它们会给你带来很多负担。您不需要维护连接,手动执行查询,遍历结果并将其填充到UI控件等中。了解有关类型化DataSet和简单绑定方案(例如您的)的更多信息,它们将为您带来长期利益。
使用类型化DataSet时,这种东西的简单4步机制是:
DataSource
属性设置为此实例,将DataMember设置为DataTable的名称。将DisplayMember
和ValueMember
设置为DataTable的显示和值字段,例如DisplayMember =“Name”和ValueMember =“ItemID”。答案 1 :(得分:0)
看到这可能对你有帮助
Public Shared Function ClientList() As DataTable
Dim dtResult As DataTable = Nothing
Try
Dim objDataLayer As New ClsDataLayer()
objDataLayer.AddParameter("@REF_USER_ID", ClsCommons.IntUserId)
dtResult = objDataLayer.ExecuteDataTable("TR_PROC_GetClientList")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return dtResult
End Function
Public Shared Sub ClientList(ByVal cmbClientList As ComboBox)
cmbClientList.DropDownStyle = ComboBoxStyle.DropDownList
Try
Dim dtClientList As DataTable = ClsClientManager.ClientList()
If ((Not IsNothing(dtClientList)) AndAlso dtClientList.Rows.Count > 0) Then
Dim drClient As DataRow = dtClientList.NewRow()
drClient(0) = -1
drClient(1) = "< -- Select Department -- >"
dtClientList.Rows.InsertAt(drClient, 0)
cmbClientList.DataSource = dtClientList
cmbClientList.DisplayMember = "CLIENT_NAME"
cmbClientList.ValueMember = "CLIENT_ID"
Else
MessageBox.Show("There is no Department to load", "Talent Recruit", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub