Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All


I am working on a software in which i need to display the search query results in a listbox. mine is a medical software and im trying to display the list of medicines in the listbox. im using c# for coding under Windows Visual studio 10 platform. please help me wid this.

Im using Sql server management studio as backend. im using a button which when clicked will fire an event performing a query. the query is select * from Table_name i want to show the query list in the listbox..




regards

haldin
Posted
Comments
Kenneth Haugland 11-Aug-12 6:28am    
You could use Linq for this :)

1 solution

C#
private void button1_Click(object sender, EventArgs e)
{
     SqlConnection connection = new SqlConnection();

     connection.ConnectionString = "Server=.;Integrated Security=true;...";

     SqlCommand command = new SqlCommand();

     command.Connection = connection;
     command.CommandText = "Select * From Table_Name";
     command.CommandType = CommandType.Text;

     try
     {
         connection.Open();

         SqlDataReader reader = command.ExecuteReader();

         while(reader.Read())
         {
             string title = (string)reader["Title"];
             string description = (string)reader["description"];

             string item = string.Format("{0} - {1}", title, description);

             this.listBox1.Items.Add(item);
         }

         reader.Close();
     }
     catch
     {
     }
     finally
     {
         if (connection.State == ConnectionState.Open)
             connection.Close();
     }
}
 
Share this answer
 
Comments
Abdul Quader Mamun 11-Aug-12 7:05am    
good work!

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