locked
Gridview For loop returning the first row RRS feed

  • Question

  • User-129549274 posted

    How can i solve this? pls help.

    For Each gvRow As GridViewRow In gvClients.Rows
    
                cmdExcel.CommandText = "INSERT INTO [Sheet2$] (tvlr_Lastname, tvlr_Firstname,tvlr_title)" & " values (@Surname,@FName,@Title)"
               cmdExcel.Parameters.AddWithValue("@Surname", gvRow.Cells(0).Text)
                cmdExcel.Parameters.AddWithValue("@FName", gvRow.Cells(1).Text)
                cmdExcel.Parameters.AddWithValue("@Title", gvRow.Cells(2).Text)
                       
                cmdExcel.ExecuteNonQuery()
        Next
     
    Friday, February 20, 2009 9:23 AM

Answers

  • User-687703514 posted
    Use Like this
     For i As Integer = 0 To gvClients.Rows.Count - 1

    cmdExcel.CommandText = "INSERT INTO [Sheet2$] (tvlr_Lastname, tvlr_Firstname,tvlr_title)" & " values (@Surname,@FName,@Title)"
    cmdExcel.Parameters.AddWithValue("@Surname", gvRow.Rows(i).Cells(0).Text)
    cmdExcel.Parameters.AddWithValue("@FName", gvRow.Rows(i).Cells(1).Text)
    cmdExcel.Parameters.AddWithValue("@Title", gvRow.Rows(i).Cells(2).Text)

    cmdExcel.ExecuteNonQuery()

    Next
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 20, 2009 9:40 AM
  • User-687703514 posted

     Sorry for the problem. it was from my side

    Use like this

    gvClients.Rows(i).Cells(0).Text
     Instead of
     gvRow.Rows(i).Cells(0).Text

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 20, 2009 10:03 AM
  • User-687703514 posted

     Use Debugger and chek what row count comes on this line

    For i As Integer = 0 To gvClients.Rows.Count - 1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 20, 2009 10:12 AM
  • User-129549274 posted

    The code does loop through each row, but, for some reason it still insert the first row.

    Checking of the row  type? i am not sure what you mean.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 23, 2009 1:41 AM
  • User-129549274 posted

    Done, I figured that I had to include everything, from the connection, in the For Loop.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 26, 2009 2:03 AM

All replies

  • User-687703514 posted
    Use Like this
     For i As Integer = 0 To gvClients.Rows.Count - 1

    cmdExcel.CommandText = "INSERT INTO [Sheet2$] (tvlr_Lastname, tvlr_Firstname,tvlr_title)" & " values (@Surname,@FName,@Title)"
    cmdExcel.Parameters.AddWithValue("@Surname", gvRow.Rows(i).Cells(0).Text)
    cmdExcel.Parameters.AddWithValue("@FName", gvRow.Rows(i).Cells(1).Text)
    cmdExcel.Parameters.AddWithValue("@Title", gvRow.Rows(i).Cells(2).Text)

    cmdExcel.ExecuteNonQuery()

    Next
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 20, 2009 9:40 AM
  • User-129549274 posted

    Thanks Muhammad,

    But i find a problem with the gvRow, I tried to declare it this way "

    Dim gvRow As GridViewRow

    For i As Integer = 0 To gvClients.Rows.Count - 1

    next

    But it does'nt work. What do you suggest i do.

    Friday, February 20, 2009 10:00 AM
  • User-687703514 posted

     Sorry for the problem. it was from my side

    Use like this

    gvClients.Rows(i).Cells(0).Text
     Instead of
     gvRow.Rows(i).Cells(0).Text

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 20, 2009 10:03 AM
  • User-129549274 posted

    still returning the first row.

    Friday, February 20, 2009 10:09 AM
  • User-687703514 posted

     Use Debugger and chek what row count comes on this line

    For i As Integer = 0 To gvClients.Rows.Count - 1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 20, 2009 10:12 AM
  • User2130758966 posted

     Hi, dont you need to check the rowtype before you process it?

    Friday, February 20, 2009 10:17 AM
  • User-129549274 posted

    The code does loop through each row, but, for some reason it still insert the first row.

    Checking of the row  type? i am not sure what you mean.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 23, 2009 1:41 AM
  • User-129549274 posted

    Done, I figured that I had to include everything, from the connection, in the For Loop.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 26, 2009 2:03 AM