Hi!
I´m usually just writing webapplications and there is no problem setting for exampel value=ID from database and then show for exampel the produktname (text value) in the dropdownbox but now I´m writing a desktopapplikation and having problem figuring out how to do...!
If someone can help with listing some data that shows text from database and if You click that one a messagebox with the ID from database is shown.
After that I´m on the road again as the rookie I am on desktopapp...
Best // TT
Fernando SotoPosted Jun 11, 2008, 3:24 PM
Thomas TafvanderPosted Jun 11, 2008, 3:15 PM
Thank You!
Best // Thomas! (This helped me :-))
Fernando SotoPosted Jun 11, 2008, 2:54 PM
Hi Thomas;
Here is some sample code using the Northwind sample database showing how to display information from the database into the list box and when the user clicks on a item in the list box a message box will display the primary key of the item in the database.
Fernando
using
System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication44
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlDataAdapter da;
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
// Build the Connection String and create a SQL Connection object
String cnnStr = "Data Source = localhost; Initial Catalog = Northwind; Integrated Security = SSPI";
SqlConnection cnn = new SqlConnection(cnnStr);
// Create a SQL Command object from the connection object and setup the SELECT command
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Customers";
// Create the data adapter to use to get the data and handle connecting to the DB
da = new SqlDataAdapter(cmd);
// Get the data from the database. I am using the Northwind database the customers table
da.Fill(ds, "Customers");
// Disconnect the event handler to handle the listBox1_SelectedIndexChanged event
// while the list box is being populated, otherwise it will fire a couple of times
// before you are ready to handle them.
this.listBox1.SelectedIndexChanged -= new System.EventHandler(this.listBox1_SelectedIndexChanged);
// Connect the list box to the data source from where the data is comming from
// In this case the Customers data table in the dataset.
listBox1.DataSource = ds.Tables["Customers"];
// Tell the list box what collumn to display to the user
listBox1.DisplayMember = "CompanyName";
// Tell the list box what collumn to use with the displayed value, value is not displayed
listBox1.ValueMember = "CustomerID";
// Restored the event handler
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// When the user clicks on a item in the list box ist primary key will be displayed.
MessageBox.Show("Customer Primary Key is \"" + listBox1.SelectedValue + "\"");
}
}
}