locked
Doing LINQ to DataSet in Visual C# 2008 Express: Problems in SqlConnection/SqlCommand/SqlDataAdapter and LINQ namespace

    Question

  • Hi all,

    My first LINQ programming project (LINQ-Lee439)  in Visual C# 2008 Express and SQL Server 2008 Express has 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;

    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=.\SQLEXPRESS;" +

     

    "Initial Catalog=pubs;Integrated Security=True");

    comm =

    new SqlCommand("SELECT * FROM Authors", conn);

    adapter =

    new SqlDataAdapter(comm);

    adapter.Fill(ds);

     

    foreach (DataRow row in ds.Tables[0].Rows)

    {

     

    Console.WriteLine("(0) - (1) (2)",

    row[

    "au_id"], row["au_lname]");

    }

     

    //---Using LINQ to DataSet---

     

    //---query for authors living in CA---

    var authors =

    from authors indexer Tables[0].AsEnumerable()

    where author.Field<

    string>("State") == "CA"

    select author;

    }

    ------------------------------------------------------
    I got the following 25 errors:
    Error 1 Invalid token '=' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 28 14 LINQ-Lee439
    Error 2 Method must have a return type C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 28 20 LINQ-Lee439
    Error 3 Type expected C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 28 34 LINQ-Lee439
    Error 4 Invalid token '=' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 30 14 LINQ-Lee439
    Error 5 Method must have a return type C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 30 20 LINQ-Lee439
    Error 6 Type expected C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 30 31 LINQ-Lee439
    Error 7 Invalid token ')' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 30 60 LINQ-Lee439
    Error 8 Invalid token '=' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 31 17 LINQ-Lee439
    Error 9 Method must have a return type C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 31 23 LINQ-Lee439
    Error 10 Identifier expected C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 31 42 LINQ-Lee439
    Error 11 Invalid token '(' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 32 21 LINQ-Lee439
    Error 12 Invalid token ')' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 32 24 LINQ-Lee439
    Error 13 ; expected C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 33 30 LINQ-Lee439
    Error 14 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 33 43 LINQ-Lee439
    Error 15 Invalid token '.' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 33 45 LINQ-Lee439
    Error 16 Invalid token ')' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 33 50 LINQ-Lee439
    Error 17 Invalid token '(' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 35 29 LINQ-Lee439
    Error 18 Invalid token '[' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 36 18 LINQ-Lee439
    Error 19 Invalid token '"au_id"' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 36 19 LINQ-Lee439
    Error 20 Identifier expected C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 36 19 LINQ-Lee439
    Error 21 Invalid token '[' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 36 32 LINQ-Lee439
    Error 22 Invalid token '"au_lname]"' in class, struct, or interface member declaration C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 36 33 LINQ-Lee439
    Error 23 Identifier expected C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 36 33 LINQ-Lee439
    Error 24 A namespace does not directly contain members such as fields or methods C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 40 5 LINQ-Lee439
    Error 25 Identifier expected C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 41 37 LINQ-Lee439
    -------------------------------------------------------------------
    I have no ideas why I got so many errors and how to resolve these problems related to SqlConnection, SqlCommand, SqlDataAdatpter, and LINQ things.  Please help and advise.

    Thanks,
    Scott Chang

    Tuesday, July 14, 2009 12:48 PM

Answers

All replies

  • You have  

    private void Form1_Load(object sender, EventArgs e);

    it should be 

    private void Form1_Load(object sender, EventArgs e) {


    replace the semicolon with a curly brace and you should be good to go


    If this answers your question, please mark the question as answered.
    Tuesday, July 14, 2009 2:45 PM
  • Hi BigTuna99,   Thanks for your valuable response.

    I corrected the mistake you pointed out and few other mistakes. I still got 5 errors in the LINQ part of coding:
       

    //---Using LINQ to DataSet---

     

    //---query for authors living in CA---

     

    var authors =

     

    from authors indexer Tables[0].AsEnumerable()

     

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

     

    select author;

    --------------------------------------------------------------
    Error 1 The name 'Tables' does not exist in the current context C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 42 30 LINQ-Lee439
    Error 2 The type or namespace name 'authors' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 42 14 LINQ-Lee439
    Error 3 The name 'author' does not exist in the current context C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 43 15 LINQ-Lee439
    Error 4 The name 'author' does not exist in the current context C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 44 16 LINQ-Lee439
    Error 5 Syntax error, 'in' expected C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs 42 30 LINQ-Lee439
    ==============================================

    This is my first LINQ project and I am not able to resolve these errors.  Please help me again and tell me how to correct the LINQ-related problems.

    Thanks,
    Scott Chang  
    Tuesday, July 14, 2009 3:49 PM
  • I added a "using System.Diagnostics;" to the top of my file and had the following corrected code statements in my project:
      

     

    //---Using LINQ to DataSet---

     

    //---query for authors living in CA---

     

    var authors =

     

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

     

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

     

    select author;

    ------------------------------------------
    I executed the project with the revised code and I got only 2 new errors:

    Error    1          The name 'author' does not exist in the current context   C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs         44        15        LINQ-Lee439

    Error    2          A local variable named 'authors' cannot be declared in this scope because it would give a different meaning to 'authors', which is already used in a 'child' scope to denote something else      C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\LINQ-Lee439\LINQ-Lee439\Form1.cs         42        9          LINQ-Lee439

    --------------------------------------------

    Please help and advise.

    Thanks,
    Scott Chang 
    Tuesday, July 14, 2009 7:55 PM
  • Should be:

    var authors =
    from author in ds.Tables[0].AsEnumerable()
    where author.Field<string>("State") == "CA"
    select author;

    Note it's "author" not "authors".  "authors" is what you're assigning to, not the value you want to reference internally in the LINQ query to represent a single author.


    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Marked as answer by Figo Fei Thursday, July 16, 2009 7:51 AM
    Tuesday, July 14, 2009 7:58 PM
  • Hi David, Thank you very much for your nice, valuable response.

    I corrected the mistake as you instructed.  The LINQ part of this project worked nicely. It is time for me to refresh my skills of using Windows Form (label, textbox, etc.) to present the LINQ results out.

    Thanks again,
    Scott Chang 
    Tuesday, July 14, 2009 8:13 PM
  • Hi David,  Thanks for your response.

    I corrected the mistake and executed the project code.  I just got the blank Form1 appeared without errors, but no output result showed up and no Console screen showed up.   I am completely lost now. I copied this project from a book. I do not understand the following thing:   The author of the book started project with Windows Application, why does the code statement 'Console.WriteLine ("{0} - {1} {2}", row["au_id"], row , row["au_fname"], row["au_lname");' appear in

    foreach

     

    (DataRow row in ds.Tables[0].Rows)

    {

     

    Console.WriteLine("{0} - {1} {2}",

    row[

    "au_id"], row["au_lname]"]);

    }

    ?

    Please help and tell me why I do not get the output and how to make it work.

    Thanks,
    Scott Chang

    Tuesday, July 21, 2009 2:57 PM
  • There are a few types of assemblies you can create with C#.  Among those, are CUI (Console User Interface) and GUI (Graphical User Interface) applications.  A Windows Form's application is a GUI application, while calls to the Console class are typically only used in CUI applications.  In order to view console output, you have to have an active console for your application, but in your situation, since you're writing a GUI application, having a console open (the console is the black box with text that some applications use) would be overkill. 

    That being said, from what you're working on, you might do better to follow the following steps:

    1. Create a new application, but make it a Console Application.
    2. Copy the code within the Form_Load event handler to the interior of the Main method that is generated for you. 
    3. Make sure you have the proper namespace declarations at the top of the page. 
    4. Rebuild and run. 

    The main issue here is that you're trying to write a console application in a windows application, which isn't the right way to go if you're wanting console output only.


    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Tuesday, July 21, 2009 3:07 PM