Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6707783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:40:49+00:00 2026-05-26T07:40:49+00:00

I am trying to display the value returned by a stored procedure in a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:40:50+00:00Added an answer on May 26, 2026 at 7:40 am

    If you want the procedure to output the value of @iMAG you need to set the parameter direction to Output.

    SqlParameter parm = cmd.Parameters.Add( "@iMAG", SqlDbType.Char);
    parm.Direction = ParameterDirection.Output;
    

    EDIT

    You’re creating the @iMAG parameter twice in your code. Just create it once, like this:

    SqlParameter param = cmd.Parameters.Add("@iMAG", SqlDbType.VarChar); 
    param.Direction = ParameterDirection.Output
    param.Value = label1.Text; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

2 Hi! I am trying to create stored procedure that gone return varchar value,
I'm trying to use $.getJSON inside $.post function and display returned value in textbox
I am trying to display a default value (or even NO value) when selected
I'm trying to get the $url value to display from the MySQL database but
I'm trying to display a loading icon while my iPhone app downloads a network
I'm trying to display Key/Value Pairs from a Dictionary to a ListBox. Key Value
Hi I am trying to display the database value on the dropdownlist in the
I'm trying to display an image inside a ListView control based on the value
I am trying to display content from a YQL Query. However, the value I
I'm trying to modify a stored procedure used in an ASP.NET page. By default,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.