Hi,
I have a database with a few tables in, one being "Customers"
I don't know how to fill the datagrid with the Customers table contents
I need to be able to display that table in a datagrid view on the click of a button on my customers search form
I will eventually be able to search by name, post code, city and ALL which is what i need to do first
I think I have tp use data adapters and data sets but I'm not exactly sure how to use them with SQL to get the results I want
I am using SQL Server 2005, Visual Studio 2008 and C#
Any help would be appreciated
Thanks
Loading
DanielPosted Mar 8, 2011, 5:04 AM
{
SqlConnection sqlNWConn = new SqlConnection();
SqlDataAdapter daCustomers;
DataSet dsCustomers;
String tableName = "Customers";
sqlNWConn = new SqlConnection(@"Data Source=DANS-MAC--WIN7\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");
if (radioAll.Checked == true)
{
try
{
sqlNWConn.Open();
dsCustomers = new DataSet(); // Creates a new DataSet
daCustomers = new SqlDataAdapter("select * from Customers", sqlNWConn); // Creates a new SqlDataAdapter with select command and connection
SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daCustomers); // Fills in insert, update, and delete commands
daCustomers.Fill(dsCustomers, tableName); // Fills the dataset
dataGridCustomers.DataSource = dsCustomers;
dataGridCustomers.DataMember = tableName;
}
catch
{
MessageBox.Show("there was an error and your code is crap"); // error will be changed when working
}
}
if (radioName.Checked == true)
{
// Searches value in txtSearch in table Customer ContactName
}
}
Sam HobbsPosted Mar 7, 2011, 8:08 PM