473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Command Object PArameters

I have written a stored procedure in SQL 2005 as below.

When I call this procedure from QA with the SQL QA Test code the value of
@strNewRef is returned correctly with my limited amount of testing thus far.

When I try to use this procedure from my vb code im getting some unexpected
results.

Using the "VB Code Not Working" code I get incorrect results. The value
returned into m_strNewRef is only ever 1 char long

If I change my vb procedure to the "Working VB Code" I get correct values
returned
By messing about with "Working VB Code" I can see that by not setting the
Size property of the parameter, I get incorrect results ie only 1 character
being returned.

Could soeone please explain this behaviour

Terry Holland
SQL QA Test
=============
declare @strNewRef nvarchar(10)
exec sprGetNextItemRef -999,@strNewRef output
select @strNewRef

VB Code Not Working
==============
Private Sub ExecuteUpdate(ByVal tr As SqlTransaction)

Using cm As SqlCommand = tr.Connection.CreateCommand()
cm.Transaction = tr
cm.CommandType = CommandType.StoredProcedure
cm.CommandText = "sprGetNextItemRef"
cm.Parameters.AddWithValue("@intQuoteID", m_intQuoteID)
cm.Parameters.AddWithValue("@strItemRef", m_strNewRef)
cm.Parameters("@strItemRef").Direction = ParameterDirection.Output

cm.ExecuteNonQuery()

m_strNewRef = cm.Parameters("@strItemRef").Value

End Using
End Sub

If I change my vb procedure to the following I get correct values returned

Working VB Code
===============
Private Sub ExecuteUpdate(ByVal tr As SqlTransaction)
' If Not OnExecuteUpdate(tr) Then Return
Using cm As SqlCommand = tr.Connection.CreateCommand()
cm.Transaction = tr
cm.CommandType = CommandType.StoredProcedure
cm.CommandText = "sprGetNextItemRef"
cm.Parameters.AddWithValue("@intQuoteID", m_intQuoteID)
Dim p As New SqlClient.SqlParameter
With p
.ParameterName = "@strItemRef"
.SqlDbType = SqlDbType.NVarChar
.Size = 10
.Direction = ParameterDirection.Output
.Value = m_strNewRef
End With
cm.Parameters.Add(p)
cm.ExecuteNonQuery()

m_strNewRef = cm.Parameters("@strItemRef").Value

End Using
End Sub

Procedure
===========
USE [Contest03UK]
GO
/****** Object: StoredProcedure [dbo].[sprGetNextItemRef] Script Date:
05/02/2007 11:22:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sprGetNextItemRef]
-- Add the parameters for the stored procedure here
@intQuoteID int,
@strItemRef nvarchar(10) = '' output
AS
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @strLastRef nvarchar(10)
declare @intPointer int
declare @strLeft nvarchar(10)
declare @strRight nvarchar(10)

--Get last Item Ref for Items related to Quote
select top (1)
@strLastRef = item_txt_Reference
from
Item
where
item_quot_int_ID = @intQuoteID
order by
item_int_ID desc
--Set @strLastRef to 0 if the item ref
--of last item is null or empty
if @strLastRef = null
begin
set @strLastRef = 0
end
if @strLastRef = ''
begin
set @strLastRef = 0
end

/*
If last item ref is numeric the next ref should also
be numeric and it should be incremented
ie
Last Next
100 >101
10.1 >10.1

If last item ref is non-numeric the next ref should
increase the last character by 1 ascii value
ie
Last Next
1a >1b
Item 1 >Item 2
*/
if isnumeric(@strLastRef) = 1
begin
set @intPointer = charindex(N'.', @strLastRef)
if @intPointer 0
--non-integer
begin
set @strLeft = left(@strLastRef,@intPointer-1)
set @strRight = substring(@strLastRef,@intPointer + 1, len(@strLastRef))
set @strRight = convert(nvarchar(10), convert(int, @strRight)+1)
set @strItemRef = @strLeft + '.' + @strRight
end
else
begin
--integer
set @strItemRef = convert(nvarchar(10), convert(int, @strLastRef)+1)
end

end
else
begin
--not numeric
set @strLeft = left(@strLastRef,len(@strLastRef)-1)
set @strRight = right(@strLastRef,1)
set @strRight = char(ascii(@strRight)+1)
set @strItemRef = @strLeft + @strRight
end

May 2 '07 #1
0 1153

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stefan Berglund | last post by:
I was looking for a general consensus as to whether the ActiveConnection property of the command object should be set to Nothing or whether it's sufficient to set the command object itself to...
5
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm...
5
by: Teo | last post by:
Hi! Sorry for my many postings on this Newsgroup. I am new to ADO.NET and I got Sceppa's book on ADO.NET. It's a great book but covers a lot on the DataAdapter and Offline Data. Now, if I want to...
0
by: Elliot M. Rodriguez | last post by:
I implemented a very small, basic data access layer for my web application. It works just fine, except for this one bug. One of my methods returns an abstracted dataset. To accomodate X number of...
5
by: Gene | last post by:
What can I do if I want to get the result using the sql command? for example, the select command is "select Name from Employee where StaffID=10" How to get the "Name"??? dim Name as string and...
3
by: Ed | last post by:
Hi, I want to load data to a table in Sql Server from a dataset table in my vb.net app using a dataAdapter. I know how to do this as follows (my question is to see if I can reduce the amount...
5
by: mabond | last post by:
Hi VB.NET 2005 Express edition Microsoft Access 2000 (SP-3) Having trouble writing an "insert into" command for a Microsoft table I'm accessing through oledb. I've tried to follow the same...
2
by: explode | last post by:
I made nova oledbdataadapter select update insert and delete command and connection veza. dataset is Studenti1data, I made it by the new data source wizard,and made datagridview and bindingsource...
3
by: iKiLL | last post by:
Hi all I am having problems getting my SqlCeDataAdapter to Update the SQL Mobile Data base. i am using C# CF2. I have tried this a number of different ways. Starting with the command builder...
3
by: Sagar | last post by:
Hi. I am working on a project to migrate a web application from 1.1 to 2.0 Within in the DAL of the application, there is a call to below function that builds a command object for later use. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.