Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jacob Buus 6 posts 27 karma points
    May 01, 2010 @ 21:08
    Jacob Buus
    0

    Using LINQ-to-SQL with user controls

    I'm completely new to Umbraco - and I must say; I'm VERY impressed! :)

    However - I've been trying out the different features in the newest release of Umbraco and stumbled across a small problem. 
    I've created an .NET Web Application and added an user control to the solution. I want this user control to be able to retrieve products from my database using a ProductController-class - which the uses LINQ-to-SQL to retrieve the products. No problem so far - it works like a charm in Visual Studio. 

    The problem arises when I try to use my newly created user control within Umbraco. I can easily add my new user control but when I try to run the page I get an "Object reference not set to an instance of an object"-exception - logically enough seeing as how my user control can no longer retrieve my connectionstring etc. from the web.config file, and hence no data can be retrieved.  

    My question is - what's the best practice approach for using LINQ-to-SQL in my user controls for use with Umbraco?

    It would really save my day if somebody could give me an example of how I could get this working :)

  • Jesper Hauge 298 posts 487 karma points c-trib
    May 01, 2010 @ 21:24
    Jesper Hauge
    0

    There shouldn't be any problems retrieving anything from web.config from usercontrols. You can hand-edit the file and upload it to your site, as you please.

    Does your umbraco site use the same connection string (same database) as your development environment? If not - it is not enough to change the string in web.config since the linq-to-sql classes uses a connection string stored in a settings file, and doesn't read the string from the config file at run-time.

    I personally find this to be a pain in the a**, and even though there are work arounds, I for this reason prefer Subsonic, which will do what you expect in this regard.

     

    Regards
    Jesper Hauge

  • Jacob Buus 6 posts 27 karma points
    May 01, 2010 @ 23:08
    Jacob Buus
    0

    Hi Jesper; 

    Thank you very much for your reply - I would very much like to a small example of what's required to get my user control working using LINQ-to-SQL. Is this something you might be able to help me with? :)

    I have created a test-table in my Umbraco-DB called "Products". In my project I've created a LINQ-to-SQL Classes file which maps this table to a class called "Product". 
    I've then created a controller-class called "ProductsController" along with the user control. The user control then calls the ProductsController through an ObjectDataSource to retrieve all my products. 

    But which files should I upload to my Umbraco site (and where) in order for this to work? Right now I keep getting and NullReference-exception - so something is missing :S

    I hope you might be able to help me out :)

    Regards
    Jacob

  • Yannick Smits 321 posts 718 karma points
    May 02, 2010 @ 00:11
    Yannick Smits
    0

    have you uploaded your compiled dll from your linq-to-sql app to the umbraco bin folder?

  • Jacob Buus 6 posts 27 karma points
    May 02, 2010 @ 01:04
    Jacob Buus
    0

    Yes - I've uploaded the DLL to the bin-folder and the .ascx file to the usercontrols folder.

    I can find the user control through Umbraco and place it on a page - but when I run the page I get an error.  


  • Jesper Hauge 298 posts 487 karma points c-trib
    May 02, 2010 @ 01:05
    Jesper Hauge
    0

    Hi again Jacob

    Okay, assuming your .dbml file is placed in your web-application project, here's a short checklist I think should get you there:

    • Compile your web-application. This should produce a dll file in the bin folder below your web-application root. The name of the dll file is the same as the assmbly name as found on the Application tab when viewing the properties of the project.
    • Copy the dll file to the bin folder of your umbraco site.
    • Copy the ascx file (and only the ascx file) of the usercontrol(s) to the \usercontrol folder below the root of the umbraco website. (I prefer to have the usercontrols in a folder called "usercontrols" below the web-application too it makes things easier in respect to namespaces and such.)
    • Make sure the connection string for your linq-to-sql file is copied to the conectionstrings section in the web.config in the umbraco site, and make sure the connection string is using an account that actually has enough access rights to the umbraco database with the Products table.
    • Set up an umbraco macro for your usercontrol, or insert it via a masterpage to see it work.
    I think that should be enough.
    Regards
    Jesper Hauge

     

  • Jacob Buus 6 posts 27 karma points
    May 02, 2010 @ 01:17
    Jacob Buus
    0

    Hi Jesper; 

    Thank you very much:)
    I debugged my way through the code and found the problem - I've followed your steps - and I do not get an error anymore. On the other hand I don't see my user control either - but I'll just try resetting the page and doing it right from the start. 

    I'll keep you updated :)

    Regards
    Jacob

  • Jacob Buus 6 posts 27 karma points
    May 02, 2010 @ 01:33
    Jacob Buus
    0

    Okay; 

    Jesper - your steps got rid of the error....however....I've made a small simple user control - it's some static text saying "Here are your products" and a GridView displaying products in the database (using the ProductController-class through an ObjectDataSource). When I use the compiled user control through a demo website, it displays the data perfect - no problems at all. 
    But - when I use the user control through Umbraco, it does not display my GridView - only the static text(!!!)

    What could cause this? I don't get an error or anything - the data (and the gridview) just isn't displayed :(

    Best regards
    Jacob

  • Jacob Buus 6 posts 27 karma points
    May 02, 2010 @ 12:49
    Jacob Buus
    1

    I found a solution to my problem! :)

    It seems I needed an <form runat="server"> surrounding my user control in my Umbraco template - it works like a charm now! :)

    Thank you very much for your input though :)

    Regards
    Jacob

  • Thijs 97 posts 117 karma points
    Oct 26, 2011 @ 12:57
    Thijs
    0

    Hi Jacob,

    I'm having the same problem as you had. How did you modify the connectionstring so the whole thing worked? If you still remember :)

    Many thanks!

    Thijs

  • Thijs 97 posts 117 karma points
    Oct 26, 2011 @ 14:08
    Thijs
    0

    nvm, I found the problem. I'll post the code if other people are stuck with this.

    in the designer-file of your linq to sql file replace this line

    public DataSourceDataContext() : base(global::System.Configuration.ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString, mappingSource) { OnCreated(); }

    to

    public DataSourceDataContext() : base("Hardcode your connectionstring from the umbraco web.config", mappingSource) { OnCreated(); }

    This should solve the problem when having the object null reference.

    Thijs

Please Sign in or register to post replies

Write your reply to:

Draft