Solved

declare decimal in C#

Posted on 2009-07-14
21
1,414 Views
Last Modified: 2012-05-07
i have something like 100.50

i declare it using
public static decimal calculated_sub_total = 0.00M;

i still get 'input string is not in the correct format'.
0
Comment
Question by:doramail05
  • 9
  • 9
  • 3
21 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24856031
What is the code where you are passing the input string to the decimal variable?  What is the piece of code you are getting the error from?
0
 
LVL 1

Author Comment

by:doramail05
ID: 24856114
when the calculated sub_total total ups and it is 100.00,
but when the calculated sub_total total ups and it is 100.50 and insert into table , it says 'input string was not in correct format'.

 SQL Server sub_total is in numeric (18,2)
public static decimal calculated_sub_total = 0.00M;
 

SqlCommand sqlcmd_insert_into_oneuSO = sqlsourceconnextract.CreateCommand();

                                        sqlcmd_insert_into_oneuSO.CommandText = "USE " + txtSourceDatabase.Text + "; INSERT INTO dbo.ONEU_Sales_Order (sale_id, sale_order_no, sale_order_invoice, sale_datetime, sale_status, sub_total, discount_percent, discount_amount, tax_percent, tax_amount, service_charge_percent, service_charge_amount, grand_total, line_type, chg_type, whse_code, upload_status) " +

                                    " VALUES ('" + generated_saleid + "', NULL, NULL, '" + Convert.ToDateTime(dsselectSOLfields.Tables[0].Rows[m]["sol_date_stamp"])

                                    + "', '11', " + calculated_sub_total + ", 0, " + dsselectSOLfields.Tables[0].Rows[m]["sol_line_amount"].ToString() + ", 0, 0, 0, 0, 0, '', '', '', 0)";

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24856144
Maybe it is precision issue, try like this:
public static decimal calculated_sub_total = 0.00M;

 

SqlCommand sqlcmd_insert_into_oneuSO = sqlsourceconnextract.CreateCommand();

                                        sqlcmd_insert_into_oneuSO.CommandText = "USE " + txtSourceDatabase.Text + "; INSERT INTO dbo.ONEU_Sales_Order (sale_id, sale_order_no, sale_order_invoice, sale_datetime, sale_status, sub_total, discount_percent, discount_amount, tax_percent, tax_amount, service_charge_percent, service_charge_amount, grand_total, line_type, chg_type, whse_code, upload_status) " +

                                    " VALUES ('" + generated_saleid + "', NULL, NULL, '" + Convert.ToDateTime(dsselectSOLfields.Tables[0].Rows[m]["sol_date_stamp"])

                                    + "', '11', " + (double)calculated_sub_total + ", 0, " + dsselectSOLfields.Tables[0].Rows[m]["sol_line_amount"].ToString() + ", 0, 0, 0, 0, 0, '', '', '', 0)";

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24856146
try using this:

dsselectSOLfields.Tables[0].Rows[m]["sol_date_stamp"].ToString()


rather than

dsselectSOLfields.Tables[0].Rows[m]["sol_date_stamp"]
0
 
LVL 1

Author Comment

by:doramail05
ID: 24856299
the (double)calculated_subtotal  still aint work
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24856817
ok than do one thing. comment your SQLCommand code right now, execute below code which will print Insert statement. show it to me and also show me your Create Table script for table dbo.ONEU_Sales_Order.


string strSQL;

        strSQL = "USE " + txtSourceDatabase.Text + "; INSERT INTO dbo.ONEU_Sales_Order (sale_id, sale_order_no, sale_order_invoice,";

        strSQL=strSQL + " sale_datetime, sale_status, sub_total, discount_percent, discount_amount, tax_percent, tax_amount, ";

        strSQL = strSQL + " service_charge_percent, service_charge_amount, grand_total, line_type, chg_type, whse_code, upload_status) ";

        strSQL = strSQL + " VALUES ('" + generated_saleid + "', NULL, NULL, '";

        strSQL = strSQL + Convert.ToDateTime(dsselectSOLfields.Tables[0].Rows[m]["sol_date_stamp"]);

        strSQL = strSQL + " ', '11', " + (double)calculated_sub_total + ", 0, ";

        strSQL=strSQL + dsselectSOLfields.Tables[0].Rows[m]["sol_line_amount"].ToString() + ", 0, 0, 0, 0, 0, '', '', '', 0)";

        Response.Write(strSQL);

Open in new window

0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24856824
one wild guess, this may work, try this one too:



public static decimal calculated_sub_total = 0.00M;

 

SqlCommand sqlcmd_insert_into_oneuSO = sqlsourceconnextract.CreateCommand();

                                        sqlcmd_insert_into_oneuSO.CommandText = "USE " + txtSourceDatabase.Text + "; INSERT INTO dbo.ONEU_Sales_Order (sale_id, sale_order_no, sale_order_invoice, sale_datetime, sale_status, sub_total, discount_percent, discount_amount, tax_percent, tax_amount, service_charge_percent, service_charge_amount, grand_total, line_type, chg_type, whse_code, upload_status) " +

                                    " VALUES ('" + generated_saleid + "', NULL, NULL, '" + Convert.ToDateTime(dsselectSOLfields.Tables[0].Rows[m]["sol_date_stamp"])

                                    + "', '11', " + Convert.ToString(calculated_sub_total) + ", 0, " + dsselectSOLfields.Tables[0].Rows[m]["sol_line_amount"].ToString() + ", 0, 0, 0, 0, 0, '', '', '', 0)";

Open in new window

0
 
LVL 1

Author Comment

by:doramail05
ID: 24857092
cannot work

i think decimal cannot accept string

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24857096
show me insert string and create table script
0
 
LVL 1

Author Comment

by:doramail05
ID: 24857280
the sub total is changed to decimal in design mode.

the 'response' keyword does not apply, i copy and paste from the html visualiser.

i tried the date time convert.toDatetime(... .ToString()) but got the same error 'input format.. ";

notice that the previous record did insert into table , like 0.00, once it has 0.50 ,  that does not insert
USE [RetailDB]

GO

/****** Object:  Table [dbo].[ONEU_Sales_Order]    Script Date: 07/14/2009 09:45:48 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_PADDING ON

GO

CREATE TABLE [dbo].[ONEU_Sales_Order](

	[sale_id] [nvarchar](50) NOT NULL,

	[sale_order_no] [varchar](max) NULL,

	[sale_order_invoice] [varchar](max) NULL,

	[sale_datetime] [datetime] NOT NULL,

	[sale_status] [nvarchar](max) NOT NULL,

	[sub_total] [money] NOT NULL,

	[discount_percent] [float] NULL,

	[discount_amount] [money] NULL,

	[tax_percent] [float] NULL,

	[tax_amount] [money] NULL,

	[service_charge_percent] [float] NULL,

	[service_charge_amount] [money] NULL,

	[grand_total] [money] NOT NULL,

	[line_type] [varchar](max) NULL,

	[chg_type] [varchar](max) NULL,

	[whse_code] [varchar](max) NULL,

	[upload_status] [int] NULL,

 CONSTRAINT [PK_1U_Sales_Order] PRIMARY KEY CLUSTERED 

(

	[sale_id] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]
 

GO

SET ANSI_PADDING OFF
 
 

USE RetailDB; INSERT INTO dbo.ONEU_Sales_Order (sale_id, sale_order_no, sale_order_invoice, sale_datetime, sale_status, sub_total, discount_percent, discount_amount, tax_percent, tax_amount, service_charge_percent, service_charge_amount, grand_total, line_type, chg_type, whse_code, upload_status) VALUES ('SO53000000000002', NULL, NULL, '

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

Expert Comment

by:RiteshShah
ID: 24857617
I can't see complete INSERT statement,
0
 
LVL 1

Author Comment

by:doramail05
ID: 24857659
i write from msgbox, hope that declare

one thing is that i have 'money' in discount_amount, but i put it as
dsselectSOLfields.Tables[0].Rows[m]["sol_line_amount"].ToString()
it write to the field as 5530.0000 .

USE RetailDB; INSERT INTO dbo.ONEU_Sales_Order(sale_id, sale_order_no, sale_order_invoice, sale_datetime, sale_status, sub_total, discount_percent, discount amount, tax_percent, tax_amount, service_charge_amount, grand_total, line_type, chg_type, whse_code, upload_status) VALUES ('SO5300000000002', NULL, NULL, '10/22/2008 :12:00:00 AM', '11', 649, 0, 649, 0, 0 , 0, 0, 0 , ' ', ' ', ' ', 0)
0
 
LVL 1

Author Comment

by:doramail05
ID: 24857680
previously it wrote 11 rows,

now below only wrote 1 row.
 SqlCommand sqlcmd_insert_into_oneuSO = sqlsourceconnextract.CreateCommand();

                                                    sqlcmd_insert_into_oneuSO.CommandText = "USE " + txtSourceDatabase.Text + "; INSERT INTO dbo.ONEU_Sales_Order (sale_id, sale_order_no, sale_order_invoice, sale_datetime, sale_status, sub_total, discount_percent, discount_amount, tax_percent, tax_amount, service_charge_percent, service_charge_amount, grand_total, line_type, chg_type, whse_code, upload_status) " +

                                                " VALUES ('" + generated_saleid + "', NULL, NULL, '" + Convert.ToDateTime(dsselectSOLfields.Tables[0].Rows[m]["sol_date_stamp"])

                                                + "', '11', " + Convert.ToString(calculated_sub_total) + ", 0, " + dsselectSOLfields.Tables[0].Rows[m]["sol_line_amount"].ToString() + ", 0, 0, 0, 0, 0, '', '', '', 0)";

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24857720
if you see your select statement given above, I found three mistakes,

1.) "_" missing in Discount_amount field
2.) one more field in VALUES clause
3.) wrong date format, ":" before 12 is wrong.

if you correct these three mistakes, you will be able to insert records as I did just now.


have a look



INSERT INTO dbo.ONEU_Sales_Order(
 

sale_id, 

sale_order_no, 

sale_order_invoice, 

sale_datetime, 

sale_status, 

sub_total, 

discount_percent, 

discount_amount, --you were MISSING _ sign here

tax_percent, 

tax_amount, 

service_charge_amount, 

grand_total, 

line_type, 

chg_type, 

whse_code, 

upload_status
 

) VALUES (
 

'SO5300000000002', 

NULL, 

NULL, 

'10/22/2008 12:00:00 AM', 

'11', 

649, 

0, 

649, 

0, 

0 , 

0, 

0, 

0 , 

' ', 

' ', 

0

) 

Open in new window

0
 
LVL 1

Author Comment

by:doramail05
ID: 24857794
3.) wrong date format, ":" before 12 is wrong.

10/22/2008 is the default date format in sql server i think, that's US time,

how should the date format be and the code's one?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24857810
The colon itself is wrong.

'10/22/2008 :12:00:00 AM'

Should be:

'10/22/2008 12:00:00 AM'
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24857815
you can see proper date in my insert script I have provided, that is completely working script, I have inserted record.
0
 
LVL 1

Author Comment

by:doramail05
ID: 24858292
i prepost the comment earlier and wondering it didnt get thru, i post again for my findings

i firstly changed the data type of datetime to varchar(max), then pass in NULL in code
still it has the same error 'input string was not in correct format'

secondly, i replaces all money and float to varchar(max), then pass in same all NULL, still getting

then finally, i left sale_id only and all varchar(max) NULL.. still getting error and only write in 1 line into the table..  
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24858338
can you run query in SSMS directly?
0
 
LVL 1

Author Comment

by:doramail05
ID: 24858927
i think it will work, in another 12 hours i will be in the office
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24859377
if it is working from SSMS than it has no reason to not work from .NET. I tried the query I gave you and it is working with the table you gave above.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now