2010-07-01 205 views
2

我想創建存儲過程,返回varchar值,我需要顯示在文本框中的值。存儲過程返回varchar

這是爲存儲過程的代碼:

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 

這是在vb.net代碼,其中i在需要文本框以顯示從過程返回值:

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

當嘗試執行此代碼我收到此異常信息:

「String [1]:Size屬性的無效大小爲0。

我做錯了什麼?謝謝!

回答

3

創建時需要指定@Name參數的size

您還可以在"@Name "串一個額外的空間 - 它應該是"@Name"

cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50) 
2

嘗試改變:

cmd.Parameters.Add("@Name", SqlDbType.VarChar) 

到:

cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50) 

定義大小的輸出參數

0

你應該嘗試調用ExecuteNonQuery而不是ExecuteScalar