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 6213405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:41:24+00:00 2026-05-24T06:41:24+00:00

I am having a hard time trying to figure out which data type I

  • 0

I am having a hard time trying to figure out which data type I would use in C# to enter data into my table in my database which contains a decimal(5,2). When I tried using a C# decimal data type, it said that It had an error converting numeric to decimal. When I tried string, It said that it could not convert nvarchar to decimal. When I tried float… Same thing happened, except the excuse was a “real” data type. double also failed to work.

I have a stored procedure which enters the data into my table, but before I run of and cast data types in my stored procedure to the actual decimal, is there any other way I can rather Convert a c# data type to fit in my decimal(5,2) field?

private void btnAddClientComputer_Click(object sender, EventArgs e)
{
    SQLCommands comm = new SQLCommands();
    try
    {
        comm.AddClientComputer(int.Parse(cbCustomerID.Text), cbAction.Text, decimal.Parse(tbCost.Text));
    }
    catch (FormatException)
    {
        MessageBox.Show("The cost you have entered is invalid. Please ensure the cost is above 0, and is an actual number", "Invalid Input at Cost", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    }
}

…

public void AddClientComputer(int CustomerID, string Action, decimal Cost)
{
    try
    {
        comm = new SqlCommand("UspAddClientComputer", conn); // Stored Procedure - see sql file
        comm.Parameters.AddWithValue("@CustomerID", CustomerID);
        comm.Parameters.AddWithValue("@Action", Action);
        comm.Parameters.AddWithValue("@Cost", Cost);
        comm.CommandType = CommandType.StoredProcedure;
        comm.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
    }
}

…

CREATE TABLE ClientComputers
            (ClientComputerID int Identity(1,1) primary key clustered
            ,CustomerID int
            ,Action varchar(7) check (Action = 'Upgrade' OR Action = 'Store')
            ,Cost decimal(5,2) check (Cost > 0)
            ,Constraint FKCustomerComputer FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID));
Go

…

CREATE PROCEDURE uspAddClientComputer @CustomerID int, @Action varchar(7), @Cost decimal(5,2)
AS
BEGIN TRY
    BEGIN TRANSACTION TrnAddClientComputer;
     INSERT INTO [TCTdb].[dbo].[ClientComputers]
           ([CustomerID]
           ,[Action]
           ,[Cost])
     VALUES
           (@CustomerID
           ,@Action
           ,@Cost)
    COMMIT TRANSACTION TrnAddClientComputer;
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION TrnAddClientComputer;

    DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT 
        @ErrorMessage = ERROR_MESSAGE(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

    RAISERROR (@ErrorMessage,
               @ErrorSeverity,
               @ErrorState
               );
END CATCH
GO

enter image description here

  • 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-24T06:41:25+00:00Added an answer on May 24, 2026 at 6:41 am

    I have found a way to fix my problem under the circumstances, thanks to sll bringing a few things under my attention. The following code worked for me.

    private void btnAddClientComputer_Click(object sender, EventArgs e)
    {
        SQLCommands comm = new SQLCommands();
        double trycost;
        if (double.TryParse(tbCost.Text,out trycost))
        {
            comm.AddClientComputer(int.Parse(cbCustomerID.Text), cbAction.Text, trycost);
        }
        else
        {
            MessageBox.Show("The cost you have entered is invalid. Please ensure the cost is above 0, and is an actual number", "Invalid Input at Cost", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
    

    …

    public void AddClientComputer(int CustomerID, string Action, double Cost)
        {
            try
            {
                comm = new SqlCommand("UspAddClientComputer", conn); // Stored Procedure - see sql file
                comm.Parameters.AddWithValue("@CustomerID", CustomerID);
                comm.Parameters.AddWithValue("@Action", Action);
                comm.Parameters.Add(new SqlParameter("@Cost",Cost));
                comm.CommandType = CommandType.StoredProcedure;
                comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
    

    ..Here comes the solution part. I replaced the Decimal(5,2) with smallmoney.

    CREATE TABLE ClientComputers
                (ClientComputerID int Identity(1,1) primary key clustered
                ,CustomerID int
                ,Action varchar(7) check (Action = 'Upgrade' OR Action = 'Store')
                ,Cost smallmoney check (Cost > 0)
                ,Constraint FKCustomerComputer FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID));
    Go
    
    ---------STORED PROCEDURES
    --ADD CLIENT COMPUTER
    CREATE PROCEDURE uspAddClientComputer @CustomerID int, @Action varchar(7), @Cost smallmoney
    AS
    BEGIN TRY
        BEGIN TRANSACTION TrnAddClientComputer;
         INSERT INTO [TCTdb].[dbo].[ClientComputers]
               ([CustomerID]
               ,[Action]
               ,[Cost])
         VALUES
               (@CustomerID
               ,@Action
               ,@Cost)
        COMMIT TRANSACTION TrnAddClientComputer;
    END TRY
    BEGIN CATCH
        ROLLBACK TRANSACTION TrnAddClientComputer;
    
        DECLARE @ErrorMessage NVARCHAR(4000);
        DECLARE @ErrorSeverity INT;
        DECLARE @ErrorState INT;
    
        SELECT 
            @ErrorMessage = ERROR_MESSAGE(),
            @ErrorSeverity = ERROR_SEVERITY(),
            @ErrorState = ERROR_STATE();
    
        RAISERROR (@ErrorMessage,
                   @ErrorSeverity,
                   @ErrorState
                   );
    END CATCH
    GO
    

    Thanks to everyone who has tried. This answered my question. Right Idea, WRONG data type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having one heck of a hard time trying to figure this out.
I am having a hard time trying to figure out if the following design
I've been having a hard time trying to understand PyPy's translation. It looks like
I'm having a hard time trying to get my team comfortable with interface based
I'm still having a hard time not wanting to use Tables to do my
I'm having a hard time figuring out how to architect the final piece of
I'm having a hard time figuring out how Firefox and Chrome determining what fields
I'm having a hard time trying to record something other than linear PCM on
I have now used way too long time, trying to figure out a problem,
Not the biggest whiz on math here... having a hard time figuring this out.

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.