Solved

Loop through dataset check checkbox in gridview rowdatabound

Posted on 2008-06-20
11
4,820 Views
Last Modified: 2012-08-14
Hello, I have a gridview that is populated from a stored procedure. In one itemtemplate I have lblSkill_Id and in another itemtemplate I have a ckbSkill. That runs great and I have a gridview populated with several rows of the skill_id and an empty checkbox.
Now, on the rowdatabound for the gridview fill a dataset of skill_Id's for the given record. I need to loop through the datset and populate the checkboxes in the gridview where the value of the gridviews lblSkill_Id = the value of each skill_id of the dataset.


With my code below I have an inifinite loop issue where the first row in the dataset keep on being used without going to the next row. How can i fix this????

 Protected Sub gvSkills_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSkills.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("EngineeringSkills").ToString)
            Try
                Dim objCmd As SqlCommand = New SqlCommand("Get_RequestSkills", objConn)
                objCmd.CommandType = CommandType.StoredProcedure
                Dim Request_Id As SqlParameter = objCmd.Parameters.Add("@Request_Id", SqlDbType.Int)
                Request_Id.Value = Session("Request_Id").ToString
                Session("Request_Id") = ""

                objConn.Open()

                Dim objDA As New SqlDataAdapter(objCmd)
                Dim objDS As New DataSet("RequestSkills")

                objDA.Fill(objDS)

                If objDS.Tables(0).Rows.Count = 0 Then
                Else
                    Dim iRows As Integer = objDS.Tables(0).Rows.Count
                    For i As Integer = (0) To iRows
                        Dim CheckedSkill As Integer = objDS.Tables(0).Rows(0).Item("Skill_Id")


                        Dim Skill As Label = CType(e.Row.FindControl("lblSkill_Id"), Label)
                        Dim s As Integer = Skill.Text
                        Dim CheckIt As CheckBox = CType(e.Row.FindControl("ckbSkill"), CheckBox)
                        If CheckedSkill = s Then
                            CheckIt.Checked = True
                        End If

                    Next i

                End If
            Catch ex As Exception
                lblMessage.Text = ex.ToString
            Finally
                objConn.Close()

            End Try
        End If
    End Sub
0
Comment
Question by:gogetsome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 22

Expert Comment

by:prairiedog
ID: 21834845
Here:
Dim CheckedSkill As Integer = objDS.Tables(0).Rows(0).Item("Skill_Id")

You are always checking the row(0), the first row.
0
 

Author Comment

by:gogetsome
ID: 21834966
Thank you for assisting!
I changed the line to this:
   Dim CheckedSkill As Integer = objDS.Tables(0).Rows(i).Item("Skill_Id")

It now loops through the dataset, but now when coming through the next row of the dataset the value of
 Dim Skill As Label = CType(e.Row.FindControl("lblSkill_Id"), Label)
                        Dim s As Integer = Skill.Text
does not loop to the next row of the gridview

so even though I'm looping through the dataset the condition is only evaluated for the first row of the gridview.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21834993
Where do you bind your GridView? Can I take a look at your Page_Load?
Also you code will eventually throw an exception because this:
For i As Integer = (0) To iRows  <=== Need to be iRows - 1
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:gogetsome
ID: 21835101
Thank you! I fixed the iRows - 1

The gvSkills is populated on the SelectedindexChanged of another gridview. The sproc just gets all the skills form the skills table and thows them in the gvSkills. In GetRequest() of the same event I populate a session with the primary key of RequestSkills table where the dataset gets the skill(s) for the requested record. Which is why I need to loop through the dataset of the records skills and check the checkbox of the gvskills. The idea is that the user can add or remove skills. When the user is finished I will update the records skills.

Here is where I populate the gvSkills:

Protected Sub gvRequestList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvRequestList.SelectedIndexChanged
        Dim row As GridViewRow = gvRequestList.SelectedRow
        Session("Request_Id") = (CType(row.Cells(1).FindControl("lblRequest_Id"), Label)).Text
        pnlRequest.Visible = True
        pnlRequestList.Visible = False
        GetRequest()
        gvSkills.DataBind()
    End Sub



Is the problem due to binding order??
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21835149
Because you are using Session("Request_Id") to store your data. After the first row, you did this:
Session("Request_Id") = ""
so there is no data in the session, and when it comes to the second row, it will find empty string in Session("Request_id"), so your Request_id.Value is nothing. No data will be loaded then. Can you confirm that after the first datarow, this line is always true?
     If objDS.Tables(0).Rows.Count = 0 Then
0
 

Author Comment

by:gogetsome
ID: 21835207
I commented out the Session("Request_Id") = ""

The dataset value does loop.
 Dim CheckedSkill As Integer = objDS.Tables(0).Rows(i).Item("Skill_Id")

but the

 Dim Skill As Label = CType(e.Row.FindControl("lblSkill_Id"), Label)
                        Dim s As Integer = Skill.Text


Where I want to get the next row of the gridview to evaluate does not change.

So this condition only truely validates the first row of the gridview against the first row of the dataset.
 If CheckedSkill = s Then
                            CheckIt.Checked = True
                        End If

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21835221
It's not Session("Request_Id") = "" causing the problem.
Where did you assign value to the session?
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21835238
>>>It's not Session("Request_Id") = "" causing the problem.
I meant "Not only". Sorry.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21835250
As far as I can see, the purpose of looping through your dataset is to find one row that matches the Skill_id. Once found, check the checkbox, right?
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 21835266
>>>Dim Skill As Label = CType(e.Row.FindControl("lblSkill_Id"), Label)
                        Dim s As Integer = Skill.Text
Where I want to get the next row of the gridview to evaluate does not change.

Because it is within the loop of the dataset. It will not change until the loop is finished, and then it will move to the second row of the Gridview.
0
 

Author Closing Comment

by:gogetsome
ID: 31469318
Thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question