0

Hi

I have previously populated a listbox / dropdown list with data from a stored procedure in ASP.NET C# and called the method in the PageLoad so that the list would be available once the page loaded up.

However, now I want to do the same thing except I want to do it for a Windows Application and not a website. So, how can I make the listbox automatically display the results of the sproc?

public void getCompanyName() //Method to return list of company names and add to listbox
        {
            //Clear the listbox
            listBox_CompanyName.Items.Clear();
            //Create SQL command, declare SPROC and connection to database
            myCommand = new SqlCommand("get_CompanyName", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;
            //Open database connection
            myConnection.Open();

            //Execute myCommand into the reader and close the connection
            myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            while (myReader.Read())
            {
                //Loop through the reader appending each element to the listbox
                listBox_CompanyName.Items.Add(myReader.GetString(1));
               
            }
            
            myReader.Close();
            myCommand.Dispose();
        }

Has anyone any ideas please? :)

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by dickersonka
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.