[C#]ListBox show data
#1

As I see a uer have problem and he dont have a clear idea with wizas answer i will explain this is the code to show it i will explain with comments:

 

 

// Variables

SqlConnection connection = new SqlConnection();

SqlDataAdapter adapter = new SqlDataAdapter(); ;

SqlCommand SQLcmd = new SqlCommand();

DataTable table = new DataTable();

 

// To connect db fill your data if you want to share the pogram change to a app.config

connection.ConnectionString = "server=server;uid=user;pwd=password;";

 

// Properties

SQLcmd.Connection = connection;

SQLcmd.CommandType = CommandType.Text;

// SQL

SQLcmd.CommandText = "select * from [DB].[dbo].table";

 

// Execute query

adapter.SelectCommand = SQLcmd;

 

// Filling data for variable table

try

{

// Fill with query

adapter.Fill(table);

}

catch (SqlException ex)

{

// Show error

MessageBox.Show(ex.Message);

}

catch (Exception ex)

{

// Show error

MessageBox.Show(ex.Message);

}

 

listBox1.DisplayMember = "Name of column to show";

listBox1.DataSource = table;

 

 

Of course dont forget:

using System.Data.SqlClient;

 

 

if you think it is interesting just click like



Forum Jump:


Users browsing this thread: 1 Guest(s)