"Input string was not in a correct format." when updating datatable... [modified]
-
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
-
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
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
-
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
check whether it is a strongly typed data table
-
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
-
check whether it is a strongly typed data table
-
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
How can you directly assign to a foreach iteration variable ? It will throw an error , right ?
-
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
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."
-
How can you directly assign to a foreach iteration variable ? It will throw an error , right ?
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."
-
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."
-
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
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.
-
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
-
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.
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
-
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
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.