views:

46

answers:

2

2

Hi! I am trying to create stored procedure that gone return varchar value, and that value I need to display in textbox.

This is the code for stored procedure:

Create PROCEDURE Status @id_doc int, @Name varchar(50) OUTPUT
AS
select  @Name =items.name    
from Doc,Items where @id_doc=IDN and doc.IDN=Items.ID 
return @Name 

This is the code in vb.net where i need to display returned value from procedure in textbox:

Public Sub Current()
    Dim dtc As New Data.DataTable
    Dim dr As SqlClient.SqlDataReader
    Dim da As New SqlClient.SqlDataAdapter
    Dim cmd As New SqlClient.SqlCommand
    Dim id_doc As Integer
    Dim dset As New DataSet
    Dim recordset As DataRow
    Dim Name As String

    Try
        id_doc = idDocExplorer
        cmd.Connection = pubCon
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "Status"
        cmd.Parameters.Add("@id_doc", id_doc)
        cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50)
        cmd.Parameters("@Name ").Direction = ParameterDirection.Output
        cmd.ExecuteScalar()

        Name = cmd.Parameters("@Name").Value
       TextBox1.text=Name
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        dtc = Nothing
        dr = Nothing
        da = Nothing
    End Try
End Sub

When I try to execute this code I get this message in exception:

"Conversion failed when converting the varchar value 'Blue color' to data type int."

What I am doing wrong? Thanks!

+3  A: 

RETURN takes an integer argument only so return @Name is causing it

Use this instead to return a record set (with a proper JOIN too)

select
    items.name    
from
    Doc
    JOIN
    Items ON doc.IDN = Items.ID 
where
    @id_doc = IDN

Although, it can be written like this too because @id_doc = doc.IDN = Items.ID:

select
    items.name    
from
    Items
where
    Items.ID = @id_doc

If you want to use an OUTPUT parameter rather than a recordset, then simply remove the RETURN statement and leave the assign to @Name

ALTER PROCEDURE Status @id_doc int, @Name varchar(50) OUTPUT
AS
select @Name = items.name from Items where Items.ID = @id_doc
GO
gbn
Thank you so much! Now it working fine!
A: 

I think you are passing "Blue Color" as a parameter value under id_doc.
And the stored procedure call could be failing because it accepts an int type as 1st input parameter

shahkalpesh
no, OP is trying to return "blue colour" via the return statement
gbn
@gbn: What happens if you pas a varchar value for an int parameter?
shahkalpesh
The same error, but it's not the cause **here**
gbn
@gbn: Was the sight of `RETURN` statement reason for your answer? For me, I didn't know what user could be passing for `id_doc`.
shahkalpesh
The vb.net has `Dim id_doc As Integer` so you can't pass "blue colour" to SQL; it would fail earlier. But the error message is a SQL error, not .net, so it must be in the SQL code. I did not jump on RETURN immediately, I worked through and analyzed the problem...
gbn
shahkalpesh