Solved

ASP.NET 2.0/c#: Passing decimal as parameter in parameterized sql query using MySql

Posted on 2006-07-20
4
625 Views
Last Modified: 2012-05-05
Hi Experts!

I am working on a web application in ASP.NET 2.0.

I have a webform, on which users can input data in textboxes, which is then send to the MySql database by a parameterized query.

The problem is, if I use decimals, then somehow the query just get mangled and doesn't execute.

This is probably because the decimal has a comma seperator, which wrecks the insert query.

Example:

dec NoOfHours = Convert.ToDecimal(txtNoOfHours.Text);
MySqlCommand updCommand = new MySqlCommand();
            updCommand.Connection = myConnection;
updCommand.CommandText = "INSERT INTO items (NoOfHours) VALUES (?NoOfHours)"
            updCommand.Parameters.Add(new MySqlParameter("?NoOfHours", NoOfHours));
myConnection.Open()
updCommand.ExecuteNonQuery();    //it breaks here, giving me a wrong sql syntax error.



Anyone able to help me out here?

Thanks!

0
Comment
Question by:DarkXiphoid
4 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 17144998
Have you tried removing the commas?
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17145002
You need to escape the string,

Normally you would use the mysql_real_escape_string() in the C API,

But if it is a numerical field, and you know all you are getting is , then you can just do a replace in the string replacing a , with  \,

ie

$123,334.33

would go to

$123\,334.33

Which will remove your problem
0
 
LVL 11

Accepted Solution

by:
vo1d earned 500 total points
ID: 17145227
what about this:
dec NoOfHours = decimal.Parse(txtNoOfHours.Text, NumberStyles.Currency);
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 17146359
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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

13 Experts available now in Live!

Get 1:1 Help Now