Search code examples
c#asp.netn-tier-architecture3-tier

Returning record inserted successfully in C# ASP.NET


In my DAL I have the following:

DB.ExecuteNonQuery(DBCommand);

then in my BLL I have the following:

DAL.data.insertticket(a);

and then in my presentation layer I have:

DAL.collection cobj = new collection();
BLL.business bobj = new business();
bobj.insertticket(cobj);

How do I check if the record was inserted in the database and then get my lbl to fade in and disappear after 2 seconds?

<asp:Label ID="lblUpdatedMessage" runat="server" 
           Text="Ticket Updated"></asp:Label>

I thought about doing the following in my presentation layer:

if (DAL.data.insertticket(a) == true) {
    lblUpdatedMessage.Visible = true;
}

But I get an error saying:

The name 'a' does not exist in the current context


Solution

  • ExecuteNonQuery returns an integer value representing the number of records modified/deleted/inserted by the executing query. It is zero if the insert fails, otherwise is greater than zero (depending on how many rows are affected by the call). All you need to do is returning that value up to the call chain until you could handle it in your presentation layer.

    And do not just jump from the presentation layer directly to the data layer. This invalidates totally the layer structure that you have built:

    DATALAYER

    int InsertObject(yourValidObjectInstance)
    {
        .... code to prepare the command to be executed ...
    
        // returns 0 if no record has been added (failure)
        int recordsAffected = DB.ExecuteNonQuery(DBCommand);
        return recordAffected;
    }
    

    BUSINESS LAYER

    ... code that checks if your object follows the business rules 
    ... and the validity of your object
    
    int records = DAL.InsertObject(yourValidObject);
    // True if you have inserted your records
    return (record > 0);
    

    PRESENTATION LAYER

    if(BusinessLayer.AddObject(yourValidObject))
       // execute your presentation code.....