Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loop through dataset check checkbox in gridview rowdatabound

Posted on 2008-06-20
11
Medium Priority
?
4,831 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
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!

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

636 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