Calling SQL Server Stored Procedures in VB 2005

I am new to Visual Basic and am having some difficulty calling a stored procedure in a SQL Server database.

I have set up a Stored Procedure in SQL server, uspGetCompanyDetails, which has a parameter of @CompanyID.

When calling the stored procedure in Visual Basic, I have used the following line of code;

Dim objDataAdapter As New SqlDataAdapter("uspGetCompanyDetails 6", objConnection)

This code fires on a button click event and works fine, however I wish to remove the refernce "6" parameter from the code, and have the parameter @CompanyID enetered into a text box on the VB form, in this case TextBox1, prior to the click event and then passed into the code as a parameter. I have been trying to follow some book examples, however I am getting no where fast.

Any suggestions would be very much appreciated.



Answer this question

Calling SQL Server Stored Procedures in VB 2005

  • Chango V.

    Thanks
  • Mike Harsh

  • Rodrigo Wolburg

    This is code i got from a microsoft site:

    Dim sqlConnection1 As New SqlConnection("Your Connection String")
    Dim cmd As New SqlCommand
    Dim returnValue As Object

    cmd.CommandText = "StoredProcedureName"
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Connection = sqlConnection1

    sqlConnection1.Open()

    returnValue = cmd.ExecuteScalar()

    sqlConnection1.Close()

    This does not seem to work for me. I am having trouble with my CommandText. I am not sure if I should have the CommandText in quotes "" or not.
    If I do not use quotes it tells me that it is not declared and if I use quotes it sees it as a string and not as the procedure name.
    Any thoughts

  • Calling SQL Server Stored Procedures in VB 2005