Click here to Skip to main content
15,947,389 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to fix this error??
"Failed to convert parameter value from a Double to a DateTime."

This is my Code

//setting procedure properties
procedureName = "Add_tbloutboundInvoiceDetails";
cmdType = CommandType.StoredProcedure;

parameters.Add(new SqlParameter("@InvoiceNo", SqlDbType.Int));//0
parameters.Add(new SqlParameter("@H", SqlDbType.NVarChar, 50));//1
parameters.Add(new SqlParameter("@value", SqlDbType.Decimal));//2
parameters.Add(new SqlParameter("@ExchangeRate", SqlDbType.Decimal));//3
parameters.Add(new SqlParameter("@HAWBamount", SqlDbType.Decimal));//4
parameters.Add(new SqlParameter("@HAWBAmountLKR", SqlDbType.Decimal));//5
parameters.Add(new SqlParameter("@Discountamount ", SqlDbType.Decimal));//6
parameters.Add(new SqlParameter("@DisType", SqlDbType.Int));//7
parameters.Add(new SqlParameter("@CurrencyType", SqlDbType.VarChar,(10)));//8
parameters.Add(new SqlParameter("@InvoiceDatetime",SqlDbType.DateTime));//9
parameters.Add(new SqlParameter("@fuel",SqlDbType.Decimal));//10
parameters.Add(new SqlParameter("@Invoicecode", SqlDbType.NVarChar, 30));//11
parameters.Add(new SqlParameter("@InvoiceTotal", SqlDbType.Decimal));//12
parameters.Add(new SqlParameter("@compCode", SqlDbType.NVarChar, 30));//13
parameters.Add(new SqlParameter("@CreateUserID", SqlDbType.NVarChar, 30));//14
parameters.Add(new SqlParameter("@Status", SqlDbType.Int));//15

//Inserting data through stored procedure
parameters[0].Value = 0;//0
parameters[1].Value = DropDownh.SelectedValue.ToString().Replace("'", "''");//1
parameters[2].Value = Double.Parse(txtvalue.Text.Trim().Replace("'", "''"));//2
parameters[3].Value = Double.Parse(txtexchage.Text.Trim().Replace("'", "''"));//3
parameters[4].Value = Double.Parse(txtUSD.Text.Trim().Replace("'", "''"));//4
parameters[5].Value = Double.Parse(txtLKR.Text.Trim().Replace("'", "''"));//5
parameters[6].Value = Double.Parse(txtfuel0.Text.Trim().Replace("'", "''"));//6
parameters[7].Value = DropDiz.SelectedValue.ToString().Replace("'", "''");//7
parameters[8].Value = "USD";//8
DateTime DateTime1 = DateTime.Now;
parameters[9].Value = DateTime1.ToString();//DateStampLabel.Text.Trim().Replace("'", "''"); //9
parameters[10].Value = "0.00";//10
parameters[11].Value = "N/A";//11
parameters[12].Value = "0.00";//12
parameters[13].Value = LblcompCode.Text.Trim().Replace("'", "''");//13
parameters[14].Value = LbluserName.Text.Trim().Replace("'", "''");//14
parameters[15].Value = 0;//15

Boolean status1 = Common.executeProcedure(procedureName, cmdType, parameters);

[edit]indexation changed to improve readability[/edit]
Updated 28-Jun-13 7:14am
Member 9581488 27-Jun-13 16:25pm    
Post your stored procedure as well. Are you trying to insert any value which is of datatype double and you pass it as DateTime?

First off, use AddWithValue - it makes it a lot clearer:
parameters.AddWithValue("@InvoiceNo", 0);
parameters.AddWithValue("@H",DropDownh.SelectedValue.ToString().Replace("'", "''"));
parameters.AddWithValue("@value", Double.Parse(txtvalue.Text.Trim()));
Then, don't convert DateTime to string:
parameters.AddWithValue("@InvoiceDatetime", DateTime.Now);
And assuming your database columns are right, it should all work ok.
Share this answer
[no name] 27-Jun-13 10:31am    
My data table,
@InvoiceNo int,
@H varchar(50),
@value Numeric ( 18,2),
@ExchangeRate Numeric ( 18,2),
@HAWBamount Numeric ( 18,2),
@HAWBAmountLKR Numeric ( 18,2),
@Discountamount Numeric ( 18,2),
@DisType int,
@CurrencyType varchar(10),
@InvoiceDatetime datetime,
@fuel Numeric ( 18,2),
@Invoicecode varchar(50),
@InvoiceTotal Numeric ( 18,2),
@compCode varchar(30),
@CreateUserID varchar(20),
OriginalGriff 27-Jun-13 10:53am    
If you convert to AddWithValue do you still get an error?
[no name] 27-Jun-13 11:20am    
I think there is null coming somehow in your double variable ,if it so then it will not convert into datetime. It is my assumption only
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900