I am trying to display the value returned by a stored procedure in a C# WPF App. The user types in their card number, clicks “Submit” and the following code runs:
if (textBox1.Text != "")
{
long numCardNumber; //Card number (card number scanned and converted to a number)
string strCardNumber; // Card number (card number stored as string)
char[] MyChar = {';','?'};
label2.Text = textBox1.Text.TrimEnd(MyChar); // Trims end
label2.Text = label2.Text.TrimStart(MyChar); //Trims beginning
strCardNumber = label2.Text;
try //try and convert the string to a number (if valid numerical characters
{
numCardNumber = Convert.ToInt64(strCardNumber);
label1.Text = "Your number is: " + label2.Text; // Scanned " + numOfTimeScanned + " times";
}
catch(FormatException) // thrown if input characters are not valid numeric
{
label1.Text = "NOT A VALID CARD INPUT!";
}
SqlConnection connection = new SqlConnection("Data Source=TestServer;Initial Catalog=Testdb;Persist Security Info=True;User ID=testuser;Password=testpass");
connection.Open();
SqlCommand cmd = new SqlCommand("GW_MAGTOOCR", connection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@iMAG", SqlDbType.Char);
param.Value = label1.Text;
cmd.Parameters.Add(param);
SqlParameter retval = cmd.Parameters.Add("@iMAG", SqlDbType.VarChar);
retval.Direction = ParameterDirection.Output;
int returnvalue = (int)cmd.Parameters["@iMAG"].Value;
} //END IF
else
{
label1.Text = "Nothing Submitted!";
}
The following are the 2 stored procedures involved. Should be returning an integer
Stored procedure 1 called GW_MAGTOOCR that is called from code (which in turn, calls the stored procedure 2 called CDS_INTTOHEX).
STORED PROCEDURE 1:
USE [Testdb]
GO
/****** Object: StoredProcedure [dbo].[GW_MAGTOOCR] */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[GW_MAGTOOCR](
@iMag char(18),
@oOCR varchar(10) output
)
--WITH ENCRYPTION
as
declare
@vTStr varchar(9),
@vPrefix char(2),
@vCRC varchar(2),
@vTemp int
begin
IF isnumeric(@iMag) = 0
Begin
Set @oOcr = Null
Return
End
-- get the prefix
select @vTemp = convert(int, substring(@iMag, 4, 3))
execute CDS_INTTOHEX @vTemp, @vTStr output
select @vPrefix = substring(@vTStr, 7, 2)
-- get the CRC
select @vTemp = convert(int, substring(@iMag, 7, 2))
execute CDS_INTTOHEX @vTemp, @vTStr output
select @vCRC = substring(@vTStr, 8, 1)
select @vTemp = convert(int, substring(@iMag, 9, 2))
execute CDS_INTTOHEX @vTemp, @vTStr output
select @vCRC = @vCRC + substring(@vTStr, 8, 1)
-- get the account #
select @vTemp = convert(int, substring(@iMag, 11, 8))
execute CDS_INTTOHEX @vTemp, @vTStr output
select @oOCR = @vPrefix + @vCRC + substring(@vTStr, 3, 6)
/*select output = @vPrefix + @vCRC + substring(@vTStr, 3, 6)*/
end
STORED PROCEDURE 2:
USE [Testdb]
GO
/****** Object: StoredProcedure [dbo].[CDS_INTTOHEX] *****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[CDS_INTTOHEX] (
@iInteger int,
@oHex varchar(9) output
)
--WITH ENCRYPTION
as begin
declare
@vDec int,
@vHex char,
@vLen int,
@vDivisor int,
@vQuotient int,
@vRemainder int,
@vTemp varchar(9)
select @vTemp = ''
select @vQuotient = @iInteger
select @oHex = ''
select @vLen = 8
while @vLen > 0 begin
select @vLen = @vLen - 1
select @vDivisor = power(16, @vLen)
select @vDec = floor(@vQuotient / @vDivisor)
if @vDec < 10 select @vHex =convert(char, @vDec)
else if @vDec = 10 select @vHex = 'A'
else if @vDec = 11 select @vHex = 'B'
else if @vDec = 12 select @vHex = 'C'
else if @vDec = 13 select @vHex = 'D'
else if @vDec = 14 select @vHex = 'E'
else if @vDec = 15 select @vHex = 'F'
select @vTemp = @vTemp + @vHex
select @vQuotient = @vQuotient - (@vDec * @vDivisor)
end
select @oHex = @vTemp
end
Any help would be GREATLY appreciated. I’m quite sure the issue lies in my C# code, as I can successfully call the stored procedure from within SQL management studio and receive the expected value back.
EDIT
I’ve updated my code a bit, I’ve created a simple stored proc that basically calls the first two (initially MAGTOOCR) which seems to run fine when I run it from Management Studio, now I’m just not sure if my code is calling it correctly, here is my updated code:
connection.Open();
SqlCommand cmd = new SqlCommand("CNBID", connection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = cmd.Parameters.Add("@iMAG", SqlDbType.Char, 18);
param.Direction = ParameterDirection.Output;
param.Value = label2.Text;
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
label2.Text = "Reading";
}
}
else
{
label2.Text = "NOTHING READ!";
}
I know the code doesn’t output anything useful at the end, but I want to watch the variables while I debug. I’m sure I’m not doing something correctly above. Any help again is appreciated.
If you want the procedure to output the value of
@iMAGyou need to set the parameter direction toOutput.EDIT
You’re creating the
@iMAGparameter twice in your code. Just create it once, like this: