Using a Built-in Stored Procedure With Parameters : Stored Procedure Parameters « ADO.net Database « ASP.Net






Using a Built-in Stored Procedure With Parameters

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
  <head><title>Using a Built-in Stored Procedure With Parameters</title></head>
  <body>
    <form runat="server" method="post">
      <asp:ListBox id="lbCustomers" runat="server" Size="1" />
      <asp:Button id="btnSubmit" runat="server"
                  Text="Submit" OnClick="Submit" />
      <br/><br/>
      <asp:DataGrid id="dgOutput" runat="server" />
    </form>
  </body>
</html>

<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
  If Not IsPostBack Then
    Dim strConnection As String = ConfigurationSettings.AppSettings("NorthWind")
    Dim objConnection As New SqlConnection(strConnection)
    Dim objCommand As New SqlCommand("SELECT CustomerID, CompanyName " & _
                                     "FROM Customers", objConnection)

    objConnection.Open()

    lbCustomers.DataSource = objCommand.ExecuteReader()
    lbCustomers.DataTextField = "CompanyName"
    lbCustomers.DataValueField = "CustomerID"
    lbCustomers.DataBind()

    objConnection.Close()
  End If
End Sub

Sub Submit(ByVal Source As Object, ByVal E As EventArgs)

  Dim strConnection As String = ConfigurationSettings.AppSettings("NWind")
  Dim objConnection As New SqlConnection(strConnection)
  Dim objCommand As New SqlCommand("CustOrdersOrders", objConnection)
  objCommand.CommandType = CommandType.StoredProcedure

  Dim objParameter As New SqlParameter("@customerid", SqlDbType.NChar, 5)
  objCommand.Parameters.Add(objParameter)
  objParameter.Direction = ParameterDirection.Input
  objParameter.Value = lbCustomers.SelectedItem.Value

  objConnection.Open()

  dgOutput.DataSource = objCommand.ExecuteReader()
  dgOutput.DataBind()

  objConnection.Close()
End Sub
</script>
           
       








Northwind.zip( 736 k)

Related examples in the same category

1.Using Stored Procedures With Output Parameters
2.Using Stored Procedures With Parameters: filter