ASP.net Update multiple GridView Checkbox

Capodecina
Permabanned
Joined
31 Dec 2003
Posts
5,172
Location
Barrow-In-Furness
Looking for some help or a point in the right direction with what i'm trying to achieve.

I basically need up update multiple checkbox values at once. The only examples I can find are adding extra columns to GridViews so rows can be deleted. I actually have a field within the table that needs updating so it's a fair bit different.

It's for a timesheet system and the page will be used for approving hours (the checkbox value needs to be set to approve the hours)

The SQL Query i'm using is:

Code:
SELECT ID , TimeCode , Date , Hours , Username , Comments , Approved  , 

( SELECT CodeOwner FROM lward.tblCodes WHERE tblCodes.CodeID = tblHours.TimeCode AND CodeOwner = @UserName) as CodeOwner

FROM lward.tblHours WHERE Approved is NULL and tblHours.TimeCode in (SELECT distinct CodeID from lward.tblCodes WHERE tblCodes.CodeID = tblHours.TimeCode AND CodeOwner = @UserName)

Can anyone help me with this?
 
Last edited:
Multiple checkboxes of the same field in the GridView.

I want to UPDATE several rows of the GridView at once. The only field that will be updated is the CheckBox field and nothing else.

It's basically so a Manager and Approve peoples hours by using the checkbox.
 
When I try create a new stored procedure, the default query is....

Code:
UPDATE [lward].[tblHours] SET [TimeCode] = @TimeCode, [Date] = @Date, [Hours] = @Hours, [Username] = @Username, [Comments] = @Comments, [Approved] = @Approved WHERE (([ID] = @Original_ID))

I changed this to..

Code:
UPDATE lward.tblHours SET Approved WHERE ID = @ID

It says generated UPDATE statement is fine, but gives me an error which just says "There was a problem with the UPDATE stored procedure, The stored procedure was not created."

Hardly descriptive :[
 
Not only do I have no idea how to add a script like that to my DAL, i've got no idea about what's going no so it's abit pointless to use it (i'll struggle with creating the actual VB code to do what I need as well).
 
I've never had to do that is all, there's no need to be funny about it because i'm just learning... :)

I can manage with the rest of the code but i'm just unsure about how to do the actual updating and setting up the query/stored procedure
 
Last edited:
Using Visual Studio 2005, MS SQL Server (2000) and MS SQL Management Studio Express.

I'm fairly certain i'm fine with the rest of the coding, it's just the ADO.net part and actually performing the update. I'm pretty sure I can code the rest of it (checking which checkboxes have been selected).

Thanks :]
 
So if i'm just wanting up UPDATE one field at a time, the UPDATE query work fine as:

UPDATE lward.tblHours SET Approved WHERE ID = @ID

The approved field is a bit type. I guess i'll do some reading on BLLs and try get my head round it again.
 
Ok i've added a new query in my DAL which looks like this...

Code:
UPDATE [tblHours] SET  [Approved] = 1 WHERE (([ID] = @Original_ID))

I'm struggling to workout how to complete my code though and I could do with it checking to make sure i'm not going about this the wrong way. Here's the Code..

Code:
Protected Sub UpdateApproved_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateApproved.Click

        Dim intUpdateValue As Integer
        Dim num As Integer = GridView1.Rows.Count

        Dim hoursAdapter As New ManHoursTableAdapters.tblHoursTableAdapter()


        For i As Integer = 0 To num - 1

            Dim cbox As CheckBox = GridView1.Rows(i).FindControl("CheckBox1")

            If cbox.Checked = True Then
                intUpdateValue = "1"
            Else
                intUpdateValue = "0"
            End If
        Next

        If intUpdateValue = "1" Then
           [B][COLOR="Red"]hoursAdapter.UpdateApproveCheckBoxQuery()[/COLOR][/B]

        End If

    End Sub

On the line i've highlighted i'm getting the message:

"Argument not specified for parameter 'Original_ID' of 'Public overridable function UpdateApprovedCheckBoxQuery(Original_ID as Integer) As Integer"

I know that I need to get the CodeID/Original_ID into the code somewhere but i'm not sure how to do it.

Should I scrap this and go about it a different way?
 
Last edited:
I'm not sure what you mean by post my DAL method? I posted the UPDATE query I added to my tblHours Table Adapter.

The page runs fine using the above code but nothing happens when I click the Approve button.

Here's the code, I had to change it slightly to get it to work. Original_ID needed to be declared outside the If statement.

Code:
Protected Sub UpdateApproved_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateApproved.Click

        Dim intUpdateValue As Integer
        Dim num As Integer = GridView1.Rows.Count
        Dim Original_ID As Integer = -1

        Dim hoursAdapter As New ManHoursTableAdapters.tblHoursTableAdapter()


        For i As Integer = 0 To num - 1

            Dim IDlabel As Label = GridView1.Rows(i).FindControl("Label1")
            Dim cbox As CheckBox = GridView1.Rows(i).FindControl("CheckBox2")


            If IDlabel.Text.Trim().Length > 0 Then
                Original_ID = Convert.ToInt32(IDlabel.Text)
            End If

            If cbox.Checked = True Then
                intUpdateValue = "1"
            Else
                intUpdateValue = "0"
            End If
     [B][COLOR="Red"]   Next[/COLOR][/B]

        If intUpdateValue = "1" AndAlso Original_ID > -1 Then
            hoursAdapter.UpdateApproveCheckBoxQuery(Original_ID)

        End If

I put a break in where the red highlighted line is and the value of Original_ID was "-1"

Thanks for the help, i'm struggling with this one
 
Last edited:
Sorry, i'm not sure what you mean the by the "for .. next" loop?

My DAL Looks like... (Not sure if this is what you're after though)

tblHours
ID - int (Auto-increment identity field)
TimeCode
Date
Hours
Username
Comments
Approved - Bit

Main query is simply: SELECT tblHours.* FROM tblHours
 
Ok I can't figure this out. I checked the GridView to make sure Label1 is the ItemTemplate for the ID field.

IDlabel isn't getting any value :S
 
Code:
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
        DataSourceID="ObjectDataSource1">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="TimeCode" HeaderText="TimeCode" SortExpression="TimeCode" />
            <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
            <asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours" />
            <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" />
            <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
            <asp:TemplateField HeaderText="Approved" SortExpression="Approved">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Approved") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="CodeOwner" HeaderText="CodeOwner" ReadOnly="True" SortExpression="CodeOwner" />
        </Columns>
    </asp:GridView>
 
Cracked it!

The problem with the Label was because I hadn't bound the control within the GridView. Because it's an ItemTemplate and isn't used for inserting etc I didn't think I needed to. Someone on the ASP.net forum told me to bind the Label control and not it's all working perfectly.

Thanks a lot for your help, it's really appreciated :)
 
Back
Top Bottom