Home All Groups Group Topic Archive Search About

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

Author
4 Jul 2009 11:10 AM
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

Author
4 Jul 2009 12:26 PM
Family Tree Mike
Tony Johansson wrote:
Show quoteHide quote
> 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();
>       }
>    }
>

> //Tony
>
>

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];


--------------------
Mike
Are all your drivers up to date? click for free checkup

Author
4 Jul 2009 2:02 PM
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

Show quoteHide quote
"Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com> skrev i meddelandet
news:eCKhZKK$JHA.2824@TK2MSFTNGP03.phx.gbl...
>
> Tony Johansson wrote:
>> 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();
>>       }
>>    }
>>
>
>> //Tony
>
> 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];
>
>
> --------------------
> Mike
Author
4 Jul 2009 7:56 PM
Family Tree Mike
Tony Johansson wrote:
Show quoteHide quote
> 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.

--
Mike
Author
4 Jul 2009 10:52 PM
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

Show quoteHide quote
"Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com> skrev i meddelandet
news:eEeVCGO$JHA.1248@TK2MSFTNGP04.phx.gbl...
>
> Tony Johansson wrote:
>> 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.
>
> --
> Mike
Author
4 Jul 2009 11:00 PM
Family Tree Mike
Tony Johansson wrote:
Show quoteHide quote
> 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" <FamilyTreeM***@ThisOldHouse.com> skrev i meddelandet
> news:eEeVCGO$JHA.1248@TK2MSFTNGP04.phx.gbl...
>> Tony Johansson wrote:
>>> 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.
>>
>> --
>> Mike
>
>

Yes, that is what I am saying.

--
Mike

Bookmark and Share