Solved

how to convert textbox input to double type in c#

Posted on 2008-10-22
7
2,843 Views
Last Modified: 2012-05-05
Hi All,
I have a datatype float in sql server for the field  Hours which i need to get from the client side. I get the value in the textbox like this
double  Hours = Convert.ToDouble(txtHours.Text);
but i am getting an error ' string in  not correct format. I need to get this value in double and then store it in sql server table. My Question is how can we convert this input into double type and then convert this double value back to float in the database.
I am using vs 2005 and sql server 2005
thanks
0
Comment
Question by:mmalik15
7 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 250 total points
ID: 22775685
have you tried letting sql server to do the conversion?
insert into ....
 values (.., cast(your_string as float), ...
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22775743
wht actually you entered in txtHours.Text ?
0
 
LVL 3

Expert Comment

by:Blackninja2007
ID: 22775753
double hours;

if (txtHours.Text.trim() != "")
{
hours = convert.todouble(txtHours.Text.trim());
}
else
{
hours = 0;
}

this works on my machine just make sure that the text box only contains numeric values or place in a try catch.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:mmalik15
ID: 22776924
Hi Momi,
i have the following sql in my sp
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[Ex_InsertGradAcctHours]
@EmployeeID int,
@SubjectID int,
@DateFrom DateTime,
@DateTo      DateTime,
@Hours      varchar(100)
as

insert into dbo.tblGradAcctTrainingHours (EmployeeID,SubjectID,DateFrom,DateTo,NoOfHours)
values(@EmployeeID,@SubjectID,@DateFrom,@DateTo,cast(@Hours as float))

and after passing on the hours value 10.25 i get an error cannnot convert varchar to float
0
 
LVL 7

Expert Comment

by:Cedric_D
ID: 22777611
Obviously, @Hours contains something incorrect just before INSERT statement.

Pls debug this (add SELECT @Hours or PRINT @Hours).

Or show us, where how @Hours filled.
0
 
LVL 7

Assisted Solution

by:Cedric_D
Cedric_D earned 250 total points
ID: 22777627
make sure dot '.' not replaced to comma ','.
0
 

Author Comment

by:mmalik15
ID: 22777814
I am sending a string value for @Hours Parameter which is e.g. "9.25" but i get the error when i run it
Error converting data type varchar to float.

This is how i pass the value to the sp

 DbParameter param5 = _factory.CreateParameter();
            param5.ParameterName = "@Hours";
            param5.Value = hours;
            cmd.Parameters.Add(param5);

and at run time i have checked the value for the variable  hours is "9.25" and i get the error
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

862 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

23 Experts available now in Live!

Get 1:1 Help Now