10 Replies - 3987 Views - Last Post: 13 April 2012 - 03:53 PM Rate Topic: -----

#1 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Input String Is Not In A Correct Format - Help!

Posted 12 April 2012 - 02:35 PM

Hey Everyone!

So my issue is I am creating textboxes in the code behind, & I am trying to insert the values from the textbox to a SQL database. If the textboxes are blank (which is fine) the insert works and if the textboxes are populated with figures then the insert works as well. The textboxes are pre-populated based on their figures in the SQL table, so some are populated whereas some aren't. I only get the error when a text box is pre-populated and I erase/delete the value by making it blank and then try to perform the 'Insert'. The error message is 'Input String Is Not In A Correct Format', here is my code:

    void tb2_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)sender;
        int item = Convert.ToInt32(tb.ID.Substring(tb.ID.LastIndexOf("_") + 1).Replace("N", "-"));
        int store = (int)Session["PItem"];

        if (tb.Text.Length > 0)
        {
            Data.SetPrice(store, item, Convert.ToDouble(tb.Text.Replace(".", ",")));
        }
        else
        {
            string str = ((TableCell)((TableRow)tb.Parent.Parent).Cells[((TableRow)tb.Parent.Parent).Cells.GetCellIndex((TableCell)tb.Parent) - 1]).Text.Replace("Kd", "").Trim();
            Data.SetPrice(store, item, Convert.ToDouble(str));
        }
    }



Any help would be appreciated.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Input String Is Not In A Correct Format - Help!

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Input String Is Not In A Correct Format - Help!

Posted 12 April 2012 - 02:45 PM

put a breakpoint on line 14
Then look at the actual value of str
You should find there are non-numerics in it. you can't convert "yogibear" to a double... or "5kb"... or "5 " (note the space)


What does this error mean?

sorry.. You didn't say which line the error was on. I didn't see this one right away

Data.SetPrice(store, item, Convert.ToDouble(tb.Text.Replace(".", ",")));
You can't convert "5,000" to a double. comma is not a numeric.
Was This Post Helpful? 0
  • +
  • -

#3 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Input String Is Not In A Correct Format - Help!

Posted 12 April 2012 - 04:07 PM

Yea the error is on Line 14. I did put a breakpoint on that line but it was almost as though that line wasn't being executed. I think my Visual Studio was acting up as the application would point the error on that line in the browser but the breakpoint wouldn't produce any thing as though the statement was never executed.
Was This Post Helpful? 0
  • +
  • -

#4 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Input String Is Not In A Correct Format - Help!

Posted 13 April 2012 - 08:33 AM

View PosttlhIn`toq, on 12 April 2012 - 02:45 PM, said:

put a breakpoint on line 14
Then look at the actual value of str
You should find there are non-numerics in it. you can't convert "yogibear" to a double... or "5kb"... or "5 " (note the space)


What does this error mean?

sorry.. You didn't say which line the error was on. I didn't see this one right away

Data.SetPrice(store, item, Convert.ToDouble(tb.Text.Replace(".", ",")));
You can't convert "5,000" to a double. comma is not a numeric.


The error is on line 14 and after putting a breakpoint. The debugger suggest that the value of str is 60,00 kd. which is fine but I think it's having a hard time converting the comma into a decimal and erasing the kd.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Input String Is Not In A Correct Format - Help!

Posted 13 April 2012 - 08:50 AM

Right. That's what I said. You can't have commas, letters and other non-numerics in your string that you are trying to convert to a number. YOU need to strip them out first. There is no way to have ConvertTo do your work for you.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,464
  • Joined: 29-May 08

Re: Input String Is Not In A Correct Format - Help!

Posted 13 April 2012 - 10:23 AM

tlhIn`toq: Some non-American cultures do us comma , as the decimal point . . Some us it as the thousands separator 10,000 (Ten Thousand)

I think the OP is using it as the latter.

The OP could try using TryParseExact and provide the format string which matches their formatting.
Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Input String Is Not In A Correct Format - Help!

Posted 13 April 2012 - 10:43 AM

but its not going to work with "{space}kd" in the string.

The OP has to take responsibility for striping the non-numerics and not expect the framework to do it for them.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4480
  • View blog
  • Posts: 7,803
  • Joined: 08-June 10

Re: Input String Is Not In A Correct Format - Help!

Posted 13 April 2012 - 10:54 AM

Well, there are ways. Using double.Parse, and CultureInfos, you can make it behave the way you want.

Usually, the current culture on the machine will be set for the right region, and will include the correct parsing rules. But if you're using a different culture than what's running, you can explicitly tell it which cultural rules to parse with.

For instance, here's using French parsing rules:

string numStr = "3,5";
double num = double.Parse(numStr, new CultureInfo("fr-FR"));
Console.WriteLine(num);



That prints "3.5". CultureInfo contains info on formatting, in and out. So, we parsed it according to French culture, but since I'm running under default culture, when it's output, it uses the decimal instead of the comma. But it parsed the comma correctly.
Was This Post Helpful? 0
  • +
  • -

#9 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Input String Is Not In A Correct Format - Help!

Posted 13 April 2012 - 11:39 AM

Thanks for the responses,

I tried this :

if (tb.Text.Length > 0){

  double numericValue;
  try{
    numericValue=Convert.ToDouble(tb.Text.Replace(".", ","));
  }
  catch(Exception e){
    numericValue=0;
  }
  Data.SetPrice(store, item, numericValue);
}


It allows me to erase the value that is pre-populated in the textbox and go through but it records the value that was previously in the textbox before it was erased when I want it to reference the parent textbox if the value is erased. So it should be if TxtBox 1 = 100,00 and TxtBox 2 = 150,00. When TxtBox 2's value is erased it should go into the db as 100,00. I tried doing this, and I got the same input string error

string str = ((TableCell)((TableRow)tb.Parent.Parent).Cells[((TableRow)tb.Parent.Parent).Cells.GetCellIndex((TableCell)tb.Parent) - 1]).Text.Replace("Kd", "").Trim();
double str2 = double.Parse(str, new CultureInfo("da-Dk"));
Data.SetPrice(store, item, Convert.ToDouble(str2));



I can't use the 'var' keyboard in C# 2.0. In this piece the str value is still 60,00 kd so something definitely is wrong with my Trim code.I am having trouble stripping the currency off. Any ideas?

This post has been edited by staindromeo: 13 April 2012 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4480
  • View blog
  • Posts: 7,803
  • Joined: 08-June 10

Re: Input String Is Not In A Correct Format - Help!

Posted 13 April 2012 - 01:15 PM

You're not understanding us...the comma is a problem. You cannot include the comma in the string you're trying to parse unless you include some CultureInfo that defines the comma as the decimal separator. Your code will replace any "." with a "," so of course Convert will choke on it.

Anyway, you mentioned C# 2.0 so I'll update my previous example.
Was This Post Helpful? 0
  • +
  • -

#11 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Input String Is Not In A Correct Format - Help!

Posted 13 April 2012 - 03:53 PM

Ok now I am confused :helpsmilie:

I am not sure if you had seen this code snippet that I had posted:

string str = ((TableCell)((TableRow)tb.Parent.Parent).Cells[((TableRow)tb.Parent.Parent).Cells.GetCellIndex((TableCell)tb.Parent) - 1]).Text.Replace("Kd", "").Trim();
double str2 = double.Parse(str, new CultureInfo("da-Dk"));
Data.SetPrice(store, item, Convert.ToDouble(str2));



I edited the code so that it would be suited for C# 2.0 and the CultureInfo should recognize the commas as decimal values but when I use the debugger it still shows as 60,00 kd. Which mean the Text.Replace function above that line isn't working as well. I am aware the comma is the issue and I tried setting it this way

Data.SetPrice(store, item, Convert.ToDouble(tb.Text.Replace(",", ".")));


But that still doesn't work. I know the issue is the comma and the text that needs to be stripped off, I am just lost as to how to get it to that point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1