Solved

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

Posted on 2006-07-20
4
617 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

11 Experts available now in Live!

Get 1:1 Help Now