By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,408 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,408 IT Pros & Developers. It's quick & easy.

Dropdown list is getting cleared after button_click event

P: n/a
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?


Nov 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.