code working but still getting error

M

Mike

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?
 
G

George Ter-Saakov

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.
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?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mike said:
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);
}
}
 
M

Mike

this works for only one. I need to capture all the rows that are checked
Göran Andersson said:
Mike said:
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);
}
}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mike said:
this works for only one. I need to capture all the rows that are checked
news:[email protected]...

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!
 
M

Mike

Actually I got it working with my code.

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

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

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mike said:
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;
}
}
 
M

Mike

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 said:
Mike said:
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;
}
}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mike said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top