LCKB
[C#]ListBox show data - Printable Version

+- LCKB (https://lckb.dev/forum)
+-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109)
+--- Forum: Programmers Gateway (https://lckb.dev/forum/forumdisplay.php?fid=196)
+---- Forum: Coders Talk (https://lckb.dev/forum/forumdisplay.php?fid=192)
+---- Thread: [C#]ListBox show data (/showthread.php?tid=828)



- TheEvilAnt - 05-21-2012


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