Click here to Skip to main content
15,884,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

I am trying to retrieve value in listbox using stored procedure. i m wform's listbox but it is not working. what i am doing wrong. can u please give the solution


C#
private void listWBox_SelectedIndexChanged(object sender, EventArgs e)
        {
             string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
             using (SqlConnection con = new SqlConnection(CS))
           

                try
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                     DataTable dt = new DataTable();

                    con.Open();
                    SqlCommand sqlcmd = new SqlCommand("readListWBox", con);
                    sqlcmd.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand = sqlcmd;
                    da.Fill(dt);
                    listJobBox.DataSource = dt;              
                    
                    
                }
                catch (Exception ex)                
                {

                    MessageBox.Show("exception raised");
                
                }

                finally
                {

                    con.Dispose();
                    con.Close();
                
                }

        }
Posted
Comments
j snooze 17-Oct-14 17:29pm    
I believe you are just missing the line
listJobBox.DataBind();
that goes after you set the datasource.
Mycroft Holmes 19-Oct-14 5:19am    
It would be useful to know what the error is, if the query returns data or is it the binding to the listview?

1 solution

You should set listJobBox.DisplayMember to the name of the column in the DataTable you want to display.

For example:
You have a column in the data table called 'FirstName';
Then
C#
listJobBox.DisplayMember = "FirstName";
 
Share this answer
 

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