Solved

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

Posted on 2006-07-20
4
645 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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