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 27, 2011 @ 07:17
    hetaurhet
    0

    get and set values from checkbox list

    I am using customized user control wrapper. this has one dropdownbox with values populated from database.

    here is the code of .ascx.cs file ....

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;

    namespace umbracotest
    {
        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";

                DataSet ds = b.fetchdata(st, null); // dynamically fill book type in dropdownlist
                DataTable dt = ds.Tables[0];
               
                if (dt != null)
                {
                    DropDownList1.Items.Clear();
                    DropDownList1.DataValueField = "BOOK_ID";
                    DropDownList1.DataTextField = "BOOK_NAME";
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataBind();
                  

                    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
        }
    }

    now on before publish event I am inserting this dropdown box value to another DB. this work fine....

    now I want to replace dropdownbox with checkbox list, so that user can select multiple values and so I can insert multiple records in DB on before publish event.... so how to get and set values for this in above code....

    currently my before publish event code is as follows

    namespace umbracotest
    {
        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")
                {
                    //BookList bl = new BookList();
                    book b = new book();

                    b.insertBookNode(sender.getProperty("listOfBooks").Value.ToString());
                   
                    //sender.ExpireDate = DateTime.Now.AddDays(14);
                    sender.Save();
                }


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

    can anybody help.. I am using umbraco 4.7 and new to this.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jun 27, 2011 @ 07:29
    Dirk De Grave
    0

    Don't know why you're writing the values for the datatype to your own tables (you must have a reason for that), but if you really want to save the values into your own custom tables on before publish, then you'd need to do almost the same as your code above, only difference is that the value returned from

    sender.getProperty("listOfBooks").Value.ToString()

    will most probably be a comma separated list of values. So, split up that value using .Split(), iterate the values and perform a b.insertBookNode() for each value.

     

    Hope this helps.

    Regards,

    /Dirk

     

  • hetaurhet 245 posts 267 karma points
    Jun 29, 2011 @ 10:53
    hetaurhet
    0

    I have naother question now. I am updating the listbox values in DB on before publish. But it then gives error of unique key constraints. so want to know that when before publish event actually fires... as on save and then publish also give DB UK constraint error and directly publish button also give this error.

  • hetaurhet 245 posts 267 karma points
    Jun 29, 2011 @ 16:39
    hetaurhet
    0

    on before publish or after publish... I get the same error................ when i update DB through for loop in which I am passing selected values of checkboxlist

    Server Error in '/' Application.


    Violation of UNIQUE KEY constraint 'IX_BOOK_NODE_MAPPING'. Cannot insert duplicate key in object 'dbo.BOOK_NODE_MAPPING'.
    The statement has been terminated.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.SqlClient.SqlException: Violation of UNIQUE KEY constraint 'IX_BOOK_NODE_MAPPING'. Cannot insert duplicate key in object 'dbo.BOOK_NODE_MAPPING'.
    The statement has been terminated.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    Stack Trace:

    [SqlException (0x80131904): Violation of UNIQUE KEY constraint 'IX_BOOK_NODE_MAPPING'. Cannot insert duplicate key in object 'dbo.BOOK_NODE_MAPPING'.
    The statement has been terminated.]
    System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2030802
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009584
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
    System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
    System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +228
    System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +326
    System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
    book.insertBookNode(String bookid) +259
    UmbracoEvents.AppBase.Document_AfterPublish(Document sender, PublishEventArgs e) +434
    umbraco.cms.businesslogic.web.PublishEventHandler.Invoke(Document sender, PublishEventArgs e) +0
    umbraco.cms.businesslogic.web.Document.FireAfterPublish(PublishEventArgs e) +27
    umbraco.cms.businesslogic.web.Document.PublishWithResult(User u) +1519
    umbraco.cms.presentation.editContent.Publish(Object sender, EventArgs e) +236
    System.EventHandler.Invoke(Object sender, EventArgs e) +0
    umbraco.controls.ContentControl.savePublish(Object Sender, ImageClickEventArgs e) +46
    System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +115
    System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +120
    System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

Please Sign in or register to post replies

Write your reply to:

Draft