Copied to clipboard

Flag this post as spam?

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


  • Fredrik Esseen 594 posts 830 karma points
    Oct 19, 2009 @ 10:48
    Fredrik Esseen
    0

    Using existing DB connection

    Ive created a table in the db where I want to store information when members is logged in so that I can display that in the back.

    How do I get the db connection from my Usercontrol? Ive tried to use the Mysql.Data as reference but then it complains about wrong reference..

  • Rich Green 2246 posts 4006 karma points
    Oct 19, 2009 @ 11:03
    Rich Green
    0

    You can use the connection string from the Web.Config file.

    Include a a reference to:

    using System.Configuration;

    Then something like this

    MyConnection.ConnectionString = ConfigurationManager.AppSettings["umbracoDbDSN"].ToString();

    Hope this helps.

    Rich

  • Dirk De Grave 4537 posts 6006 karma points MVP 3x admin c-trib
    Oct 19, 2009 @ 11:09
    Dirk De Grave
    0

    Hi,

    You could also use the

    umbraco.BusinessLogic.Application.SqlHelper

    object (only to use if you're using the same umbraco database to store the info about member visits)

     

    Cheers,

    /Dirk

  • Fredrik Esseen 594 posts 830 karma points
    Oct 19, 2009 @ 12:04
    Fredrik Esseen
    0

    Perfect!

    But I get an error: Keyword datalayer not supported.

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Linq;

    using

     

    System.Web;

    using

     

    System.Web.UI;

    using

     

    System.Web.UI.WebControls;

    using

     

    System.Data;

    using

     

    umbraco.cms.businesslogic.member;

    using

     

    umbraco.cms.businesslogic.propertytype;

    using

     

    System.Configuration;

    using

     

    MySql.Data;

    namespace

     

    NormalLogin

    {

     

    public partial class NormalLogin : System.Web.UI.UserControl

    {

     

    protected void Page_Load(object sender, EventArgs e)

    {

     

    }

     

    public void OnLoggedIn(object sender, EventArgs e)

    {

     

    Member theMember = Member.GetCurrentMember();

     

     

    string constring = ConfigurationManager.AppSettings["umbracoDbDSN"].ToString();

     

    MySql.Data.MySqlClient.

    MySqlConnection mysqlcon = new MySql.Data.MySqlClient.MySqlConnection(constring);

     

    string sqlstr = "INSERT INTO MemberLog (MemberID) VALUES ('" + theMember.Id + "', '" + DateTime.Now + "')";

    MySql.Data.MySqlClient.

    MySqlDataAdapter mysqladapt = new MySql.Data.MySqlClient.MySqlDataAdapter(sqlstr, mysqlcon);

    mysqlcon.Close();

    }

    }

    }

  • Rich Green 2246 posts 4006 karma points
    Oct 19, 2009 @ 12:48
    Rich Green
    0

    This may help debugging

    When testing usercontrols I test them outside Umbraco (unless using the API) by copying the database connection from the Umbraco site, creating a standard .NET page and then include the usercontrol within that, I can then debug that user control within Visual Studio as usual.

    Essentially it's just a standard web app as you're not using Umbraco functionality in your above code.

    Set that up and you should see your error pretty quickly?

     

  • Fredrik Esseen 594 posts 830 karma points
    Oct 19, 2009 @ 12:57
    Fredrik Esseen
    0

    I think the problem is that the connectionstring includes datalayer. Can that be it? And how do I work around it? Do I have to add a new key myself?

  • Rich Green 2246 posts 4006 karma points
    Oct 19, 2009 @ 13:08
    Rich Green
    0

    Try removing the "datalayer" part of your connection string.

  • Dirk De Grave 4537 posts 6006 karma points MVP 3x admin c-trib
    Oct 19, 2009 @ 13:26
    Dirk De Grave
    0

    Or use the SqlHelper, it's designed to not worry about the datalayer part of the connection string!

     

    Cheers,

    /Dirk

  • Fredrik Esseen 594 posts 830 karma points
    Oct 19, 2009 @ 14:04
    Fredrik Esseen
    0

    And how do I use the SqlHelper..?

  • Dirk De Grave 4537 posts 6006 karma points MVP 3x admin c-trib
    Oct 19, 2009 @ 14:16
    Dirk De Grave
    0

    froad,

    Have a look at the msdn docs on http://umbraco.org/apiDocs/index.html

    ISqlHelper can be found in the umbraco.DataLayer namespace.

     

    Cheers,

    /Dirk

  • Fredrik Esseen 594 posts 830 karma points
    Oct 19, 2009 @ 14:29
    Fredrik Esseen
    0

    Made it work..Thanks!

    Next problem...how do I get the current member? I tried following:

    Member

     

    theMember = Member.GetCurrentMember();

    But that didnt fetch anything..

  • Dirk De Grave 4537 posts 6006 karma points MVP 3x admin c-trib
    Oct 19, 2009 @ 14:43
    Dirk De Grave
    0

    froad,

    Are you sure you're logged on, GetMember will only return a member object if logged on! And btw, you should be using the membership provider api over the existing member api. Details may be found on the wiki pages.

     

    Cheers,

    /Dirk

  • Fredrik Esseen 594 posts 830 karma points
    Oct 19, 2009 @ 14:45
    Fredrik Esseen
    0

    Sorry to overwhelm you with questions before diggin in myself...

    Solved it this way:

     

    Member theMember = Member.GetMemberFromLoginName(HttpContext.Current.User.Identity.Name);

    Thank you for your time and advices!

  • Fredrik Esseen 594 posts 830 karma points
    Oct 19, 2009 @ 15:05
    Fredrik Esseen
    0

    Returning because it didnt work..

    //.aspx:

    <

     

    asp:Login FailureText="Felaktigt anvndarnamn eller lsenord" OnLoggingIn="OnLoggedIn" ID="Login1" runat="server">

    public void OnLoggedIn(object sender, EventArgs e)
    {
    Member theMember = Member.GetMemberFromLoginName(HttpContext.Current.User.Identity.Name);
    string sqlstr = "INSERT INTO MemberLog (MemberID, LoggedInDate) VALUES
    ('"
    + theMember.LoginName + "', '" + DateTime.Now + "')";
    umbraco.BusinessLogic.
    Application.SqlHelper.ExecuteNonQuery(sqlstr);
    }

    As you can see im trying to add the member to the log when he has logged in but the Identity.Name is empty. Why is that? I know im logged in because the page is only visible to logged in members!

  • Fredrik Esseen 594 posts 830 karma points
    Oct 19, 2009 @ 15:16
    Fredrik Esseen
    0

    Solved it in ´the not best way:

    Login1.UserName

     

    Why go for the cache when I had the answer in the login-control all the time :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies