1

I tried to Replace sign "," with "." on UPDATE parameter but I'm not able to solve it. This parameter updates numeric type from textBoxes inSQL DB.

I thought that it can me done something like this, but parameter does not contain definiton Replace. is there any way to do that?

 prikaz.Parameters.AddWithValue("@poc_kli", poc_kli.Text).Replace(',', '.');
1
  • 1
    you have to do it on textbox prikaz.Parameters.AddWithValue("@poc_kli", poc_kli.Text.Replace(',', '.')); Commented Jul 29, 2013 at 11:13

2 Answers 2

4

Parse the value first; then use typed data in the parameter. For example:

decimal val = decimal.Parse(poc_kli.Text);
prikaz.Parameters.AddWithValue("poc_kli", val);

Rather than "replace", you should use an appropriate CultureInfo (if it differs from the default culture) when parsing the value. For simplicity I'm assuming that you do not in fact need to specify a culture, but that this was simply an attempt at making SQL Server happy. But for example:

CultureInfo culture = CultureInfo.InvariantCulture;
// ^^^ or GetCulture, CurrentCulture, etc
decimal val = decimal.Parse(poc_kli.Text, culture);

Even better would be to completely separate your data access code from your UI code:

decimal val = decimal.Parse(poc_kli.Text);
SomeDatabaseMethod(val, ...other args);

...

public void SomeDatabaseMethod(decimal pocKli, ... other parameters ...)
{
    ...
    prikaz.Parameters.AddWithValue("poc_kli", pocKli);
    ...
}

The fact that you have one line of code that touches both the UI and the database (which are completely unrelated, or should be) should concern you.

Sign up to request clarification or add additional context in comments.

Comments

2

You're replacing at the wrong place in your code. Try:

prikaz.Parameters.AddWithValue("@poc_kli", poc_kli.Text.Replace(',', '.'));

2 Comments

I would wager that the , vs . is an attempt to format it to make SQL Server happy with culture differences; as such, it is almost certainly entirely the wrong thing to do
I agree; I've already upvoted your comment as it's more useful as a general teaching point than mine (which only addresses the immediate technical issue).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.