T
Tim Zych
I'm having problems with a dropdownlist control.
I fill the control with values in the page_load event. But when I click the
button the dropdownlist control gets cleared out.
How do I use a value from the dropdownlist?
I've simplified what's going on. This example is supposed to add to a
textbox the value in the dropdownlist.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Page.IsPostBack Then Exit Sub
Dim strConn As String = ConfigurationSettings.AppSettings("DbLoc")
Dim cn As New OleDbConnection(strConn)
Dim cmd As New OleDbCommand("SELECT tblCats.Cat FROM tblCats;", cn)
cn.Open()
ddlCats.DataSource = cmd.ExecuteReader()
ddlCats.DataTextField = "Cat"
ddlCats.DataBind()
cn.Close()
End Sub
'When I click the button, the dropdown list is cleared first and then I get
an error during the btnSave_Click event.
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
txtCode.Text = ddlCats.SelectedItem.Text
End Sub
When I run it, I get an error "Object reference not set to an instance of an
object." because the dropdownlist has been cleared out first before the
actions in the click event can run.
Why is the dropdownlist getting cleared out every time and how do I fix it?
I fill the control with values in the page_load event. But when I click the
button the dropdownlist control gets cleared out.
How do I use a value from the dropdownlist?
I've simplified what's going on. This example is supposed to add to a
textbox the value in the dropdownlist.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Page.IsPostBack Then Exit Sub
Dim strConn As String = ConfigurationSettings.AppSettings("DbLoc")
Dim cn As New OleDbConnection(strConn)
Dim cmd As New OleDbCommand("SELECT tblCats.Cat FROM tblCats;", cn)
cn.Open()
ddlCats.DataSource = cmd.ExecuteReader()
ddlCats.DataTextField = "Cat"
ddlCats.DataBind()
cn.Close()
End Sub
'When I click the button, the dropdown list is cleared first and then I get
an error during the btnSave_Click event.
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
txtCode.Text = ddlCats.SelectedItem.Text
End Sub
When I run it, I get an error "Object reference not set to an instance of an
object." because the dropdownlist has been cleared out first before the
actions in the click event can run.
Why is the dropdownlist getting cleared out every time and how do I fix it?