Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. "Input string was not in a correct format." when updating datatable... [modified]

"Input string was not in a correct format." when updating datatable... [modified]

Scheduled Pinned Locked Moved C#
help
13 Posts 6 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Joshi Rushikesh

    I think you are getting error when trying to convert DateTime values. A DateTime value "{6/1/1998 12:00:00 AM}" has a space and it tries to convert to "{6/1/1998% 12:00:00 AM}" which is not valid. Change to below code to resolve this.

    if (row[column].ToString().Contains(" ") && column.DataType != Type.GetType("System.DateTime") )
    {
    temp = row[column].ToString().Replace(" ", "%");
    row[column] = temp; //getting error in this line...
    }

    Regards Rushi

    S Offline
    S Offline
    siva455
    wrote on last edited by
    #4

    no i don"t have any column which of datetime type and even i tried the above code it"s not working....

    1 Reply Last reply
    0
    • S Subin Mavunkal

      check whether it is a strongly typed data table

      S Offline
      S Offline
      siva455
      wrote on last edited by
      #5

      no it"s not strongly typed datatable.. -- Modified Thursday, May 12, 2011 2:20 AM

      1 Reply Last reply
      0
      • S siva455

        Hi,

        String temp;
        foreach (DataRow row in Table.Rows)
        {
        foreach (DataColumn column in Table.Columns)
        {
        if(row[column].ToString().Contains("%"))
        {
        temp = row[column].ToString().Replace("%"," ");
        row[column] = temp;
        }
        }
        }
        Table.AcceptChanges();
        Table is nothing but a datatable...
        and when im trying to run the below code... im getting error like input string was not in a correct format
        in the below highlighted line of code..
        String tem;
        foreach (DataRow row in Table.Rows)
        {
        foreach (DataColumn column in Table.Columns)
        {
        if (row[column].ToString().Contains(" "))
        {
        tem = row[column].ToString().Replace(" ", "%");
        row[column] = temp; //getting error in this line...
        }
        }
        }
        Table.AcceptChanges();

        Please help me regarding the same..

        modified on Thursday, May 12, 2011 1:14 AM

        S Offline
        S Offline
        Subin Mavunkal
        wrote on last edited by
        #6

        How can you directly assign to a foreach iteration variable ? It will throw an error , right ?

        OriginalGriffO 1 Reply Last reply
        0
        • S siva455

          Hi,

          String temp;
          foreach (DataRow row in Table.Rows)
          {
          foreach (DataColumn column in Table.Columns)
          {
          if(row[column].ToString().Contains("%"))
          {
          temp = row[column].ToString().Replace("%"," ");
          row[column] = temp;
          }
          }
          }
          Table.AcceptChanges();
          Table is nothing but a datatable...
          and when im trying to run the below code... im getting error like input string was not in a correct format
          in the below highlighted line of code..
          String tem;
          foreach (DataRow row in Table.Rows)
          {
          foreach (DataColumn column in Table.Columns)
          {
          if (row[column].ToString().Contains(" "))
          {
          tem = row[column].ToString().Replace(" ", "%");
          row[column] = temp; //getting error in this line...
          }
          }
          }
          Table.AcceptChanges();

          Please help me regarding the same..

          modified on Thursday, May 12, 2011 1:14 AM

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #7

          Are our DataTable columns of type string? Because if they aren't, then data conversion will occur, and "%" will likely be an invalid character in the input string. I assume you are also the author of this: http://www.codeproject.com/Answers/195111/input-string-is-not-in-correct-format-error-when-u.aspx[^] Q&A question, where you give more details.

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          S 1 Reply Last reply
          0
          • S Subin Mavunkal

            How can you directly assign to a foreach iteration variable ? It will throw an error , right ?

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #8

            He isn't: he is modifying the content of a foreach variable - completely fine!

            foreach (item i in myList)
            {
            i.Contents = "New value";
            }

            Will work,

            foreach (item i in myList)
            {
            i = "New value";
            }

            Will not.

            Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Are our DataTable columns of type string? Because if they aren't, then data conversion will occur, and "%" will likely be an invalid character in the input string. I assume you are also the author of this: http://www.codeproject.com/Answers/195111/input-string-is-not-in-correct-format-error-when-u.aspx[^] Q&A question, where you give more details.

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

              S Offline
              S Offline
              siva455
              wrote on last edited by
              #9

              please give reply in the link which OriginalGriff provided...

              1 Reply Last reply
              0
              • S siva455

                Hi,

                String temp;
                foreach (DataRow row in Table.Rows)
                {
                foreach (DataColumn column in Table.Columns)
                {
                if(row[column].ToString().Contains("%"))
                {
                temp = row[column].ToString().Replace("%"," ");
                row[column] = temp;
                }
                }
                }
                Table.AcceptChanges();
                Table is nothing but a datatable...
                and when im trying to run the below code... im getting error like input string was not in a correct format
                in the below highlighted line of code..
                String tem;
                foreach (DataRow row in Table.Rows)
                {
                foreach (DataColumn column in Table.Columns)
                {
                if (row[column].ToString().Contains(" "))
                {
                tem = row[column].ToString().Replace(" ", "%");
                row[column] = temp; //getting error in this line...
                }
                }
                }
                Table.AcceptChanges();

                Please help me regarding the same..

                modified on Thursday, May 12, 2011 1:14 AM

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #10

                I already provided you with what may be essential information here[^]. However you did not provide sufficient information to come up with the definitive answer. What column type is it? how does it get formatted? Are you using an explicit Cell Style? Are you using the CellFormatting event? Do you want to change the value itself, or just how it looks when shown in the DGV?

                pradeep455 wrote:

                if (row[column].ToString().Contains(" ")) { tem = row[column].ToString().Replace(" ", "%"); row[column] = temp; //getting error in this line... }

                I am not surprised you are getting an error here: if the cell is a string type, then ToString() is redundant; so I'll assume the cell isn't holding a string. Now how could you assign a string value to it then? So rather than asking over and over again, please just ask once and provide all that is required. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                S 1 Reply Last reply
                0
                • S siva455

                  Hi,

                  String temp;
                  foreach (DataRow row in Table.Rows)
                  {
                  foreach (DataColumn column in Table.Columns)
                  {
                  if(row[column].ToString().Contains("%"))
                  {
                  temp = row[column].ToString().Replace("%"," ");
                  row[column] = temp;
                  }
                  }
                  }
                  Table.AcceptChanges();
                  Table is nothing but a datatable...
                  and when im trying to run the below code... im getting error like input string was not in a correct format
                  in the below highlighted line of code..
                  String tem;
                  foreach (DataRow row in Table.Rows)
                  {
                  foreach (DataColumn column in Table.Columns)
                  {
                  if (row[column].ToString().Contains(" "))
                  {
                  tem = row[column].ToString().Replace(" ", "%");
                  row[column] = temp; //getting error in this line...
                  }
                  }
                  }
                  Table.AcceptChanges();

                  Please help me regarding the same..

                  modified on Thursday, May 12, 2011 1:14 AM

                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #11

                  Please stop reposting this. You have asked the same question twice on this forum and at least twice in the Questions area.

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    I already provided you with what may be essential information here[^]. However you did not provide sufficient information to come up with the definitive answer. What column type is it? how does it get formatted? Are you using an explicit Cell Style? Are you using the CellFormatting event? Do you want to change the value itself, or just how it looks when shown in the DGV?

                    pradeep455 wrote:

                    if (row[column].ToString().Contains(" ")) { tem = row[column].ToString().Replace(" ", "%"); row[column] = temp; //getting error in this line... }

                    I am not surprised you are getting an error here: if the cell is a string type, then ToString() is redundant; so I'll assume the cell isn't holding a string. Now how could you assign a string value to it then? So rather than asking over and over again, please just ask once and provide all that is required. :)

                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    S Offline
                    S Offline
                    siva455
                    wrote on last edited by
                    #12

                    as i already said i had provide the complete info at http://www.codeproject.com/Answers/195111/input-string-is-not-in-correct-format-error-when-u.aspx Actually the columns in the datatable is string type..my requirement is when user clicks on the header sorting should happen ...and the datatable(3 columns) contains values in 2nd,3rd columns were like.. 2%,5%,6% etc...and in 1st column 1,2,3.. so wat i tried is im creating one more column of decimal type dynamically when user clicks on column..from the values in the column(after removing % from the string values and converting to decimal)... correct me if im wrong.. -- Modified Monday, May 16, 2011 4:42 AM

                    L 1 Reply Last reply
                    0
                    • S siva455

                      as i already said i had provide the complete info at http://www.codeproject.com/Answers/195111/input-string-is-not-in-correct-format-error-when-u.aspx Actually the columns in the datatable is string type..my requirement is when user clicks on the header sorting should happen ...and the datatable(3 columns) contains values in 2nd,3rd columns were like.. 2%,5%,6% etc...and in 1st column 1,2,3.. so wat i tried is im creating one more column of decimal type dynamically when user clicks on column..from the values in the column(after removing % from the string values and converting to decimal)... correct me if im wrong.. -- Modified Monday, May 16, 2011 4:42 AM

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #13

                      You should not be using a string-type column at all; if the data is numeric, use a numeric-type column, that will give you numeric sorting instead of alphabetical sorting (i.e. 9 - 10 - 11 and not 10 - 11 - 9). There is a difference between what a cell value means (a number, a percentage, whatever) and how it looks (with or without thousands separator, with or without a % sign, etc), that is what cell format properties and events are taking care of. I think I already told you all that. :|

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups