Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-07-20
4
Medium Priority
?
657 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 2000 total points
ID: 17145227
what about this:
dec NoOfHours = decimal.Parse(txtNoOfHours.Text, NumberStyles.Currency);
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

670 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