Wednesday, March 28, 2012

Running a Stored Procedure without passing parameters

HI all,

I'd like to run a simple stored procedure on the Event of a button click, for which I don't need to pass any parameters, I am aware how to run a Stored Procedure with parameters, but I don't know how without, any help would be appreciated please.

thanks.


The code would be similar to what you have if you had parameters. Just chop off the part of the code where you add the parameter.|||

thanks for that, however I can't use the normal way I used with parameters :

SQLDataSource1.InsertParameters("Till_Name").DefaultValue = tillname

As for the above I've used the InsertParameters method of my SQLDataSource1 object, which requires a parameter, although all I want to do is trigger a Stored Procedure. Do you follow?

|||

Have a look at the link

http://msdn2.microsoft.com/en-us/library/w1kdt8w2.aspx

Quoting the relevant portion from the link

"

If the database you are working with supports stored procedures, you can set theSelectCommand property to the name of the stored procedure and theSelectCommandType propertyStoredProcedure to indicate that theSelectCommand property refers to a stored procedure. The following example demonstrates a simple stored procedure that you can create in SQL Server:

CREATE PROCEDURE sp_GetAllEmployees AS
SELECT * FROM Employees;
GO

To configure theSqlDataSource to use this stored procedure, set theSelectCommand text to "sp_GetAllEmployees" and theSelectCommandType property toStoredProcedure.

Most stored procedures use parameters. For more information about using stored procedures with parameters, seeUsing Parameters with the SqlDataSource Control.

At run time,SqlDataSource control submits the text in theSelectCommand property to the database, and the database returns the result of the query or stored procedure to theSqlDataSource control. Any Web controls that are bound to the data source control display the result set on your ASP.NET page.

"

|||

thanks for that ambarsihg, I have my stored procedure as a SelectCommand, however my problem is the syntax of running the SelectCommand from a button press for example.

e.g.

SqlDataSource2.SelectCommandType = SqlDataSourceCommandType.StoredProcedureSqlDataSource2.SelectCommand()
This doesnt work, so how do I do it please?|||Dim myConnectionAsNew SqlConnection([your connection])
Dim myCommandAsNew SqlCommand("[your stored procedure]", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.ExecuteNonQuery()

ExecuteNonQuery executes the Stored Procedure

|||Thanks very much, much head scratching avoided!Smile

No comments:

Post a Comment