Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Everyone,

I have two list boxes - lstBusinesses and lstBusinessDetails. When the business name in lstBusinesses is selected I want to run a query to the database that will select all the associated information from this business name which is done by passing a parameter with the value of the selected item in lstBusinesses and display the result information in lstBusinessDetails.

Here is my code but when I debug it, it seems to be skipping the reader part where i write information to the lstBusinessDetails.

This is fired when the OnSelectedIndexChanged event is initiated in lstBusinesses

protected void ShowBusinessDetails(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
String connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

conn = new SqlConnection(connectionString);
conn.Open();
comm = new SqlCommand("SELECT Address_Line_1, Address_Line_2, Address_Line_3, Provence, County, Telephone from Business WHERE Business_Name=@BusinessName", conn);
comm.Parameters.Add("@BusinessName", System.Data.SqlDbType.VarChar).Value = lstBusinesses.SelectedIndex.ToString();
SqlDataReader reader = comm.ExecuteReader();
while(reader.Read())
{
String AddressLine1 = reader.GetString(3);
lstBusinesses.Items.Add(AddressLine1);
String AddressLine2 = reader.GetString(4);
lstBusinesses.Items.Add(AddressLine2);
String AddressLine3 = reader.GetString(5);
lstBusinesses.Items.Add(AddressLine3);
String County = reader.GetString(6);
lstBusinesses.Items.Add(County);
String Provence = reader.GetString(7);
lstBusinesses.Items.Add(Provence);
String Owner = reader.GetString(8);
lstBusinesses.Items.Add(Owner);
String Telephone = reader.GetString(10);
lstBusinesses.Items.Add(AddressLine1);


}

}

Also is it possible in the one query to select information also from another table? I have a table User_Acc which has an email address for the business that I would also like to display in lstBusinessDetails...
Posted
Updated 24-Feb-14 12:57pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Feb-14 20:17pm    
When you say "ListBox", the question is: which one? Full type name, please.
—SA
Member 10609511 24-Feb-14 20:27pm    
Not quite sure what you mean??

<asp:TableRow ID="TableRow3" runat="server">
<asp:TableCell ID="TableCell5" runat="server" HorizontalAlign="Center" VerticalAlign="Top">Business :
<asp:TableCell ID="TableCell6" runat="server" HorizontalAlign="Left" VerticalAlign="Top"><asp:ListBox ID="lstBusinesses" runat="server" Width="245px" OnSelectedIndexChanged="ShowBusinessDetails" AutoPostBack="true">


<asp:TableRow ID="TableRow4" runat="server">
<asp:TableCell ID="TableCell12" runat="server" HorizontalAlign="Center" VerticalAlign="Top">Business Details :
<asp:TableCell ID="TableCell7" runat="server" HorizontalAlign="Left" VerticalAlign="Top"><asp:ListBox ID="lstBusinessDetails" runat="server" Width="256px">

Sergey Alexandrovich Kryukov 24-Feb-14 20:35pm    
Tell us the type of ListBox. Do you know what the "type" is?!!! You program with types...
I actually can see now what it is, but you should never say just "ListBox". There is a number of unrelated types under this name.
—SA
Member 10609511 24-Feb-14 20:39pm    
I just dragged and dropped the listbox item from the toolbox in visual studio and the above code was generated from that
Sergey Alexandrovich Kryukov 24-Feb-14 20:46pm    
I have bad feeling that you don't know the type of ListBox. Is this is really so, I would advise you to stop doing what you are doing and learn the very basics of programming and the language, as well as platform. Otherwise your programming will lead you nowhere, will give you nothing but a lot of frustrations. You cannot skip the fundamentals...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900