How do add a blank choice to dropdownlist

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2005
Posts: 33
Reputation: edmicman is an unknown quantity at this point 
Solved Threads: 0
edmicman's Avatar
edmicman edmicman is offline Offline
Light Poster

How do add a blank choice to dropdownlist

 
0
  #1
Feb 13th, 2006
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 33
Reputation: edmicman is an unknown quantity at this point 
Solved Threads: 0
edmicman's Avatar
edmicman edmicman is offline Offline
Light Poster

Re: How do add a blank choice to dropdownlist

 
0
  #2
Feb 14th, 2006
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC