Search code examples
sql-servervisual-studio3-tier

Displaying data from the database on a textbox in the presentation layer. Three tier architecture


Create procedure sp_getTotal   
     @InvoiceID varchar(50)
As
Begin
    (Select Invoice.Total
     from Invoice
     where Invoice.InvoiceID = @InvoiceID)
End

I have created the above procedure to get the "total" from Invoice table in my database on SQL Server. I need to make this value be displayed on a textbox in the presentation layer on button click of an app created using three tier architecture. The procedure has already been passed into the Data Access Layer and the Business Logic Layer.

How do I code the button for this?

Thanks in advance for replies


Solution

  • Well, basically on the presentation layer, create a button, hook-up its click event.
    Then, on the button click event, call your middle tier method.
    This method will basically only call your data tier, which contacts the database and runs the stored procedure and maps the return to a proper int.
    On the way back to the presentation, simply assign the returned value to the desired text box...

    On the button click:

    MyMiddleTierController lvl2 = new MyMiddleTierController();
    myTextBox.Text = lvl2.GetInvoice("12345");
    

    On the data tier:

    //Open sql connection
    //Map stored proc command to SqlCommand
    using(SqlDataReader reader = myCommand.ExecuteReader()){
       if(reader.Read())
          return reader[0];
    }