[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Code behind concept

Posted on 2005-04-08
3
Medium Priority
?
258 Views
Last Modified: 2008-03-06
I have been programming in ASP using Vbscript and I am trying to learn ASP.NET.

I am looking for a code snippet that does the following:
The User will select a table name from a list. The selection is passed to the "Code Behind"  where a "Stored Procedure" is executed . The results are then returned to the UI, and data is displayed.

Thanks

0
Comment
Question by:novice12
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
MikeMCSD earned 2000 total points
ID: 13742539
in the .aspx page . . .
when an item is click in this list, it will post back to the server and update the table:

<asp:DropDownList AutoPostBack=True ID="ddlShip" Runat=server>
</asp:DropDownList>

in the code-behind:

Private Sub ddlShip_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlShip.SelectedIndexChanged
      'update Orders table
      Dim OrderID As String = Request.QueryString("OrderID")
      Dim shipID As String = ddlShip.SelectedValue
      OrderManager.UpdateShip(OrderID, shipID)
      BindShoppingCart()
      ddlShip.Visible = False
      btnUpdateShip.Visible = True
End Sub

Here is the Function in the "Public Class OrderManager" that call the stored procedure:

Public Shared Function UpdateShip(ByVal orderID As String, ByVal shipID As String)
      Dim connection As New SqlConnection(connectionString)
      Dim command As New SqlCommand("UpdateOrderShip", connection)
      command.CommandType = CommandType.StoredProcedure
      command.Parameters.Add("@orderID", SqlDbType.Int)
      command.Parameters("@orderID").Value = orderID
      command.Parameters.Add("@ShipID", SqlDbType.TinyInt)
      command.Parameters("@ShipID").Value = shipID
      Try
         connection.Open()
         command.ExecuteNonQuery()
      Finally
         connection.Close()
      End Try
   End Function

CREATE Procedure UpdateOrderShip (@orderID int,  @ShipID int)
AS
UPDATE Orders
SET Shipping = @ShipID
WHERE OrderID = @orderID
RETURN
GO

Any questions, let me know.
0
 
LVL 5

Expert Comment

by:vadivhere
ID: 13742659
In the above code, it seems to be there is a problem...

      Dim connection As New SqlConnection(connectionString)
      Dim command As New SqlCommand("UpdateOrderShip", connection)
      command.CommandType = CommandType.StoredProcedure
      command.Parameters.Add("@orderID", SqlDbType.Int)
      command.Parameters("@orderID").Value = orderID
      command.Parameters.Add("@ShipID", SqlDbType.TinyInt)
      command.Parameters("@ShipID").Value = shipID
      Try
         connection.Open()
         command.ExecuteNonQuery()
      Finally
         connection.Close()
      End Try

Let's say that the connection is not opening excecution point will come to the finally block and try to close the connection, but the connection is not opened. I belive, it has to be changed.

Cheers
Vadivel Kumar
0
 
LVL 16

Expert Comment

by:MikeMCSD
ID: 13743997
I have been using that code for over a year with no problem. I did plan on
adding something like this to Finally:

If Connection.State <> System.Data.ConnectionState.Closed Then
     Connection.Close()
End If

This is another approach I have seen somewhere:

Try
   Connection.ConnectionString = ConnectionString()
   Command = New System.Data.SqlClient.SqlCommand
   Connection.Open()
   SqlDataReader = Command.ExecuteReader()
   If SqlDataReader.Read() Then
      Load(SqlDataReader)
   End If
   SqlDataReader.Close()
 Catch e As Exception
   Throw e
 Finally
   If Connection.State <> System.Data.ConnectionState.Closed Then
     Connection.Close()
   End If
 End Try
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question