A very small complete program that doesn't work as expected

T

Tony Johansson

Hello!

I have a complete program listed below that use the northwind database table
Customers.
I have one control in the form and that is a DataGridView called
dataGridViewCustomer.
I use a SqlDataAdapter that pass the sql statement Select * from customers
in the c-tor.
When I run in the debugger I can see that the DataSet thisDataSet contains
all the rows that should be displayed
in the DataGridView but nothing is being displayed.

As you can see I set the DataSource for the DataGridView to the DataSet
thisDataSet.
I event to a refresh() on the DataGridView but nothing helps.

So does anyone know why my DataGridView doesn't display the contents of the
DataSet ?

//Here is the program
using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

SqlConnection thisConnection = new SqlConnection();
thisConnection.ConnectionString = "Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";
DataSet thisDataSet = new DataSet();
SqlDataAdapter custAdapter = new SqlDataAdapter
("Select * from customers", thisConnection);
custAdapter.Fill(thisDataSet, "Customers");
this.dataGridViewCustomer.DataSource = thisDataSet;
this.dataGridViewCustomer.Refresh();
}
}

partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.dataGridViewCustomer = new
System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewCustomer)).BeginInit();
this.SuspendLayout();
this.dataGridViewCustomer.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewCustomer.Location = new System.Drawing.Point(25,
24);
this.dataGridViewCustomer.Name = "dataGridViewCustomer";
this.dataGridViewCustomer.Size = new System.Drawing.Size(506, 342);
this.dataGridViewCustomer.TabIndex = 0;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(543, 408);
this.Controls.Add(this.dataGridViewCustomer);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridViewCustomer)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridViewCustomer;
}

//Tony
 
F

Family Tree Mike

Tony said:
Hello!

I have a complete program listed below that use the northwind database table
Customers.
I have one control in the form and that is a DataGridView called
dataGridViewCustomer.
I use a SqlDataAdapter that pass the sql statement Select * from customers
in the c-tor.
When I run in the debugger I can see that the DataSet thisDataSet contains
all the rows that should be displayed
in the DataGridView but nothing is being displayed.

As you can see I set the DataSource for the DataGridView to the DataSet
thisDataSet.
I event to a refresh() on the DataGridView but nothing helps.

So does anyone know why my DataGridView doesn't display the contents of the
DataSet ?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

SqlConnection thisConnection = new SqlConnection();
thisConnection.ConnectionString = "Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";
DataSet thisDataSet = new DataSet();
SqlDataAdapter custAdapter = new SqlDataAdapter
("Select * from customers", thisConnection);
custAdapter.Fill(thisDataSet, "Customers");
this.dataGridViewCustomer.DataSource = thisDataSet;
this.dataGridViewCustomer.Refresh();
}
}

I changed your code to the following after noticing the returned table
name was not "Customers", but was "Table" in the dataset.

DataSet thisDataSet = new DataSet();
SqlDataAdapter custAdapter =
new SqlDataAdapter("Select * from customers", thisConnection);
custAdapter.Fill(thisDataSet);
this.dataGridViewCustomer.DataSource = thisDataSet.Tables[0];
 
T

Tony Johansson

Hello!

you wrote
I changed your code to the following after noticing the returned table
name was not "Customers", but was "Table" in the dataset

The returned name of the DataTable in the DataSet thisDataSet is not Table
as you said but Customers if you do
string name = thisDataSet.Tables[0].ToString();
you get Customers.
This name Customers is taken from the second parameter in the
custAdapter.Fill(thisDataSet, "Customers");


//Tony

Family Tree Mike said:
Tony said:
Hello!

I have a complete program listed below that use the northwind database
table Customers.
I have one control in the form and that is a DataGridView called
dataGridViewCustomer.
I use a SqlDataAdapter that pass the sql statement Select * from
customers in the c-tor.
When I run in the debugger I can see that the DataSet thisDataSet
contains all the rows that should be displayed
in the DataGridView but nothing is being displayed.

As you can see I set the DataSource for the DataGridView to the DataSet
thisDataSet.
I event to a refresh() on the DataGridView but nothing helps.

So does anyone know why my DataGridView doesn't display the contents of
the DataSet ?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

SqlConnection thisConnection = new SqlConnection();
thisConnection.ConnectionString = "Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data
Source=hempc\\SQLExpress";
DataSet thisDataSet = new DataSet();
SqlDataAdapter custAdapter = new SqlDataAdapter
("Select * from customers", thisConnection);
custAdapter.Fill(thisDataSet, "Customers");
this.dataGridViewCustomer.DataSource = thisDataSet;
this.dataGridViewCustomer.Refresh();
}
}

I changed your code to the following after noticing the returned table
name was not "Customers", but was "Table" in the dataset.

DataSet thisDataSet = new DataSet();
SqlDataAdapter custAdapter =
new SqlDataAdapter("Select * from customers", thisConnection);
custAdapter.Fill(thisDataSet);
this.dataGridViewCustomer.DataSource = thisDataSet.Tables[0];
 
F

Family Tree Mike

Tony said:
Hello!

you wrote
I changed your code to the following after noticing the returned table
name was not "Customers", but was "Table" in the dataset

The returned name of the DataTable in the DataSet thisDataSet is not Table
as you said but Customers if you do
string name = thisDataSet.Tables[0].ToString();
you get Customers.
This name Customers is taken from the second parameter in the
custAdapter.Fill(thisDataSet, "Customers");


//Tony

What versions of Visual Studio and SQL Server are you using? I don't
see this behavior using VS 2008 Express with SQL Server Express 2008.
 
T

Tony Johansson

Hello!

Are you really sure about that. So you mean if you do string name =
thisDataSet.Tables[0].ToString();
You get table as answer.
It sound very strange if get another result then me.
My versions are VS 2005 and sql server Express 2005.

//Tony

Family Tree Mike said:
Tony said:
Hello!

you wrote
I changed your code to the following after noticing the returned table
name was not "Customers", but was "Table" in the dataset

The returned name of the DataTable in the DataSet thisDataSet is not
Table as you said but Customers if you do
string name = thisDataSet.Tables[0].ToString();
you get Customers.
This name Customers is taken from the second parameter in the
custAdapter.Fill(thisDataSet, "Customers");


//Tony

What versions of Visual Studio and SQL Server are you using? I don't see
this behavior using VS 2008 Express with SQL Server Express 2008.
 
F

Family Tree Mike

Tony said:
Hello!

Are you really sure about that. So you mean if you do string name =
thisDataSet.Tables[0].ToString();
You get table as answer.
It sound very strange if get another result then me.
My versions are VS 2005 and sql server Express 2005.

//Tony

Family Tree Mike said:
Tony said:
Hello!

you wrote
I changed your code to the following after noticing the returned table
name was not "Customers", but was "Table" in the dataset
The returned name of the DataTable in the DataSet thisDataSet is not
Table as you said but Customers if you do
string name = thisDataSet.Tables[0].ToString();
you get Customers.
This name Customers is taken from the second parameter in the
custAdapter.Fill(thisDataSet, "Customers");


//Tony
What versions of Visual Studio and SQL Server are you using? I don't see
this behavior using VS 2008 Express with SQL Server Express 2008.

Yes, that is what I am saying.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top