Passing Value from Stored Procedure

Receiving error, "String[1]:Size property has an invalid size 0

Page 1 of 1

13 Replies - 3768 Views - Last Post: 26 April 2010 - 12:22 PM Rate Topic: -----

#1 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Passing Value from Stored Procedure

Posted 08 April 2010 - 01:17 PM

Hello All,

I've been working on a project, but have decided to take another direction on it, after much and continued struggling.

I started out trying to use data binding, but was advised to use straight SQL statements in the code. So what I'm trying to do now: I have 2 forms, frm1 and frm2. frm1 has a combobox, which is showing all the data fine from a table, and an OK button. Upon clicking on this button, I need frm2 to load, correctly populating its textboxes and comboboxes with the corresponding data, based on the selection made on frm1. This is what I have so far:

frm1:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Windows.Forms

Public Class frmLocations
    Dim conn As New SqlConnection("Data Source=f03d3s-dev01; Initial Catalog=dos_track;User Id=vbuser; Password=tran3;")

    Private Sub frmLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn.Open()
        Call FillCombo()
    End Sub

    Private Sub FillCombo()

        Dim sql As String = "Select distinct Site,depot_refnbr From Locations order by Site asc"
        Dim cmd As New SqlCommand(sql, conn)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        Dim dt As New DataTable

        da.Fill(ds, "Locations")
        Dim dr As SqlDataReader = cmd.ExecuteReader
        Do While dr.Read
            cboLocations.Items.Add(dr("Site"))
        Loop
        dr.Close()
        cmd.Cancel()
        cmd.Dispose()
    End Sub

    Private Sub cboLocations_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLocations.SelectedIndexChanged
    End Sub

    Private Sub btnLocationOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLocationOk.Click
        clsMain.Location_str_value = cboLocations.Text
        'clsMain.Location_depot_refnbr = cboDepot.

        Dim f As New frmLocationInfo
        f.ShowDialog()
    End Sub 


I also have a stored procedure to pass the desired value, depot_refnbr to frm2:
ALTER PROCEDURE dbo.sp_CorrespDepot

	@CorrespDepot varchar(50) output
AS
	/* SET NOCOUNT ON */ 
	SELECT DISTINCT depot.depot_name, depot.depot_refnbr
FROM         depot INNER JOIN
                      locations ON depot.depot_refnbr = locations.depot_refnbr
		RETURN


frm2:
Public Class frmLocationInfo
    Dim conn As New SqlConnection("Data Source=f03d3s-dev01; Initial Catalog=dos_track;User Id=vbuser; Password=tran3;")

    Private Sub frmLocationInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        conn.Open()
        Call PopulateTextboxes()
        Call FillDepotCombo()
    End Sub

    Private Sub PopulateTextboxes()
    End Sub

Private Sub FillDepotCombo()

        Dim sql As String = "Select distinct depot_name From Depot order by depot_name asc"
Dim dtDepot As New DataTable("Depot")

        Dim cmdCorrespDepot As New SqlCommand("sp_CorrespDepot", conn)
        cmdCorrespDepot.CommandType = Data.CommandType.StoredProcedure
        cmdCorrespDepot.Parameters.Add("@CorrespDepot", SqlDbType.VarChar, 50)

   With cmdCorrespDepot
            .Parameters.Add("@CorrespDepot", SqlDbType.VarChar).Direction = ParameterDirection.InputOutput
        End With

        dtDepot.Load(cmdCorrespDepot.ExecuteReader())

 cboDepot.DataSource = dtDepot
        cboDepot.DisplayMember = "depot_name"
        cboDepot.ValueMember = "depot_refnbr"

conn.Close()
    End Sub
End Class


I also have a clsMain, in where I keep global variables. When debugging, after clicking on the OK button on frm1, I keep receiving the error, "String[1]: the Size property has an invalid size of 0". Can someone please advise on this? :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Value from Stored Procedure

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Passing Value from Stored Procedure

Posted 08 April 2010 - 01:21 PM

The VB.NET Programmers forum isn't the place t ask for programming help, it's for discussions on more advanced topics and such. Moved to regular VB.NET Forum :)
Was This Post Helpful? 0
  • +
  • -

#3 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

Re: Passing Value from Stored Procedure

Posted 08 April 2010 - 02:31 PM

Check this line in your code
With cmdCorrespDepot
            .Parameters.Add("@CorrespDepot", SqlDbType.VarChar).Direction = ParameterDirection.InputOutput
        End With




Your not specifying a size for SqlDbType.VarChar
Was This Post Helpful? 1
  • +
  • -

#4 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Passing Value from Stored Procedure

Posted 13 April 2010 - 06:37 AM

Hi Keak and everyone,

I tried putting in a size of 20 in the line you specified, i.e. .Parameters.Add("@CorrespDepot", SqlDbType.VarChar, 20).Direction = ParameterDirection.InputOutput, but I'm now getting a "Procedure or Function 'sp_CorrespDepot' expects parameter '@CorresDepot', which was not supplied" error. It appears this is happening at the "Dim dr As SqlDataReader = cmdCorrespDepot.ExecuteReader" line.

Any advice/input would be appreciated. Thanks! :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#5 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Passing Value from Stored Procedure

Posted 21 April 2010 - 09:55 AM

Hello All,

I'm now receiving the following error, ""Procedure or Function 'sp_CorrespDepot' expects parameter '@CorrespDepot', which was not supplied" after selecting a location on frm1 and clicking the "OK" button.

Here is my stored procedure:


ALTER PROCEDURE sp_CorrespDepot
(
	@CorrespDepot varchar(50) output,
	@Depot_Nbr int)
	
	as
	SELECT DISTINCT depot.depot_name 
FROM         depot INNER JOIN
                      locations ON depot.depot_refnbr = locations.depot_refnbr
                      where depot_name = @CorrespDepot


Any assistance would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1387
  • View blog
  • Posts: 4,467
  • Joined: 25-September 09

Re: Passing Value from Stored Procedure

Posted 21 April 2010 - 10:51 AM

OK, I've not done a lot with stored procedures but It appears that the procedure is wanting a value and in your code where you are assigning the parameter, you're not passing it a value for the procedure to work.

It is my understanding that if you were to exec this procedure you would do something like
exec sp_CorresDepot @CorrespDepot = 'some depot', @Depot_Nbr = 10 and it would pass those values to the query.

I don't see in your code above where you are telling the procedure what you're looking for.

Maybe try something like:
With cmdCorrespDepot
     .Parameters.AddWithValue("@CorrespDepot", "TheDepot")
End With


But keep in mind I'm guessing here.

This post has been edited by CharlieMay: 21 April 2010 - 10:53 AM

Was This Post Helpful? 1
  • +
  • -

#7 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

Re: Passing Value from Stored Procedure

Posted 21 April 2010 - 11:40 AM

Hi @Daniness,

CharlieMay is basically on the right track. You've declared your parameter object, but you haven't assigned any value to it. Try modifying your parameter declaration like so:
Private Sub FillDepotCombo() 
 
        Dim sql As String = "Select distinct depot_name From Depot order by depot_name asc" 
Dim dtDepot As New DataTable("Depot") 
 
        Dim cmdCorrespDepot As New SqlCommand("sp_CorrespDepot", conn) 
        cmdCorrespDepot.CommandType = Data.CommandType.StoredProcedure 
' Here Im adding a value as well .Value = txtValue.Text, You can use whatever 
' variable you have that stores the information for @CorrespDepot
        cmdCorrespDepot.Parameters.Add("@CorrespDepot", SqlDbType.VarChar, 50).Value = txtValue.Text 
 
'Then here, set the direction like this
   With cmdCorrespDepot 
            .Parameters("@CorrespDepot").Direction = ParameterDirection.InputOutput 
        End With 
 
        dtDepot.Load(cmdCorrespDepot.ExecuteReader()) 
 
 cboDepot.DataSource = dtDepot 
        cboDepot.DisplayMember = "depot_name" 
        cboDepot.ValueMember = "depot_refnbr" 
 
conn.Close() 
    End Sub 




Also, I noticed in your Stored Procedure you have @CorrespDepot as an Output parameter, but in the definition of your sp you're not assigning it a value. An output parameter is used to get a value back from the sp where a value is given to it from with in the sp. For example, some where in your sp you would have a statement like:
@CorrespDept = Select Scope_Identity()



Then you would use the value assigned to @CorrespDepot to do further calculation.

By the definition of your stored procedure, it looks more to me that you should remove the output in your parameter declaration, since you're only using it as an input value... (Sounds confusing, but I hope you understand what I am talking about)
Was This Post Helpful? 1
  • +
  • -

#8 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Passing Value from Stored Procedure

Posted 22 April 2010 - 08:58 AM

Thanks so much, CharlieMay and keakTheGEEK!

Keak, I've tried what you suggested for the parameter declaration:
Private Sub FillDepotCombo()

        Dim sql As String = "Select distinct depot_name From Depot order by depot_name asc"
        Dim dtDepot As New DataTable("Depot")

        Dim cmdCorrespDepot As New SqlCommand("sp_CorrespDepot", conn)
        cmdCorrespDepot.CommandType = Data.CommandType.StoredProcedure
        cmdCorrespDepot.CommandText = "sp_CorrespDepot"

        cmdCorrespDepot.Parameters.Add("@CorrespDepot", SqlDbType.VarChar, 50).Value = cboDepot.Text
        cmdCorrespDepot.Parameters.Add("@Depot_Nbr", SqlDbType.Int).Value = cboDepot.ValueMember

        With cmdCorrespDepot
            .Parameters("@CorrespDepot").Direction = ParameterDirection.InputOutput
        End With

   [color="#0000FF"]     dtDepot.Load(cmdCorrespDepot.ExecuteReader())[/color]        
        cboDepot.DataSource = dtDepot
        cboDepot.DisplayMember = "depot_name"
        cboDepot.ValueMember = "depot_refnbr"

        conn.Close()
     End Sub
   End Class



However, I'm getting a "FormatException was unhandled. Failed to convert parameter value from a String to a Int32" error. The colored line is where it is occurring. I don't see where a conversion would be happening. Any ideas?

This post has been edited by daniness: 22 April 2010 - 09:04 AM

Was This Post Helpful? 0
  • +
  • -

#9 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

Re: Passing Value from Stored Procedure

Posted 22 April 2010 - 11:31 AM

Not absolutely sure, but I suspect that it has something to do with this line:
cmdCorrespDepot.Parameters.Add("@Depot_Nbr", SqlDbType.Int).Value = cboDepot.ValueMember




cboDepot.ValueMember - What data type is that? Try this:
cmdCorrespDepot.Parameters.Add("@Depot_Nbr", SqlDbType.Int).Value = Convert.ToInt32(cboDepot.ValueMember)



If that doesn't work, you may need to pass it like this:
cmdCorrespDepot.Parameters.Add("@Depot_Nbr", SqlDbType.Int).Value = Convert.ToInt32(cboDepot.ValueMember.ToString())



See if that works...
Was This Post Helpful? 0
  • +
  • -

#10 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Passing Value from Stored Procedure

Posted 22 April 2010 - 12:51 PM

View PostkeakTheGEEK, on 22 April 2010 - 10:31 AM, said:

cmdCorrespDepot.Parameters.Add("@Depot_Nbr", SqlDbType.Int).Value = Convert.ToInt32(cboDepot.ValueMember)



If that doesn't work, you may need to pass it like this:
cmdCorrespDepot.Parameters.Add("@Depot_Nbr", SqlDbType.Int).Value = Convert.ToInt32(cboDepot.ValueMember.ToString())



See if that works...


Hey Keak,

Thanks for your reply. I tried both and they resulted in the error, "Input string was not in a correct format". Any other ideas?

This post has been edited by daniness: 23 April 2010 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

#11 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

Re: Passing Value from Stored Procedure

Posted 22 April 2010 - 02:19 PM

So typically, the error "Input string was not in a correct format" could mean that you may have other characters in the string that aren't compatible with the type you are trying to convert it to.

I would run a debug statement to check and see what the value of cboDepot.ValueMember is before you execute the command

try this debug statement:
System.Diagnostics.Debug.Print(cboDepot.ValueMember)



Again you may need to do it like this:
System.Diagnostics.Debug.Print(cboDepot.ValueMember.ToString())



Debug.Print() takes a string as a parameter and I am not sure what data type cboDepot.ValueMember is.

You can put that line of code either before or after where you are adding your parameters, or you can set a break point and execute it directly in the immediate window...

This will show you what the data is that you are actually trying to set your parameter value to. It may or may not be what you think it is...

Basically all I'm saying is to let us know what value you're setting cboDepot to.

This post has been edited by keakTheGEEK: 22 April 2010 - 10:04 PM

Was This Post Helpful? 1
  • +
  • -

#12 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Passing Value from Stored Procedure

Posted 26 April 2010 - 05:50 AM

Good Morning Keak and everyone,

I tried the debug statement suggested and it revealed that the value of cboDepot.Valuemember is "". So, it looks like it's empty, which I don't understand because I thought the line:
cmdCorrespDepot.Parameters.Add("@Depot_Nbr", SqlDbType.Int).Value = Convert.ToInt32(cboDepot.ValueMember)
was passing the value, @Depot_Nbr, which I have set as follows in a stored procedure:
ALTER PROCEDURE sp_CorrespDepot
(
	@CorrespDepot varchar(50) output,
	@Depot_Nbr int)
	
	as
	SELECT DISTINCT depot.depot_name 
FROM         depot INNER JOIN
                      locations ON depot.depot_refnbr = locations.depot_refnbr
                      where depot_name = @CorrespDepot and
                      depot.depot_refnbr = @Depot_Nbr
Sigh, I'm so lost :stupid:
Was This Post Helpful? 0
  • +
  • -

#13 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1387
  • View blog
  • Posts: 4,467
  • Joined: 25-September 09

Re: Passing Value from Stored Procedure

Posted 26 April 2010 - 08:24 AM

OK, daniness, I'm not sure if this is the problem as I'm reading down through older code you've submitted but I did notice this
Dim sql As String = "Select distinct depot_name From Depot order by depot_name asc"

and then at the end of your code you have
cboDepot.DataSource = dtDepot
        cboDepot.DisplayMember = "depot_name"
        cboDepot.ValueMember = "depot_refnbr"


This is all post in your post #8

It appears that you have only selected depot_name
Try changing it to
Dim sql As String = "Select distinct depot_name, depot_refnbr From Depot order by depot_name asc"

Was This Post Helpful? 0
  • +
  • -

#14 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

Re: Passing Value from Stored Procedure

Posted 26 April 2010 - 12:22 PM

View Postdaniness, on 26 April 2010 - 04:50 AM, said:

Good Morning Keak and everyone,

I tried the debug statement suggested and it revealed that the value of cboDepot.Valuemember is "". So, it looks like it's empty, which I don't understand because I thought the line:
cmdCorrespDepot.Parameters.Add("@Depot_Nbr", SqlDbType.Int).Value = Convert.ToInt32(cboDepot.ValueMember)
was passing the value, @Depot_Nbr, which I have set as follows in a stored procedure:
ALTER PROCEDURE sp_CorrespDepot
(
	@CorrespDepot varchar(50) output,
	@Depot_Nbr int)
	
	as
	SELECT DISTINCT depot.depot_name 
FROM         depot INNER JOIN
                      locations ON depot.depot_refnbr = locations.depot_refnbr
                      where depot_name = @CorrespDepot and
                      depot.depot_refnbr = @Depot_Nbr
Sigh, I'm so lost :stupid:


Hi @Daniness,

Don't beat yourself up over this. This is some complicated stuff. Every programmer has struggled with trying to learn something new. It takes practice and patience. You will figure this out soon enough. We've all been there before.

So, it looks to me that you're only using @Depot_Nbr as an input parameter and not an output parameter. This stored procedure:
ALTER PROCEDURE sp_CorrespDepot
(
	@CorrespDepot varchar(50) output,
	@Depot_Nbr int)
	
	as
	SELECT DISTINCT depot.depot_name 
FROM         depot INNER JOIN
                      locations ON depot.depot_refnbr = locations.depot_refnbr
                      where depot_name = @CorrespDepot and
                      depot.depot_refnbr = @Depot_Nbr


Doesn't set @Depot_Nbr to any value.

Your WHERE Clause:
 where depot_name = @CorrespDepot and
depot.depot_refnbr = @Depot_Nbr



This only checks if a value in column Depot_Refnbr and column depot_name match whatever values you have stored in your parameters @Depot_Nbr and @CorrespDepot. If the two values match, then return that record as part of the overall result set. @Depot_Nbr is actually getting its value from cboDepot.ValueMember (Set in your VB .NET code). The only problem though is you have cboDepot.ValueMemeber set to an empty string when passing it to your stored procedure. cboDepot.ValueMemeber should already have a value when you add the parameter to your command object. Somewhere in your application you should be setting cboDepot.ValueMember to something. So say somewhere in your VB .NET code, you have a statement like this:
cboDepot.ValueMember = "250"



Then this statement:
cmdCorrespDepot.Parameters.Add("@Depot_Nbr", SqlDbType.Int).Value = Convert.ToInt32(cboDepot.ValueMember)



Sets @Depot_Nbr = 250

To also use @CorrespDepot as an output parameter, you need to set it equal to something in your SELECT Statement. So, essentially you would have something like this:
ALTER PROCEDURE sp_CorrespDepot
(
	@CorrespDepot varchar(50) output,
	@Depot_Nbr int
)
	
as
              -- Output depot.depot_name to @CorrespDepot
SELECT DISTINCT @CorrespDepot = depot.depot_name
FROM depot 
    INNER JOIN locations 
        ON depot.depot_refnbr = locations.depot_refnbr
where depot_name = @CorrespDepot 
and depot.depot_refnbr = @Depot_Nbr



That should set @CorrespDepot to depot.depot_name. The way your sp is written, you're using @CorrespDepot as both an input and output parameter. But in order to get the output functionality to work, you need to set that parameter equal to a column value in your SELECT part of your sql.

It sounds like the main thing that you need to realize is that the way you currently have your code structured, cboDepot.ValueMember needs to have its value set in your VB .NET code before you create the parameter object related to it. cboDepot.ValueMember is what's feeding the value for setting your @Depot_RefNbr sql input parameter. You need to check what's going on in your application where that value gets set before you call the sp.

Here are a few links with information about input & output parameters in Sql:
Using a Stored Procedure with Output Parameters
Using SQL Server Input/Output Parameters

There's a lot more too just search sql input output parameters on google. Hope that points you in the right direction...

EDIT: Tried to explain things more clearly about your input and input/output parameters

This post has been edited by keakTheGEEK: 26 April 2010 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1