Paso de Parametros

26/07/2004 - 21:59 por Edilberto Arteaga Lopez | Informe spam
Hola a Todos

Como hago para pasar parametros a un Store Procedure desde ASP.NET usando
VB.NET

el SP lo llamo desde Query Analizer como Exec local_sp 1 donde 1 es el
modificador de la consulta en el SP este valor es variable y la va a tomar
desde una Lista Desplegable


Saludos
 

Leer las respuestas

#1 Franco Figún
27/07/2004 - 01:07 | Informe spam
Tenes varias formas, por ejemplo:

Desde un combobox, seria asi:
sql = "Select * from Employees where Lastname = '" & ddl.selecteditem.text &
"'"

Usando un query parametrizado, seria asi:

sql = "Select * from Employees where Lastname =@LastName"

Aca tenes un ejemplo un buen ejemplo:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.1">
<title>Populating a DropDownList from a RadioButtonList</title>
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
Dim strConn as string "server=YourServer;uid=YourUID;pwd=YourPWD;database=Northwind"
Dim MySQL as string = "Select CategoryID, CategoryName from NWCategories"
Dim MyConn as New SQLConnection(strConn)
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(MySQL, MyConn)
MyConn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
rb1.DataSource = objDR
rb1.DataBind()
end if
End Sub
Sub popddl(Source as Object, E as EventArgs)
Dim strConn as string "server=YourServer;uid=YourUID;pwd=YourPWD;database=Northwind"
Dim MySQL as string = "GetProductsByCategory" ' Substitute Stored Proc
Name for SQL string
Dim MyConn as New SQLConnection(strConn)
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(MySQL, MyConn)
cmd.CommandType=CommandType.StoredProcedure ' Tells application
that it's using a Stored Procedure
cmd.parameters.add(New SQLParameter("@CatID", rb1.selecteditem.value))
MyConn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
ddl.DataSource = objDR
ddl.DataBind()
ddl.visible="true"
lblProduct.text="Products in " & rb1.selecteditem.text & " Category"
label1.text= ("Selected Value = " & rb1.selecteditem.value )
End Sub
</script>
</head>
<body>
<Form id="form1" runat="server">
<table border="0">
<tr>
<td align="Left" valign="Top"><b><i>Categories:</i></b><br>
<asp:RadioButtonList id="rb1" datatextfield="CategoryName"
datavaluefield="CategoryID" runat="server">
</asp:RadioButtonList> <asp:Button id="button1" Text="Get Products"
onclick="popddl" runat="server" />
</td>

<td align="Left" valign="Top"><b><i><asp:Label ID="lblProduct"
runat="server" /></i></b><br>
<asp:DropDownList id="ddl" datatextfield="Productname" visible="false"
runat="server" /></td>
</tr>
</table>

</Form>
<asp:Label ID="label1" runat="server" />
</body>

FF
www.francofigun.com.ar
www.microsofties.com.ar
Yahoo MSN:

"Edilberto Arteaga Lopez" wrote in message
news:
Hola a Todos

Como hago para pasar parametros a un Store Procedure desde ASP.NET usando
VB.NET

el SP lo llamo desde Query Analizer como Exec local_sp 1 donde 1 es el
modificador de la consulta en el SP este valor es variable y la va a tomar
desde una Lista Desplegable


Saludos


Preguntas similares