943,998 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 9563
  • VB.NET RSS
Feb 13th, 2006
0

How do add a blank choice to dropdownlist

Expand Post »
I know this is probably a basic question, but I'm stuck and can't seem to figure it out

This is in VB.NET 2005 and I'm making a windows forms app. I'm filling a DataTable from a SQL command, and then binding a dropdownlist to that DataTable. I'm assigning the DisplayMember and ValueMember just fine, and the dropdownlist is working fine. But what I want to do is after binding it, add a "blank" choice (or some other text like "please choose something" to the first slot, and default to that being selected when the app runs. I know it's simple, I'm just blanking out here. Thanks!

Here's the code for what I'm doing:

VB.NET Syntax (Toggle Plain Text)
  1. Private Sub BindAccounts()
  2. Dim conn As New SqlConnection()
  3. Dim myCommand As New SqlCommand
  4. Dim da As New SqlDataAdapter
  5. Dim myData As New DataTable
  6.  
  7. Dim SQL As String
  8. Dim sSQL As String
  9.  
  10. SQL = "server=" & My.Settings.DataSource & ";" _
  11. & "user id=" & Login.txtUserName.Text & ";" _
  12. & "password=" & Login.txtPassword.Text & ";" _
  13. & "database=" & My.Settings.InitialCatalog
  14.  
  15. conn.ConnectionString = SQL
  16. sSQL = "select distinct lngAccountID_pk, strAccountName"
  17. sSQL = sSQL & " from tblAccounts"
  18. sSQL = sSQL & " where lngAccountID_pk in (select accountid from tblstaging)"
  19. sSQL = sSQL & " order by strAccountName"
  20.  
  21. Try
  22. conn.Open()
  23. Try
  24.  
  25. myCommand.Connection = conn
  26. myCommand.CommandText = sSQL
  27. myCommand.CommandType = CommandType.Text
  28.  
  29. da.SelectCommand = myCommand
  30. da.Fill(myData)
  31.  
  32. ddlAccount.DataSource = myData
  33. ddlAccount.DisplayMember = "strAccountName"
  34. ddlAccount.ValueMember = "lngAccountID_pk"
  35.  
  36. Catch myerror As SqlException
  37. MsgBox("There was an error reading from the database: "
  38. & myerror.Message)
  39.  
  40. End Try
  41.  
  42. ddlAccount.SelectedIndex = 0
  43.  
  44. conn.Close()
  45.  
  46. Catch myerror As SqlException
  47. MessageBox.Show("Error Connecting to Database: " &
  48. myerror.Message)
  49. conn.Dispose()
  50. End Try
  51. End Sub
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
edmicman is offline Offline
33 posts
since Oct 2005
Feb 14th, 2006
0

Re: How do add a blank choice to dropdownlist

Derrrr, I found some old C# code I used in another project, I'm hoping to try it tonight after work. I think this might work?

// Add blank option
ddlDepartment.Items.Insert(0, new ListItem("", ""));

I think my problem was in the ListItem part - I'd forgotten about that.

On a related note (or maybe not), when I bind the account list it is firing the selection changed event and then trying to bind the datagrid (when I load the form), and is crapping out on that. Basically it's trying to do the SQL query before there is data for it, I think. Is there anything like a PostBack in forms programming? I don't want it to bind the datagrid on load....ah well, I'll figure it out!
Reputation Points: 10
Solved Threads: 0
Light Poster
edmicman is offline Offline
33 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: how to create stock chart using vb.net?
Next Thread in VB.NET Forum Timeline: Does AutoCad 14 support COM object?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC