Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Undo formatting

Reply
Thread Tools

Undo formatting

 
 
tshad
Guest
Posts: n/a
 
      04-06-2005
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


 
Reply With Quote
 
 
 
 
James Steele
Guest
Posts: n/a
 
      04-06-2005
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!

 
Reply With Quote
 
 
 
 
Dave Fancher
Guest
Posts: n/a
 
      04-07-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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!
>



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      04-13-2005
"James Steele" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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!
>



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      04-13-2005
"tshad" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> "James Steele" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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!
>>

>
>



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      04-13-2005
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]


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to "undo" a database delete Morgan Bachu ASP .Net 13 08-31-2005 03:08 AM
How to open a "closed TAB" ( is there an UNDO command?) bd Firefox 5 01-05-2005 03:43 AM
How do I undo WebRequest.RegisterPrefix john farrow ASP .Net 0 04-13-2004 09:21 PM
Need formatting options menu for formatting hard drive Mark T. Computer Support 3 11-24-2003 11:50 PM
Undo in TextBox Marco Liedekerken ASP .Net 0 06-26-2003 10:03 AM



Advertisments