473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Undo formatting

I have a number with commas that a user may put into our textbox.

The problem is I can't use this in my Sql Statement without an error:

For example, if my variable is 12,000.00 or $12,000.00, I can't use it in my
where clause of my Sql Statement. I need to convert it to 12000.000 first.

Is there a way to easily do this in asp.net?

Thanks,

Tom
Nov 19 '05 #1
5 1071
Hi tshad,

Sure there is. You can just use string.Replace(oldchar char, newchar
char).

http://msdn.microsoft.com/library/de...placetopic.asp

You could also use regular expressions if you wanted to.

Good luck!

Nov 19 '05 #2
You could also use one of the Decimal.Parse overloads.

In this case, I would typically use something like this:

[C#]
// Connection initialization, etc...
SqlCommand cmd = new SqlCommand("SELECT * FROM [Table1] WHERE Price < @amt",
conn);
cmd.Parameters.Add(new SqlParameter("@amt", SqlDbType.Decimal));

try
{
cmd.Parameters["@amt"].Value = Decimal.Parse(amt,
System.Globalization.NumberStyles.Currency);
}
catch (Exception ex)
{
// An error occurred, so handle it
return;
}

conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
// process reader
}
conn.Close();
// clean up
--
HTH
----------------
Dave Fancher
http://davefancher.blogspot.com

"James Steele" <as***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi tshad,

Sure there is. You can just use string.Replace(oldchar char, newchar
char).

http://msdn.microsoft.com/library/de...placetopic.asp

You could also use regular expressions if you wanted to.

Good luck!

Nov 19 '05 #3
"James Steele" <as***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi tshad,

Sure there is. You can just use string.Replace(oldchar char, newchar
char).

http://msdn.microsoft.com/library/de...placetopic.asp

You could also use regular expressions if you wanted to.

I tried that and I was getting an error that said it was expecting an
expression.

trace.warn("WagesMin.Text = " &
WagesMin.Text.toString().replace(/\$|\,/g,""))

Here I am just trying to take out the commas and $, which works in
javascript (maybe not in vb.net).

I also tried:

trace.warn("WagesMin.Text = " & WagesMin.Text.replace(/\$|\,/g,""))

And that doesn't work either.

I just want to move this into my parameter which gives me an iput error if I
try to do this:

objCmd.parameters.add("@WagesMin",SqlDbType.Money) .value = WagesMin.Text

and the variable has anything but digits and a decimal.

Thanks,

Tom Good luck!

Nov 19 '05 #4
"tshad" <ts**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
"James Steele" <as***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi tshad,

Sure there is. You can just use string.Replace(oldchar char, newchar
char).

http://msdn.microsoft.com/library/de...placetopic.asp

You could also use regular expressions if you wanted to.

I tried that and I was getting an error that said it was expecting an
expression.

trace.warn("WagesMin.Text = " &
WagesMin.Text.toString().replace(/\$|\,/g,""))

Here I am just trying to take out the commas and $, which works in
javascript (maybe not in vb.net).

I also tried:

trace.warn("WagesMin.Text = " & WagesMin.Text.replace(/\$|\,/g,""))

And that doesn't work either.

I just want to move this into my parameter which gives me an iput error if
I try to do this:

objCmd.parameters.add("@WagesMin",SqlDbType.Money) .value = WagesMin.Text

and the variable has anything but digits and a decimal.


I also tried:

dim temp as string
temp = Regex.replace(WagesMin.Text,"/\$|\,/g","")
trace.warn("temp = " & temp)

Where WagesMin.Text = $00.0. But temp is still showing $00.0.

This also doesn't work - but doesn't give me an error.
trace.warn("WagesMin.Text = " & WagesMin.Text.replace("/\$|\,/g",""))

I think my other problem was that I didn't have quotes around the
expression.

Tom
Thanks,

Tom
Good luck!


Nov 19 '05 #5
I found out how to get it to work.

To strip out the $ and commas from a string or textbox that has formatted
currency. For example: $1,000,000.00.

Regex.Replace(string,"\$|\,","")

In my case, I wanted to add a parameter that gets sent to Sql and use a
textbox that has formatted currency information. I didn't want to change
the original, so I did the following where WagesMin.Text would be something
like $1,500.50:

objCmd.parameters.add("@WagesMin",SqlDbType.Money) .value =
Regex.Replace(WagesMin.Text,"\$|\,","")

Worked like a charm.

Tom
[snip]
Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: D Witherspoon | last post by:
Sometimes if I build the app I will get intellisense, undo, and code completion (automatic completion of if stataments etc), but after a few lines of code this stops. I lose any code completion...
2
by: Lyn | last post by:
Hi, I have a text control on a form which is bound to table field StartDate which is in Date format. When updating the table record via the form, any data entered into the StartDate control is...
5
by: Lyn | last post by:
Hi, In the BeforeUpdate event of a textbox control I have the following simplified code:- Private Sub StartDate_BeforeUpdate(Cancel As Integer) MsgBox "StartDate Error -- Please Check and...
0
by: Wiktor Zychla | last post by:
I am still trying to get the Undo function work on Internet Explorer editor hosted as activex in my c# application. Some time ago I've found a note at msdn that says "undo is not supported". I...
3
by: babylon | last post by:
any facilities in csharp that can help me implmenting undo/redo in my application? thx
3
by: GoogleEyeJoe | last post by:
Dear ladies and gents, I'm trying to determine whether the .NET Framework implements a means of transactional processing without the need for a database. Essentially, I'd like to enlist...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, Does vb2005 have a built-in UnDo feature / object for applications so that I can undo actions like other windows apps? Or do I have to write my own UnDo routine? If vb2005 does have a...
0
by: wizard of oz | last post by:
Hi all, I'm extending an Abstract Styled Document associated with a JTextPanel to implement a syntax highlighting editor. This is all working just fine - except for undo / redo. The problem...
1
by: Jeremy | last post by:
I'm working on an application that does some DOM manipulation. I want to add a mechanism for "Undo" so that the user can revert to the previous state after performing a mistaken action. Simple...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.