none
Something wrong with my datacontext?? RRS feed

  • Question

  • Hello everyone I have a datacontext called "dataclasses1datacontext" I am having a problem invoking some properties while running a query.

    I have a table called Juveniles and I am trying to retrieve all records in the table. 

     

    DataClasses1DataContext db = new DataClasses1DataContext(Launch);
          tblJuvenilesBindingSource.DataSource = db;
    
          var Juveniles = from j
                  in db.Juveniles
                  select j;
    

    The problem I am having is at the "in db.Juveniles" part For some reason intellisense does not recognize by table called Juveniles. It underlines it with a red squiggly line. I have read many tutorials online and I am still having multiple issues. I have correctly connected my project to an access database and have all the tables set up in my O/R designer. I have this datacontext but it will not seem to work. I also had a problem using the datacontext.submitchanges() method. It will not save and I am assuming it has to deal with this problem. 

     

    **PS, the parameter in the constructor called "Launch" Is my connection to the database

    • Moved by Aspen VJ Friday, June 10, 2011 7:27 AM (From:Visual C# Language)
    Tuesday, June 7, 2011 5:30 PM

Answers

  • Hi Vamp10988;

    You need to connect the binding source control to the results of the query and NOT the data context. See code snippet below.

    DataClasses1DataContext db = new DataClasses1DataContext(Launch);
    
    var Juveniles = from j in db.Juveniles
            select j;
    
    tblJuvenilesBindingSource.DataSource = Juveniles;
    

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, June 8, 2011 7:34 PM

All replies

  • http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

    A tutorial on the linq to sql.

    I am unsure what the parameter LAUNCH does. I, in general, just create a datacontext object, and use it as stated in the tutorial.  Please check if you can directly use something like:

    DataClasses1DataContext db = new DataClasses1DataContext();

    var result = from j in db.Juveniles select j;

    Tuesday, June 7, 2011 5:59 PM
  • Thats the tutorial I have been following. Along with a book called Microsoft Visual C# 2008 by Wrox Press. At first I tried creating a parameterless constructor such as yours but I got a complaint from the compiler stating

     

    "Error 2 Error 1 'JuvenileDatabase.DataClasses1DataContext' does not contain a constructor that takes 0 arguments C:\Users\msavage\Desktop\New folder (2)\JuvenileDatabase\JuvenileDatabase\JuvenileDataEntry.cs 38 42 JuvenileDatabase

    "
    I read somewhere online that I need to put the path to my database in the constructor when this happens. Thats why I have my "Launch" connection in there..
    Tuesday, June 7, 2011 6:18 PM
  • I think it has to do something with the fact that I am using ACCESS. I just checked my O/R designer and realized that there are no tables on the pane. When I try to drag tables from the server explorer it says "the selected object(s) use an unsupported data provider" If no tables are on there then there are no classes to represent the tables correct? I am so confused because in my .dbml O/R designer there is nothing on the pane but in my .xsd O/R designer I have all my tables and relationships mapped out
    Tuesday, June 7, 2011 7:36 PM
  • Yes. If you hit the datacontext object and put a dot beside it, its relevant tables should be poping up in the intellisense.

    I think the database connection issue might be because, you havent actually added a datasource. It is preferred that when you are utilizing LINQ to SQL(I am unsure if its LINQ in general), that you go ahead with the Wizard and give the necessary settings one of which probably deals with providing the database connection. Once you get to see the databases,you should be able to drag and drop the required tables out of it. And that should give you to access your tables by using the datacontext object.

    Tuesday, June 7, 2011 10:20 PM
  • My apology since I suggested you to use Linq, it's my fault that you're stuck here.

    Here's a discussion from where you might get some help:

    http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/1108cfe0-9e15-42c6-87dd-2da05e3d5e28/


    .:Courtesy of Illusion:.
    Wednesday, June 8, 2011 5:06 PM
  • Hi Vamp10988;

    You need to connect the binding source control to the results of the query and NOT the data context. See code snippet below.

    DataClasses1DataContext db = new DataClasses1DataContext(Launch);
    
    var Juveniles = from j in db.Juveniles
            select j;
    
    tblJuvenilesBindingSource.DataSource = Juveniles;
    

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, June 8, 2011 7:34 PM
  • Hi Vamp10988,

    Based on your description, I think  your issue is more related to Linq to Sql. So I will move this thread to Linq to Sql forum. Thanks for understanding.


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 10, 2011 7:27 AM