Copied to clipboard

Flag this post as spam?

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


  • hetaurhet 245 posts 267 karma points
    Jun 16, 2011 @ 08:00
    hetaurhet
    0

    accessing another DB from admin panel

    I am using umbraco 4.7.0. how can I insert records in another DB from umbraco's admin panel.

    say, I want that when user creates a new content page, I want that user can select books from the list related to this page. books names from another DB table. on save/publish of this content page I want to insert nodeid of this page, bookid of selected books into a new table say book_node_mapping. so is that possible?

    can I have detailed steps for the same? I am new to umbraco.

     

  • hetaurhet 245 posts 267 karma points
    Jun 16, 2011 @ 13:09
    hetaurhet
    0

    anybody there to help? its a bit urgent

  • Eran Meir 401 posts 543 karma points
    Jun 18, 2011 @ 13:23
    Eran Meir
    2

    you can create a custom datatype that will add a dropdown of your books, there's a nice video explaing how to create a datatype
    http://stream.umbraco.org/video/1533917/tim-geyssens-master-of 

    good luck :) 

  • hetaurhet 245 posts 267 karma points
    Jun 20, 2011 @ 07:12
    hetaurhet
    0

    ok will see. and revert back

  • jivan thapa 194 posts 681 karma points
    Jun 20, 2011 @ 09:39
    jivan thapa
    0

    if i was you:

    - create user control to fetch book name, book id .................

    - use usercontrol wrapper, it provides you to use usercontrol as datatype.

     

  • hetaurhet 245 posts 267 karma points
    Jun 20, 2011 @ 11:25
    hetaurhet
    0

    Eran Meir,

    the link of video u sent is playing upto 15 mins... not after that....

  • hetaurhet 245 posts 267 karma points
    Jun 20, 2011 @ 11:32
    hetaurhet
    0

    so can you people guide me further. suppose I use usercontrol wrapper, then depending on user selection of book and when the page is saved, can I update outside DB? I mean can I handle this update in usercontrol itself?

  • Bex 444 posts 555 karma points
    Jun 20, 2011 @ 14:46
    Bex
    0

    Hi!

    Here's what I would do...

    I'm not completely sure if I understand what you are trying to do so the assumptions I am making are:

    1. Your list of books come from another database
    2. When a document is saved you want the id of the document saved against this book in the other database
    First step I would create a user control or some custom data type that shows the books.
    This will reference a connection string in the webconfig (you can have more than one) to get the list of books.
    When the document is saved this will store the id or name or what ever you tell it to in the umbraco xml for that page.. 
    (assuming you know how to do this part)
    Also on the save you want it to save to the other database;
    to do this I would overrride the onsave or on publish of the document
    A list of all events you can override can be found here
    Here is an example of overriding an event, "before publish" in this case but it should give you the idea.
    Bare in mind this event will fire on every "before publish" of any document so you need to check the document type of the document it's firing on, then grab the id of the book by looking at the properties of the document and then do what ever you need with it.
    There maybe a way of doing it with a custom datatype but this is the easiest way I can think of!
    Does this help?
    Bex

     

  • hetaurhet 245 posts 267 karma points
    Jun 21, 2011 @ 07:05
    hetaurhet
    0

    you have understood correct.

    you wrote,

    "When the document is saved this will store the id or name or what ever you tell it to in the umbraco xml for that page.. 

    (assuming you know how to do this part)"
    But actually as I am new to umbraco, I dont know this. can you explain me. Also does umbraco automatically saves to umbraco XML?
  • Bex 444 posts 555 karma points
    Jun 21, 2011 @ 09:48
    Bex
    0

    Check out this video, think it should explain how to create a datatype from a user control. 
    Not done it for a while, but as far as I remember you just return a value, eitherway umbraco will save it.

    It's very simple.

    If you need something more complicated you can create a custom datatype with c# classes, ut  can't find a good example of that!

  • hetaurhet 245 posts 267 karma points
    Jun 23, 2011 @ 13:36
    hetaurhet
    0

    I got the concept of custom data type, tried it. Also saw events and tried it. I am able to modify DB.

    I still need guidance exactly for my requirement. here it is....

    Say I create custom datatype using user control wrapper (say List, which allows multiple selection.). Using that on one of the template. Now on before_publish event I want to access this list and want to insert rows in another DB for the selected values of list.

     

  • Bex 444 posts 555 karma points
    Jun 23, 2011 @ 13:42
    Bex
    0

    Can you give some code of how far you have got already and I may be able to point you in the right direction.
    You have said you have looked at events and you are able to modifiy the database, what is the exact bit you are struggling with?

     

  • hetaurhet 245 posts 267 karma points
    Jun 23, 2011 @ 16:33
    hetaurhet
    0

    yes... here is the code written in .cs file ...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.web;
    using UserDatatypes;

    namespace UmbracoEvents
    {
        public class AppBase : umbraco.BusinessLogic.ApplicationBase
        {

            public AppBase()
            {
                Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
            }

            void Document_BeforePublish(Document sender, PublishEventArgs e)
            {

                Log.Add(LogTypes.Debug, sender.Id, "the document " + sender.Text + " is about to be published");

                if (sender.ContentType.Alias == "BooksPage")
                {
                  
                    book b = new book();
                    b.insertBookNode(sender.getProperty("listOfBooks").Value.ToString());
                   
                   sender.Save();
                }


                //cancel the publishing
                //e.Cancel = true;
            }
        }
    }

    --  UserDatatypes namespace is having code of user control wrapper ... so .ascx.cs file is having following code....

    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 System.Data.SqlClient;
    using System.Data.Common;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.web;

    namespace UserDatatypes
    {
        public partial class BookList : System.Web.UI.UserControl,
            umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack || DropDownList1.SelectedValue.Length.ToString() == "0")
                {
                    fillBooks(DropDownList1);

                }
            }

            private void fillBooks(DropDownList DropDownList1)
            {
                book b = new book();
                string st = "select book_id, book_name from book_master";

                DataTable dt = b.fetchdata(st, null); // dynamically fill book list in dropdownlist
                if (dt != null)
                {
                    DropDownList1.Items.Clear();
                    DropDownList1.DataValueField = "BOOK_ID";
                    DropDownList1.DataTextField = "BOOK_NAME";
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataBind();
                    ListItem l1 = new ListItem();
                    l1.Text = "All";
                    l1.Value = "0";
                    DropDownList1.Items.Insert(0, l1);

                    if (!string.IsNullOrEmpty(_umbval))
                    {
                        DropDownList1.SelectedValue = _umbval;
                    }
               }
            }



            #region IUsercontrolDataEditor Members

            private string _umbval;
            public object value
            {
                get
                {  
                  
                    return DropDownList1.SelectedValue;

                    //throw new NotImplementedException();
                }
                set
                {
                    _umbval = value.ToString();
                    //throw new NotImplementedException();
                }
            }

            #endregion


        }

    }

    in this user control I have one dropdown having books names.... so on before publish I can access the selected value with b.insertBookNode(sender.getProperty("listOfBooks").Value.ToString()); in before_publish.

    .... now say instead of dropdown I want to access Listbox in which user can select multiple books, so how can I pass its value and update that many rows in another DB...? I am stuck here now.

     

  • hetaurhet 245 posts 267 karma points
    Jun 25, 2011 @ 06:33
    hetaurhet
    0

    hey anyone any idea ? how can I pass more than one value as list will have multiple selection.

  • hetaurhet 245 posts 267 karma points
    Jun 26, 2011 @ 07:04
    hetaurhet
    0

    if I am using multiple selection listbox I am getting following error.

     

    No mapping exists from object type System.Web.UI.WebControls.ListBox to a known managed provider native type.

    I am not getting how to get / set listbox object with multiple values.


Please Sign in or register to post replies

Write your reply to:

Draft