Between sql2005 db, tableAdapters, BLL and Codehinds, How do display data related error messages?

Last post 04-18-2008 11:36 AM by akuyali. 6 replies.

Sort Posts:

  • Wink [;)] Between sql2005 db, tableAdapters, BLL and Codehinds, How do display data related error messages?

    04-11-2008, 11:40 AM
    • Loading...
    • akuyali
    • Joined on 08-23-2007, 10:18 AM
    • Posts 13

     For example, BLL Insert method, Do I have to put the message for the inserted record in the sproc or in the BLL or in the codehind of the asp pages?  I currenttly have a sproc which returns a booleam for the wether the record has been inserted or not, but my bll methods is only retruning a  true in every case, the user doen't see the error message from the codebehind which is set to fire when the returns a 0 and the insert operation has faled. 

    This is one of the BLL metheds..

    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, false)]
        public int InsertOwnership(int contactId, int staffDivId, int? themeId, int? statusId, bool inActive)
        {
            try
            {
            //Create a new ownershipRow instance
            MarketingContacts.OwnershipDataTable ownerships = new MarketingContacts.OwnershipDataTable();
            MarketingContacts.OwnershipRow ownership = ownerships.NewOwnershipRow();

          
            ownership.ContactId = contactId;
            ownership.StaffDivId = staffDivId;
            if (themeId == null) ownership.SetThemeIdNull();
            else ownership.ThemeId = themeId.Value;
            if (statusId == null) ownership.SetStatusIdNull();
            else ownership.StatusId = statusId.Value;
            ownership.InActive = inActive;

            //Add the new ownership
            ownerships.AddOwnershipRow(ownership);
               
             int  OwnershipExits = Adapter.Update(ownerships);

             return OwnershipExits;
            }
            catch (Exception ex)
             //catch (SqlException ex)
            {
                //throw new Exception("not string", ex);
                throw ex;
            }

     

    ...............And this the sproc fro the insert commanda:.................

     

    ALTER PROCEDURE [dbo].[spInsertOwnership]
        -- Add the parameters for the stored procedure here
                    @ContactId int,
                    @StaffDivId int,
                    @ThemeId int,
                    @StatusId int,
                    @InActive bit = 0
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    DECLARE @Result int --varchar(100)

    if exists (SELECT     ContactId, StaffDivId, InActive
        FROM         Ownership
        WHERE     (ContactId = @ContactId) AND (StaffDivId = @StaffDivId) AND (InActive = 0))

         Begin
            Set @Result = 0 --'duplicate found'
         End
    Else

    BEGIN
        -- Insert statements for procedure here
    INSERT INTO [dbo].[Ownership]

               ([ContactId],
               [StaffDivId],
               [ThemeId],
               [StatusId],
               [InActive])
             
    VALUES
                (@ContactId,
                 @StaffDivId,
                 @ThemeId,
                 @StatusId,
                 @InActive)
        set @Result = 0 --'new onership created'
    End
        SELECT @Result as Result

    END

     ........   this is my codebehind............ 

     

    protected void AsignContactToOwner_Click(object sender, EventArgs e)
        {
            if (StaffPlusDivision.SelectedIndex == 0)
            {
                LblErrorMessage.Text = "You need to select a staff name from the list below";
                LblErrorMessage.Visible = true;
            }
            else
                foreach (GridViewRow row in this.GridViewAllContacts.Rows)
                {
                    CheckBox checkBox = row.FindControl("TakeOwnership") as CheckBox;

                    if (checkBox.Checked)
                    {
                        Int32 temp = row.DataItemIndex;
                        Int32 pagenum = GridViewAllContacts.PageIndex;
                        Int32 pagesize = GridViewAllContacts.PageSize;

                        Int32 temporaldirective = temp - (pagenum * pagesize);

                        string contactId = GridViewAllContacts.DataKeys[temporaldirective].Value.ToString();
                    

                        string cn = row.Cells[3].Text;


                    

                        ObjectDataSource myOwnership = new ObjectDataSource();
                        OwnershipBLL ow = new OwnershipBLL();
                        int? themeId = null;
                        int? statusId = null;
                        try
                        {
                            int myResult = ow.InsertOwnership(int.Parse(contactId), StaffPlusDivision.SelectedIndex, themeId, statusId, false); 
                         
                            LblErrorMessage.Visible = false;

                            if (myResult == 0)
                            {
                                LblErrorMessage.Text = "You already have this contact on your list";
                                LblErrorMessage.Visible = true;
                            }
                            else
                            {
                             
                                ow.InsertOwnership(int.Parse(contactId), StaffPlusDivision.SelectedIndex, themeId, statusId, false);
                            }
                            ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('The contact(s) have been added to your List.');",true);
                            GridViewAllContacts.DataSourceID = "SqlDataSourceAllContact";
                            GridViewAllContacts.DataBind();
                       

                        }
                        catch (Exception ex)
                        {
                            LblErrorMessage.Text = ex.Message;
                            LblErrorMessage.Visible = true;
                            //LblErrorMessage.Visible = true;
                        }
                     
                     
                    }
                 
                }
             }
        

    Now, I know it will easire for me to use sqlDatasource and have a direct connection to to database, but I really want to understand how this could be done.  Any help at all will be greatly appreciated.

     thank you

    Akuyali 

     

  • Re: Between sql2005 db, tableAdapters, BLL and Codehinds, How do display data related error messages?

    04-11-2008, 11:43 AM
    Answer
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 7,603
    • Moderator
      TrustedFriends-MVPs

    Throw an error in your BLL and then catch it within the Selected, Updated, Deleted, or Inserted events of your ObjectDataSource. All of these event expose an Exception property which will contain your thrown error via directly or via the InnerException.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: Between sql2005 db, tableAdapters, BLL and Codehinds, How do display data related error messages?

    04-18-2008, 6:41 AM
    • Loading...
    • akuyali
    • Joined on 08-23-2007, 10:18 AM
    • Posts 13

    Hello ecbruck,

    thank you very much for your response.  I think this will work but I have not had the chance to try it yet as I hit another problem in my atempt to do so.  I have now put this as part of the BLL method,

     

     MarketingContacts.OwnershipRow OwnershipPerContact = ownerships[0];
            MarketingContacts.OwnershipDataTable existingContact =
                Adapter.GetOwnershipByContactIdAndStaffDivId(OwnershipPerContact.ContactId, OwnershipPerContact.StaffDivId);

            if (existingContact.Count > 0)
            {
                throw new ApplicationException("You already have this contact on you list");
            }

     

    In the hope of catching this on my insert property of the object data source. Now I get this:

    Error    1    Cannot implicitly convert type 'int?' to 'MarketingContacts.OwnershipDataTable'   

    Referring to the Bold and Underlined above.  I have had this previously but I didn't understand it so I abandon my approach and used sqlDatasource instead.  Any help will be greatly appreciated.

     

    Thank you

     

    Akuyali
     

     

  • Re: Between sql2005 db, tableAdapters, BLL and Codehinds, How do display data related error messages?

    04-18-2008, 8:21 AM
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 7,603
    • Moderator
      TrustedFriends-MVPs

    It looks to me that your "GetOwnershipByContactIdAndStaffDivId" method is trying to return an int. Is your method sent to "ExecuteScalar"?

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: Between sql2005 db, tableAdapters, BLL and Codehinds, How do display data related error messages?

    04-18-2008, 9:44 AM
    • Loading...
    • akuyali
    • Joined on 08-23-2007, 10:18 AM
    • Posts 13

     This is the code for the method in question:

     

    1    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
    2        public MarketingContacts.OwnershipDataTable GetOwnershipByContactIdAndStaffDivId(int ContactId,int StaffDivId)
    3        {
    4            return GetOwnershipByContactIdAndStaffDivId(ContactId,StaffDivId);
    5        }
    
     

    And the sql statement for the:

    SELECT     ContactId
    FROM         Ownership
    WHERE     (ContactId = @contactId) AND (StaffDivId = @StaffDivId)

    This method should simple return a one contactId column datatable. 

    I think the problem might be related to the nullable int? datatyp, but the parameters for the method arr not using the int? Morover, the dbuger only underlines the word 'Adapter' and nothing else.  I have gone back and looked at the example and notice that this particular line of code is only used in relation to the update methods

    1    MarketingContacts.OwnershipDataTable existingContact = Adapter.GetOwnershipByContactIdAndStaffDivId(contactId, staffDivId);
    I really con't understand why it wouldn't work for an insert method. Once again thank you for your response.  
  • Re: Between sql2005 db, tableAdapters, BLL and Codehinds, How do display data related error messages?

    04-18-2008, 9:51 AM
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 7,603
    • Moderator
      TrustedFriends-MVPs

    To me, this looks like the GetOwnershipByContactIdAndStaffDivId method is calling itself. Usually your return statment would look something like this>

    return adapter.GetOwnershipByContactIdAndStaffDivId(ContactId, StaffId)

    This would be the call to the actual TableAdapter while the GetOwnershipByContactIdAndStaffDivId method is your BLL implementation of that method. Somehow the application is getting confused on what you're calling.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: Between sql2005 db, tableAdapters, BLL and Codehinds, How do display data related error messages?

    04-18-2008, 11:36 AM
    • Loading...
    • akuyali
    • Joined on 08-23-2007, 10:18 AM
    • Posts 13

    Helle ecbruck,

    I think you are correct but I have run out of time, I will let you know how I get..

    thank you again

     

    AkuyaliWink 

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter