Dropdown list is getting cleared after button_click event

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top