Solved

Loop through dataset check checkbox in gridview rowdatabound

Posted on 2008-06-20
11
4,792 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
  • 7
  • 4
11 Comments
 
LVL 22

Expert Comment

by:prairiedog
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:gogetsome
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:gogetsome
Comment Utility
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
Comment Utility
It's not Session("Request_Id") = "" causing the problem.
Where did you assign value to the session?
0
 
LVL 22

Expert Comment

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

Expert Comment

by:prairiedog
Comment Utility
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
Comment Utility
>>>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
Comment Utility
Thank you!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now