how to convert textbox input to double type in c#

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
mmalik15Asked:
Who is Participating?
 
momi_sabagConnect With a Mentor Commented:
have you tried letting sql server to do the conversion?
insert into ....
 values (.., cast(your_string as float), ...
0
 
BinuthCommented:
wht actually you entered in txtHours.Text ?
0
 
Blackninja2007Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mmalik15Author Commented:
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
 
Cedric_DCommented:
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
 
Cedric_DConnect With a Mentor Commented:
make sure dot '.' not replaced to comma ','.
0
 
mmalik15Author Commented:
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
All Courses

From novice to tech pro — start learning today.