1

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

Thanks,

Terri

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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();
}

1 Comment

I think the question is using the sqldatasource how would one get the return value..... :-)
0

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

Comments

0
 <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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.