pisdxbamxk
New Member
I built a listbox from a datbase using the databind feature. I learned how to do this from the 4guysfromrolla.com article:<BR>http://www.4guysfromrolla.com/webtech/071101-1.shtml<BR><BR>The code works great! <BR><BR>However, he did not get a chance to write and article teaching how to access the items yet. I have tried accessing it by writing a click routine:<BR>If lstVendors.SelectedIndex > - 1 Then<BR> Label1.Text = "You chose: " & lstVendors.SelectedItem.Text<BR> End If<BR><BR>That did not work. I think its because its acutally binded so you have to access the index diffrently.<BR><BR>Does anyone know how to access items out of a binded listbox???<BR><BR>Here is the working code i developed. It will display the listbox fine. I just can NOT pull a value from it when i click the button.<BR><BR>ANY HELP WOULD BE VERY MUCH APPRECIATED!!!<BR><BR>The datbase is simple:<BR>tblvendors<BR>VendorID<BR>VendorName<BR><BR><% @Import Namespace="System.Data" %><BR><% @Import Namespace="System.Data.OleDb" %><BR><BR><script language="VB" runat="server"><BR><BR>Sub Page_Load(sender as Object, e as EventArgs)<BR> 'Create a connection<BR> Const sConnStr as String="Provider=Microsoft.Jet.OLEDB.4.0;" & _<BR> "Data Source=C:OrderOrders.mdb"<BR> Dim objConn as New OleDbConnection(sConnStr)<BR> <BR> 'You must open the connection before populating the DataReader<BR> objConn.Open()<BR><BR><BR> 'Create a command object for the query<BR> Const strSQL as String = "SELECT VendorID, VendorName " & _<BR> "FROM tblVendors"<BR> Dim objCmd as New OleDbCommand(strSQL, objConn)<BR><BR> 'Create/Populate the DataReader<BR> Dim objDR as OleDbDataReader<BR> objDR = objCmd.ExecuteReader()<BR> <BR> 'TODO: Databind the DataReader to the listbox Web control<BR> <BR> lstVendors.DataSource = objDR<BR> lstVendors.DataBind()<BR><BR> lstVendors.Items.Insert(0, new ListItem("-- Choose a Vendor --"))<BR>End Sub<BR><BR> <BR> Sub SubmitBtn_Click(sender As Object, e As EventArgs)<BR> If lstVendors.SelectedIndex > - 1 Then<BR> Label1.Text = "You chose: " & lstVendors.SelectedItem.Text<BR> End If<BR> End Sub 'SubmitBtn_Click<BR><BR></script><BR><BR><html><BR><body><BR><form runat="server"><BR> <BR> <b>Vendors</b>:<BR> <asp:listbox id="lstVendors" runat="server" Rows="1" <BR> DataTextField="VendorName" DataValueField="VendorID" SelectionMode="Single" /><BR> <BR> <asp:button id="Button1"<BR> Text="Submit" <BR> OnClick="SubmitBtn_Click" <BR> runat="server" /> <BR> <BR><asp:Label id="Label1" <BR> Font-Name="Verdana" <BR> Font-Size="10pt" <BR> runat="server"/><BR> <BR> <BR></form><BR><BR></body><BR></html>Sub SubmitBtn_Click(sender As Object, e As EventArgs)<BR>If lstVendors.SelectedIndex > - 1 Then<BR>Label1.Text = "You chose: " & lstVendors.items(lstVendors.SelectedIndex).Value<BR>End If<BR>End Sub 'SubmitBtn_Click<BR>I think you are right in that will show the selected index. HOwever, when i used your revised submitbtn_click i cannot get a valid selectedindex.<BR><BR>Otherwords the I lstVendors.SelectedIndex > -1 is false every time.<BR><BR>Do you know why my index is not greater than -1 ?<BR><BR>Thanks for the help !!!I figured out my problem!<BR><BR>I had to put the the data reader code inside of <BR><BR>If Not IsPostBack<BR><BR>Thanks for all the help!<BR><BR>Below is the completed working code in case it will help anyone else! <BR>__________________________________________________ ______<BR><BR><BR><% @Import Namespace="System.Data" %><BR><% @Import Namespace="System.Data.OleDb" %><BR><BR><script language="VB" runat="server"><BR><BR>Sub Page_Load(sender as Object, e as EventArgs)<BR>If Not IsPostBack<BR> <BR><BR><BR> 'Create a connection<BR> Const sConnStr as String="Provider=Microsoft.Jet.OLEDB.4.0;" & _<BR> "Data Source=C:OrderOrders.mdb"<BR> Dim objConn as New OleDbConnection(sConnStr)<BR> 'You must open the connection before populating the DataReader<BR> objConn.Open()<BR> 'Create a command object for the query<BR> Const strSQL as String = "SELECT VendorID, VendorName " & _<BR> "FROM tblVendors"<BR> Dim objCmd as New OleDbCommand(strSQL, objConn)<BR> 'Create/Populate the DataReader<BR> Dim objDR as OleDbDataReader<BR> objDR = objCmd.ExecuteReader()<BR> 'TODO: Databind the DataReader to the listbox Web control<BR> lstVendors.DataSource = objDR<BR> lstVendors.DataBind()<BR> lstVendors.Items.Insert(0, new ListItem("-- Choose a Vendor --"))<BR> <BR> End If <BR>End Sub<BR><BR> <BR> Sub SubmitBtn_Click(sender As Object, e As EventArgs) <BR>If lstVendors.SelectedIndex > - 1 Then <BR>Label1.Text = "You chose: " & lstVendors.items(lstVendors.SelectedIndex).Value <BR>End If <BR><BR><BR>End Sub 'SubmitBtn_Click <BR><BR><BR></script><BR><BR><html><BR><body><BR><form runat="server"><BR> <BR> <b>Vendors</b>:<BR> <asp:listbox id="lstVendors" runat="server" Rows="1" DataTextField="VendorName" DataValueField="VendorID" SelectionMode="Single"> </asp:ListBox><BR> <BR> <asp:button id="Button1"<BR> Text="Submit" <BR> OnClick="SubmitBtn_Click" <BR> runat="server" /> <BR> <BR><asp:Label id="Label1" <BR> Font-Name="Verdana" <BR> Font-Size="10pt" <BR> runat="server"/><BR> <BR> <BR></form><BR><BR></body><BR></html><BR><BR>