By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,965 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,965 IT Pros & Developers. It's quick & easy.

code working but still getting error

P: n/a
I have code that is doing some updating to a record. Its getting the ID to update from the Grid. I'm passing an INT to my method to update the record. My code is working though I'm still getting an 'input string was not in a correct format.'
Code:

foreach (GridViewRow row in grid.Rows)
{
CheckBox chk = (CheckBox)gr.FindControl("checkbox");
if (chk.Checked)
{
Data+= grid.DataKey[grid.RowIndex].value + ";";
}
}
String[] rowValues= Data.Split(';');

foreach (string updateValues in rowValues
{
Update.UpdateUserRow(Convert.ToInt32(updateValues) );
}

this is updating the record that is selected in the grid, but I'm getting the error
'input string was not in a correct format.'
any idea why?

Aug 17 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Most likely because of the last ';' symbol.
I guess
1;2;3; ends up when you do Split as a an array of 4 elements '1', '2', '3', '' (notice empty element).

So it blows up when you try to convert it to Int32.

George.
"Mike" <Mi**@community.nospam.comwrote in message news:uJ**************@TK2MSFTNGP03.phx.gbl...
I have code that is doing some updating to a record. Its getting the ID to update from the Grid. I'm passing an INT to my method to update the record. My code is working though I'm still getting an 'input string was not in a correct format.'
Code:

foreach (GridViewRow row in grid.Rows)
{
CheckBox chk = (CheckBox)gr.FindControl("checkbox");
if (chk.Checked)
{
Data+= grid.DataKey[grid.RowIndex].value + ";";
}
}
String[] rowValues= Data.Split(';');

foreach (string updateValues in rowValues
{
Update.UpdateUserRow(Convert.ToInt32(updateValues) );
}

this is updating the record that is selected in the grid, but I'm getting the error
'input string was not in a correct format.'
any idea why?

Aug 17 '07 #2

P: n/a
"Mike" <Mi**@community.nospam.comwrote in message
news:uJ**************@TK2MSFTNGP03.phx.gbl...
String[] rowValues= Data.Split(';');
String[] rowValues= Data.TrimEnd(';').Split(';');
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 17 '07 #3

P: n/a
Mike wrote:
I have code that is doing some updating to a record. Its getting the ID
to update from the Grid. I'm passing an INT to my method to update the
record. My code is working though I'm still getting an 'input string was
not in a correct format.'
Code:

foreach (GridViewRow row in grid.Rows)
{
CheckBox chk = (CheckBox)gr.FindControl("checkbox");
if (chk.Checked)
{
Data+= grid.DataKey[grid.RowIndex].value + ";";
}
}
String[] rowValues= Data.Split(';');

foreach (string updateValues in rowValues
{
Update.UpdateUserRow(Convert.ToInt32(updateValues) );
}

this is updating the record that is selected in the grid, but I'm
getting the error
'input string was not in a correct format.'
any idea why?
I assume that you only want to update each row once? With your code you
would update the first row, then your would update first row again when
the second row is updated, and so on. If ten rows are selected, you
would be updating the first of them ten times.

Just get the value and update the row:

foreach (GridViewRow row in grid.Rows) {
CheckBox chk = (CheckBox)gr.FindControl("checkbox");
if (chk.Checked) {
int value = int.Parse(grid.DataKey[grid.RowIndex].value);
Update.UpdateUserRow(value);
}
}

--
Göran Andersson
_____
http://www.guffa.com
Aug 17 '07 #4

P: n/a
this works for only one. I need to capture all the rows that are checked
"Göran Andersson" <gu***@guffa.comwrote in message
news:uE**************@TK2MSFTNGP06.phx.gbl...
Mike wrote:
>I have code that is doing some updating to a record. Its getting the ID
to update from the Grid. I'm passing an INT to my method to update the
record. My code is working though I'm still getting an 'input string was
not in a correct format.'
Code:
foreach (GridViewRow row in grid.Rows)
{
CheckBox chk = (CheckBox)gr.FindControl("checkbox");
if (chk.Checked)
{
Data+= grid.DataKey[grid.RowIndex].value + ";";
}
}
String[] rowValues= Data.Split(';');
foreach (string updateValues in rowValues
{
Update.UpdateUserRow(Convert.ToInt32(updateValues) );
}
this is updating the record that is selected in the grid, but I'm
getting the error
'input string was not in a correct format.'
any idea why?

I assume that you only want to update each row once? With your code you
would update the first row, then your would update first row again when
the second row is updated, and so on. If ten rows are selected, you would
be updating the first of them ten times.

Just get the value and update the row:

foreach (GridViewRow row in grid.Rows) {
CheckBox chk = (CheckBox)gr.FindControl("checkbox");
if (chk.Checked) {
int value = int.Parse(grid.DataKey[grid.RowIndex].value);
Update.UpdateUserRow(value);
}
}

--
Göran Andersson
_____
http://www.guffa.com

Aug 17 '07 #5

P: n/a
thanks this worked
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:uD**************@TK2MSFTNGP02.phx.gbl...
"Mike" <Mi**@community.nospam.comwrote in message
news:uJ**************@TK2MSFTNGP03.phx.gbl...
>String[] rowValues= Data.Split(';');

String[] rowValues= Data.TrimEnd(';').Split(';');
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 17 '07 #6

P: n/a
Mike wrote:
this works for only one. I need to capture all the rows that are checked
"Göran Andersson" <gu***@guffa.comwrote in message
news:uE**************@TK2MSFTNGP06.phx.gbl...
What do you mean? You are looping through the rows, you don't have to
repeat all the updates for every new row you find.

I realise now that your code is even worse than I first thought. You're
not only repeating the first update for every subsequent selected row,
but repeating it for every subsequent row regardless if it's selected or
not. If you have a hundred row and select the first one to be updated,
it will be updated a hundred times!

--
Göran Andersson
_____
http://www.guffa.com
Aug 17 '07 #7

P: n/a
Actually I got it working with my code.

Once I added Mark's suggestion, it works just fine.

String[] rowValues= Data.TrimEnd(';').Split(';');
"Göran Andersson" <gu***@guffa.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Mike wrote:
>this works for only one. I need to capture all the rows that are checked
"Göran Andersson" <gu***@guffa.comwrote in message
news:uE**************@TK2MSFTNGP06.phx.gbl...

What do you mean? You are looping through the rows, you don't have to
repeat all the updates for every new row you find.

I realise now that your code is even worse than I first thought. You're
not only repeating the first update for every subsequent selected row, but
repeating it for every subsequent row regardless if it's selected or not.
If you have a hundred row and select the first one to be updated, it will
be updated a hundred times!

--
Göran Andersson
_____
http://www.guffa.com

Aug 17 '07 #8

P: n/a
Mike wrote:
Actually I got it working with my code.

Once I added Mark's suggestion, it works just fine.

String[] rowValues= Data.TrimEnd(';').Split(';');
Yes, it's working, but you are doing a tremendous amount of work totally
needlessly.

It's like setting the values 1 to 100 in an array as:

int[] num = new int[100];
for (int i = 0; i < 100; i++) {
for (int j = 0; j <= i; j++) {
num[j] = j + 1;
}
}

--
Göran Andersson
_____
http://www.guffa.com
Aug 17 '07 #9

P: n/a
I could have 1 selected or 1000 selected, so the update needs to take place
for each row that is selected and the way you suggested only works for one
row, thats it. what happens if the user selects all rows, or 5 rows?

"Göran Andersson" <gu***@guffa.comwrote in message
news:ug**************@TK2MSFTNGP02.phx.gbl...
Mike wrote:
>Actually I got it working with my code.

Once I added Mark's suggestion, it works just fine.

String[] rowValues= Data.TrimEnd(';').Split(';');

Yes, it's working, but you are doing a tremendous amount of work totally
needlessly.

It's like setting the values 1 to 100 in an array as:

int[] num = new int[100];
for (int i = 0; i < 100; i++) {
for (int j = 0; j <= i; j++) {
num[j] = j + 1;
}
}

--
Göran Andersson
_____
http://www.guffa.com

Aug 17 '07 #10

P: n/a
Mike wrote:
I could have 1 selected or 1000 selected, so the update needs to take place
for each row that is selected and the way you suggested only works for one
row, thats it. what happens if the user selects all rows, or 5 rows?
It's looping all the rows, why do you think that it only works for one row?

You only have to update each row once. You don't have to update each row
hundreds or thousands of times.

--
Göran Andersson
_____
http://www.guffa.com
Aug 18 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.