I have no idea what I am doing but it is really driving me nuts.
I have tried as much as I know how but to no avail.
Here is what we are trying to do. Populate Gridview dropdownlist with values from the database.
This code below is doing that.
Protected Sub ddl1_load(ByVal sender As Object, ByVal e As EventArgs)
Dim conn As OleDbConnection
Dim connStr As String = ConfigurationManager.ConnectionStrings("allstringconstrng").ConnectionString
' Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("App_Data\Roster.mdb")
conn = New OleDbConnection(connStr)
' Dim adpt As New OleDbDataAdapter(s, conn)
'Open the connection
conn.Open()
Dim da As New OleDbDataAdapter("select login_id,username from tblusers", conn)
Dim dt As New DataTable()
da.Fill(dt)
Dim ddl As DropDownList = DirectCast(sender, DropDownList)
ddl.DataSource = dt
ddl.DataTextField = "username"
ddl.DataValueField = "login_id"
ddl.DataBind()
End Sub
The problem is that when trying to insert records into the database, it doesn't matter what option you select from the list, the first option is always selected and inserted into the database.
Any ideas how to fix this so that users can select any option they choose to and have that option inserted into the db.
I would also like to have a default value like
ddl.Items.Insert(0, "Select a user")
I have done this very easily without gridview. Obviously populating a dropdownlist from database is more difficult with gridview.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim conn As OleDbConnection
Dim connStr As String = ConfigurationManager.ConnectionStrings("allstringconstrng").ConnectionString
' Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("App_Data\Roster.mdb")
conn = New OleDbConnection(connStr)
' Dim adpt As New OleDbDataAdapter(s, conn)
'Open the connection
conn.Open()
Dim da As New OleDbDataAdapter("select login_id,username from tblusers", conn)
Dim dt As New DataTable()
da.Fill(dt)
Dim ddl As DropDownList = DirectCast(sender, DropDownList)
ddl.DataSource = dt
ddl.DataTextField = "username"
ddl.DataValueField = "login_id"
ddl.DataBind()
End If
End Sub
Programming to simplify, dont look for hard way
Suwandi - Non Graduate Programmer
simflex
Member
80 Points
277 Posts
Dropdownlist box always inserts the first option on the list, please help!
Jan 30, 2013 07:50 PM|LINK
I have no idea what I am doing but it is really driving me nuts.
I have tried as much as I know how but to no avail.
Here is what we are trying to do. Populate Gridview dropdownlist with values from the database.
This code below is doing that.
Protected Sub ddl1_load(ByVal sender As Object, ByVal e As EventArgs) Dim conn As OleDbConnection Dim connStr As String = ConfigurationManager.ConnectionStrings("allstringconstrng").ConnectionString ' Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("App_Data\Roster.mdb") conn = New OleDbConnection(connStr) ' Dim adpt As New OleDbDataAdapter(s, conn) 'Open the connection conn.Open() Dim da As New OleDbDataAdapter("select login_id,username from tblusers", conn) Dim dt As New DataTable() da.Fill(dt) Dim ddl As DropDownList = DirectCast(sender, DropDownList) ddl.DataSource = dt ddl.DataTextField = "username" ddl.DataValueField = "login_id" ddl.DataBind() End SubThis is the markup:
The problem is that when trying to insert records into the database, it doesn't matter what option you select from the list, the first option is always selected and inserted into the database.
Any ideas how to fix this so that users can select any option they choose to and have that option inserted into the db.
I would also like to have a default value like
I have done this very easily without gridview. Obviously populating a dropdownlist from database is more difficult with gridview.
Thanks as always.
dakvikas
Member
133 Points
102 Posts
Re: Dropdownlist box always inserts the first option on the list, please help!
Jan 30, 2013 08:28 PM|LINK
add autopostback=true property on dropdownlist and try
simflex
Member
80 Points
277 Posts
Re: Dropdownlist box always inserts the first option on the list, please help!
Jan 30, 2013 08:52 PM|LINK
Thanks for your response but that's not the problem. I tried that.
michaelalex7
Member
170 Points
35 Posts
Re: Dropdownlist box always inserts the first option on the list, please help!
Jan 30, 2013 09:21 PM|LINK
oned_gk
All-Star
35808 Points
7313 Posts
Re: Dropdownlist box always inserts the first option on the list, please help!
Jan 30, 2013 10:14 PM|LINK
Suwandi - Non Graduate Programmer
oned_gk
All-Star
35808 Points
7313 Posts
Re: Dropdownlist box always inserts the first option on the list, please help!
Jan 31, 2013 12:21 AM|LINK
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Dim conn As OleDbConnection Dim connStr As String = ConfigurationManager.ConnectionStrings("allstringconstrng").ConnectionString ' Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("App_Data\Roster.mdb") conn = New OleDbConnection(connStr) ' Dim adpt As New OleDbDataAdapter(s, conn) 'Open the connection conn.Open() Dim da As New OleDbDataAdapter("select login_id,username from tblusers", conn) Dim dt As New DataTable() da.Fill(dt) Dim ddl As DropDownList = DirectCast(sender, DropDownList) ddl.DataSource = dt ddl.DataTextField = "username" ddl.DataValueField = "login_id" ddl.DataBind() End If End SubSuwandi - Non Graduate Programmer
simflex
Member
80 Points
277 Posts
Re: Dropdownlist box always inserts the first option on the list, please help!
Jan 31, 2013 12:46 AM|LINK
This worked. Thanks a lot