Retrieving value from combo box?
I have a databound combo box in the sub page_load that I'm trying to get the value from but keep getting this error.
Object reference not set to an instance of an object.
I have a button click event that I need to retrieve this value so that when its clicked a datagrid will be created based on the selected value. Any help is greatly appreciated.
Connection Code and data binding!
Public Sub Page_Load(sender as Object, e as EventArgs)
'1. Create a connection
Dim myConnection as SQLconnection
'Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
myConnection = New SQLconnection("server=xxxx; database=xxxx; uid=xxxx; pwd=xxxx")
'2. Create the command object, passing in the SQL string
Const strSQL as String = "SELECT DISTINCT Prov FROM OPPE_Clinic"
Dim myCommand as New SqlCommand(strSQL, myConnection)
'3. Create the DataReader
myConnection.Open()
Dim objDR as SqlDataReader
objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
'Databind the DataReader to the listbox Web control
practName.DataSource = objDR
practName.DataBind()
'Add a new listitem to the beginning of the listitemcollection
practName.Items.Insert(0, new ListItem("-- Select Provider --"))
End Sub
Button Click event I'm trying to retrieve the combo box value with.
Public Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim submit As Button
Dim pracText As TextBox
pracText.text = practName.SelectedItem.text
End Sub
<form name="form1" runat="server">
<table border=0 bgcolor="#F9F9F2" height=455 width=900>
<tr>
<td><asp:listbox id="practName" runat="server" Rows="1" DataTextField="Prov" AutoPostBack="True" /> <asp:textbox id="pracText" runat="server" Width="100" /> <asp:Button id="submit" runat="server" Text="Submit" OnClick="Submit_Click" />
</td>
</tr>
</table>
</form>