Terri
Terri

Reputation: 11

execute stored procedure using sqldatasource and get return value in vb.net

How can I execute a stored procedure using sqldatasource and get the return value in vb.net.

Thanks,

Terri

Upvotes: 1

Views: 11192

Answers (4)

Alex Z
Alex Z

Reputation: 1442

 <asp:SqlDataSource ID="ADSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ADConnection %>"
    SelectCommand="GetProfile" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter ControlID="InputTextBox" Name="Host" PropertyName="Text" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

GetProfile is the stored proc name and Host is parameter name, which is retreived from a texbox called InputTextBox

Upvotes: 0

nunespascal
nunespascal

Reputation: 17724

The method you are looking for is DataBind. Call it using mySqlDataSource.DataBind()

<asp:SqlDataSource 
  ID="sds2" 
  runat="server" 
  ConnectionString="..."
  SelectCommand="spTest"      
  SelectCommandType="StoredProcedure"
  >
  <SelectParameters>
    <asp:ControlParameter ControlID="TextBox1" PropertyName="Text"
                              Name="ParamName" Type="Int32" DefaultValue="0" />
  </SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gv" runat="server" DataSourceID="sds2"></asp:GridView>

The stored procedure is executed when you call DataBind. The DataBind method is called automatically if the DataSourceID property of the GridView control refers to a valid data source control.

Upvotes: 1

Kevin LaBranche
Kevin LaBranche

Reputation: 21078

If you already have the SP returning a value then you have to grab the value in the corresponding event for the data source. AKA - Inserted, Selected, etc...

Here's a couple links illustrating the point.

http://fredrik.nsquared2.com/viewpost.aspx?PostID=162

http://www.velocityreviews.com/forums/t86158-re-how-to-retrieve-an-output-parameter-using-sqldatasource-control.html

Upvotes: 0

SLaks
SLaks

Reputation: 887415

You need to use a SqlConnection with a SqlCommand, like this:

using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("StoredProcedureName", connection)) {
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("SomeParam", someValue);

    object result = command.ExecuteScalar();
}

Upvotes: 0

Related Questions