Error converting data type nvarchar to numeric

hi Experts,

trying to insert a textbox value which is formatted 00.00 into a decimal column in sql - getting the title error

the textbox is as shown below:

<asp:TextBox CssClass="textbox" ID="TxtDurationDecimal" onblur="if(this.value=='')this.value=this.defaultValue;" value="00.00"
                                                    onfocus="if(this.value==this.defaultValue)this.value='';" runat="server" ReadOnly="true" Width="70"></asp:TextBox>

the sql is in c# code behind with the following parameter :

cmd.Parameters.AddWithValue("@otDurationDecimal", TxtDurationDecimal.Text);
forstersAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SStoryCommented:
cmd.Parameters.AddWithValue("@otDurationDecimal", Convert.ToDecimal(TxtDurationDecimal.Text));
forstersAuthor Commented:
I now get  'Input string was not in a correct format'
forstersAuthor Commented:
The destination column is Decimal(10,2) if that helps...
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Prakash SamariyaIT ProfessionalCommented:
You could try below:
cmd.Parameters.AddWithValue("@otDurationDecimal", Math.Round(Convert.ToDecimal(TxtDurationDecimal.Text), 2));
SStoryCommented:
Does the field have the possibility of spaces, etc?
[space]10.25
or 10.25[space]
will be considered a non number.
If there is the possibility of user input, then it must be filtered in all sorts of ways.  Can they enter ABC? or " 10.25 " with spaces? If so at least Trim it first.

Convert.ToDecimal(TxtDurationDecimal.Text.Trim)

If not please debug and check the value on that line in txtDurationDecimal. in a way that will show you any hidden chars.
forstersAuthor Commented:
Thanks both,

I tried cmd.Parameters.AddWithValue("@otDurationDecimal", Math.Round(Convert.ToDecimal(TxtDurationDecimal.Text), 2));
but still get  'Input string was not in a correct format'
The value in TxtDurationDecimal is not user input it's generated by a JavaScript function - the below should demonstrate (duration is a period of time in the format hh:mm) :
var hoursdecimal = "";
                var hoursrounded = "";
                                           
                if (duration.split(':')[1] >= '00' && duration.split(':')[1] <= '14') {
                    hoursdecimal = duration.split(':')[0] + ".00";
                    hoursrounded = duration.split(':')[0] + ":00";
                }

                if (duration.split(':')[1] >= '15' && duration.split(':')[1] <= '29') {
                    hoursdecimal = duration.split(':')[0] + ".25";
                    hoursrounded = duration.split(':')[0] + ":15";
                }

                if (duration.split(':')[1] >= '30' && duration.split(':')[1] <= '44') {
                    hoursdecimal = duration.split(':')[0] + ".50";
                    hoursrounded = duration.split(':')[0] + ":30";
                }

                if (duration.split(':')[1] >= '45' && duration.split(':')[1] <= '59') {
                    hoursdecimal = duration.split(':')[0] + ".75";
                    hoursrounded = duration.split(':')[0] + ":45";
                }

                document.getElementById('<%=TxtDuration.ClientID%>').value = duration;
                document.getElementById('<%=TxtDurationRounded.ClientID%>').value = hoursrounded;
                document.getElementById('<%=TxtDurationDecimal.ClientID%>').value = hoursdecimal;
Prakash SamariyaIT ProfessionalCommented:
forsters,

Could you check whether "TxtDurationDecimal" is getting value of it is empty!?

Or you can try below line:
 cmd.Parameters.Add("@otDurationDecimal", SqlDbType.Decimal).Value = Math.Round(Convert.ToDecimal(TxtDurationDecimal.Text), 2);
SStoryCommented:
My suggestion would be to output the actual value from that function to see if it IS what you expect. Strange things can happen. Always debug by checking the output instead of doggedly assuming all is right and it "should work."  I've thought that many times and found by outputting that it wasn't doing what I expected.  You could do a Decimal.Tryparse(value) and see if it thinks it is a decimal and if not, output and error message, showing the value with "" around it so if it has spaces or something is wrong you can see them.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.