none
LINQ to SQL-pub Windows Application in Visual C# 2008 Express: No error and no output of 3 columns in dataGridView!! Why? RRS feed

  • Question

  • Hi all,
    In my Visual C# 2008 Express, I created a Windows Forms application "LINQ-Lee439" with the following code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Diagnostics;
    
    namespace LINQ_Lee439
    //namespace LINQtoDataSet (Lee's Book: Page 439)
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                SqlConnection conn;
                SqlCommand comm;
                SqlDataAdapter adapter;
                DataSet ds = new DataSet();
    
            //---loads the Authors table into dataset---
            conn = new SqlConnection(@"Data Source=NAB-WK-02554356\SQLSCOTT;"  +
                  "Initial Catalog=pubs;Integrated Security=True");
            comm = new SqlCommand("SELECT * FROM Authors", conn);
            adapter = new SqlDataAdapter(comm);
            adapter.Fill(ds);
            
        //---Using LINQ to DataSet---
        //---query for authors living in CA---
            var authors =
                (from author in ds.Tables[0].AsEnumerable()
                 where author.Field<string>("State") == "CA"
                 select new
                 {
                     ID = author.Field<string>("au_id"),
                     FirstName = author.Field<string>("au_fname"),
                     LastName = author.Field<string>("au_lname")
                 }).ToList();
    
            //---bind to a datagridview control---
            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.DataSource = authors;
       
         }
        }
    }
    

    From the ToolBox, I dragged a DataGridView to the surface of Form1. It did not have any compilation errors. I executed this project. It ran and the Form1 appeared, but the dataGridView1 was empty!!!  I do not know what is wrong with this project.  Please help and advise.

    Thanks,

    Scott Chang    

    Tuesday, October 20, 2009 2:49 PM

All replies

  • Hi Scott,

    Have you got the column name showed on datagridview?
    If the three columns' name shows, that means the datasource has no rows.
    Please check the dataset and linq statement carefully.

    If the columns' name don't show, you can try to reset the VS setting.
    Click Tools in the main menu -> Import and Export Settings -> Reset All Settings -> Select the C# -> Finish

    You can also try to set breakpoint to get whether it is cuased by datasource.
    http://msdn.microsoft.com/en-us/library/k80ex6de.aspx

    If it is not caused by datasource, you can post in the following forums for better support,
    C# IDE
    http://social.msdn.microsoft.com/Forums/en-US/csharpide/threads
    C# Express Edition
    http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/threads

    Best Regards
    Yichun Feng





    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, October 23, 2009 10:00 AM
  • Hi Yichun Feng,  Thanks for your nice response.

    The 3 columns' names do not show up in the output of the project result.  I reset the VS setting as you instructed-it did not work either.  I am completely lost in this matter. I need to ask you the following things:
    (1) There are several kinds of setting breakpoints "address breakpoint", function breakpoint", "breakpoint on a function call from the Call Stack Window", etc.  Which breakpoint setting am I doing for this project?
    (2) I added an entity class "PubsAuthorNames" (see the attached revised code) to the project and it did not work either.  What do I miss in this project so far?
    (3) In this project, the DataSet is from the "pubs" databse in SQL Server 2008 Express. The Console application for this project worked. Why can I not get the titles and output of the 3 columns in the DataGridView of the Windows application?   
    (4)  I had a diffrent project (Linq to SQL-Northwind Object Relational Mapping/Design)  that had an independent DataSet made manually by me.  The entity class worked nicely in this case.  Why can I not get this "Linq to Sql pubs Windows application" working for me?

    Please help and advise/answer me again.

    Thanks again,
    Scott Chang

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Linq;
    using System.Data;
    //using System.Data.Linq;
    //using System.Data.Linq.Mapping;
    
    namespace LINQ_Lee439
    //namespace LINQtoDataSet (Lee's Book: Page 439)
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
           // [Table(Name = "PubsAuthorNames")]
            public class PubsAuthorNames
            {
                public string au_id { get; set; }
                public string au_fname { get; set; }
                public string au_lname { get; set; }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                SqlConnection conn;
                SqlCommand comm;
                SqlDataAdapter adapter;
                DataSet ds = new DataSet();
    
            //---loads the Authors table into dataset---
            conn = new SqlConnection(@"Data Source=NAB-WK-02554356\SQLSCOTT;"  +
                  "Initial Catalog=pubs;Integrated Security=True");
            comm = new SqlCommand("SELECT * FROM Authors", conn);
            adapter = new SqlDataAdapter(comm);
            adapter.Fill(ds);
            
        //---Using LINQ to DataSet---
        //---query for authors living in CA---
            var authors =
                (from author in ds.Tables[0].AsEnumerable()
                 where author.Field<string>("State") == "CA"
                 select new
                 {
                     ID = author.Field<string>("au_id"),
                     FirstName = author.Field<string>("au_fname"),
                     LastName = author.Field<string>("au_lname")
                 }).ToList();
    
            //---bind to a datagridview control---
            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.DataSource = authors;
       
         }
        }
    }
    

    Monday, October 26, 2009 1:17 PM
  • Hi Scott,

     

    For your first question, you can simply click on the left edge on this line,

    adapter.Fill(ds);

     

    You will see a breakpoint has been set. Then press F5 for debugging. Press P10 and move the mouse on dataset check the rowcount of dataset.

    Or you can simply do like this,

     

            adapter.Fill(ds);

            MessageBox.Show(ds.Tables[0].Rows.Count.ToString());

     

            var authors =

                (from author in ds.Tables[0].AsEnumerable()

                 where author.Field<string>("State") == "CA"

                 select new

                 {

                     ID = author.Field<string>("au_id"),

                     FirstName = author.Field<string>("au_fname"),

                     LastName = author.Field<string>("au_lname")

                 }).ToList();

     

            MessageBox.Show(authors.Count.ToString());

         

      

    For your second question, if the entity class is defined by you own, I do not think that it will work unless you’ve defined mapping table for it. However, I suggest that you use designer to add entity.

     

     

    For your third and forth question, you may first make sure that the datasource of datagridview is not null first. Could you make the datagridview work in other project?

    If the datasource is not null, you can post in other forum for IDE issue, because it probably not caused by your code.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, October 27, 2009 2:19 AM
  • Hi Yichun Feng,  Thanks for your nice response.

    1) I added the code statement "MessageBox.Show(...)" to the place as you indicated in your last post and executed the project. I still got the empty DataGridView (i. e. no message from MessageBox.Show).

    2) Yesterday, I posted my "LINQ to SQL-pubs Windows application in Visual C# 2008 Express:......." in the C# IDE Forum as you suggested.  This morning, I could not find my post in that Forum. I do not know what happened to my post there!!!???

    I am still interested in finding the solution/answer to the LINQ to SQL-pubs Windows Forms application that has the DataSet was derived from the SQL Server 2008 Express- some thing is strange and puzzling to the new LINQ users.
    If you can be more helpful, please let me know your valuable insight on LINQ to SQL-pubs (with SQL Server 2008 Express).

    Thanks,
    Scott Chang 
    Tuesday, October 27, 2009 12:43 PM
  • I have struggled to make this project working for me.  I found out that to make use of LINQ to DataSet, I need to have a reference to System.Data.DataSetExtensions.dll in my project.  I added that reference to my project and I got the following error: The type or namespace name 'dll' does not exist in the namespce 'System.Data.DataSetExtensions'(are you missing an assembly?).  Now I am wondering how I can get an assembly for this 'dll'.  Please help and advise.

    Thanks,
    Scott Chang

    P. S.  The following is my updated code for this project:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Linq;
    using System.Data;
    using System.Data.DataSetExtensions.dll;  
    //using AuthorDisplay.DataSetLee439TableAdapters;
    //using System.Data.Linq;
    //using System.Data.Linq.Mapping;
    
    namespace LINQ_Lee439
    //namespace LINQtoDataSet (Lee's Book: Page 439)
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
           // [Table(Name = "PubsAuthorNames")]
            public class PubsAuthorNames
            {
                public string au_id { get; set; }
                public string au_fname { get; set; }
                public string au_lname { get; set; }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                SqlConnection conn;
                SqlCommand comm;
                SqlDataAdapter adapter;
                DataSet ds = new DataSet();
    
            //---loads the Authors table into dataset---
            conn = new SqlConnection(@"Data Source=NAB-WK-02554356\SQLSCOTT;"  +
                  "Initial Catalog=pubs;Integrated Security=True");
            comm = new SqlCommand("SELECT * FROM Authors", conn);
            adapter = new SqlDataAdapter(comm);
            adapter.Fill(ds);
             MessageBox.Show(ds.Tables[0].Rows.Count.ToString());  //per Yichun Feng
            
        //---Using LINQ to DataSet---
        //---query for authors living in CA---
            var authors =
                (from author in ds.Tables[0].AsEnumerable()
                 where author.Field<string>("State") == "CA"
                 select new
                 {
                     ID = author.Field<string>("au_id"),
                     FirstName = author.Field<string>("au_fname"),
                     LastName = author.Field<string>("au_lname")
                 }).ToList();
    
            MessageBox.Show(authors.Count.ToString()); //per Yichun Feng
    
            //---bind to a datagridview control---
            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.DataSource = authors;
       
         }
    
            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                SqlConnection conn;
                SqlCommand comm;
                SqlDataAdapter adapter;
                DataSet ds = new DataSet();
    
                //---loads the Authors table into dataset---
                conn = new SqlConnection(@"Data Source=NAB-WK-02554356\SQLSCOTT;" +
                      "Initial Catalog=pubs;Integrated Security=True");
                comm = new SqlCommand("SELECT * FROM Authors", conn);
                adapter = new SqlDataAdapter(comm);
                adapter.Fill(ds);
                MessageBox.Show(ds.Tables[0].Rows.Count.ToString());  //per Yichun Feng
            
                //---Using LINQ to DataSet---
                //---query for authors living in CA---
                var authors =
                    (from author in ds.Tables[0].AsEnumerable()
                     where author.Field<string>("State") == "CA"
                     select new
                     {
                         ID = author.Field<string>("au_id"),
                         FirstName = author.Field<string>("au_fname"),
                         LastName = author.Field<string>("au_lname")
                     }).ToList();
    
                MessageBox.Show(authors.Count.ToString()); //per Yichun Feng
    
                //---bind to a datagridview control---
                this.dataGridView1.AutoGenerateColumns = true;
                this.dataGridView1.DataSource = authors;
       
            }
    
            private void authorsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
                this.Validate();
                this.authorsBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(this.dataSetLee439);
    
            }
    
            private void Form1_Load_1(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'dataSetLee439.authors' table. You can move, or remove it, as needed.
                this.authorsTableAdapter.Fill(this.dataSetLee439.authors);
    
            }
        }
    }
    

    Notes:  I added DataSetLeed439.xsd and pubs.mdf to the Solution Explorer of this project too.

     

    Wednesday, November 18, 2009 2:17 PM
  • Hi Yichun,  Good New to tell you!!

    I created a new project "LINQ-Lee440-pubsWinFrm" and brougt a DataGridView from the ToolBox to the Form1 surface. The code for this new project is:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Diagnostics;
    //using System.Data;
    //using System.Data.DataSetExtensions.dll;  
    
    namespace LINQ_Lee440_pubsWinFrm
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                SqlConnection conn;
                SqlCommand comm;
                SqlDataAdapter adapter;
                DataSet ds = new DataSet();
    
                //---loads the Authors table into dataset---
                conn = new SqlConnection(@"Data Source=NAB-WK-02554356\SQLSCOTT;" +
                      "Initial Catalog=pubs;Integrated Security=True");
                comm = new SqlCommand("SELECT * FROM Authors", conn);
                adapter = new SqlDataAdapter(comm);
                adapter.Fill(ds);
                MessageBox.Show(ds.Tables[0].Rows.Count.ToString());  //per Yichun Feng
    
                //---Using LINQ to DataSet---
                //---query for authors living in CA---
                var authors =
                    (from author in ds.Tables[0].AsEnumerable()
                     where author.Field<string>("State") == "CA"
                     select new
                     {
                         Author_ID = author.Field<string>("au_id"),
                         First_Name = author.Field<string>("au_fname"),
                         Last_Name = author.Field<string>("au_lname"),
                         State = author.Field<string>("state"),
                         Phone = author.Field<string>("phone")
                     }).ToList();
    
                MessageBox.Show(authors.Count.ToString()); //per Yichun Feng
    
                //---bind to a datagridview control---
                this.dataGridView1.AutoGenerateColumns = true;
                this.dataGridView1.DataSource = authors;
       
            }
    
    
        }
    }
    

    It worked nicely and I got the expected output in the DataGridView. I am so glad to see this result!!!!

    One question to ask you: I do not need to use "using System.Data.DataSetExtensions.dll" in this project. Why not? Please explain it if you can.

    Thanks,

    Scott Chang

     

     

    Thursday, November 19, 2009 1:14 PM