Solved

How to update records by Checkbox in ASP.NET

Posted on 2007-03-20
10
279 Views
Last Modified: 2008-01-09
Greeting,

I use Repeater to display data on asp.net form. I added a checkbox field(not from the DB) in the repeater and a 'update' button outside of the repeater. Here is what I want, the user can click on the checkboxes next to each record, then click on 'update' button to run a update sql stmt for those check marked records.

the update sql stmt will always be the same like the following:
update tbl1
set field1='completed'
where ID= MyID

MyID is a field on the repeater which is the primary field(unique).

Thanks in advance.
0
Comment
Question by:mrong
[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
  • 5
  • 3
  • 2
10 Comments
 
LVL 16

Accepted Solution

by:
Swapnil Piparia earned 70 total points
ID: 18755811
Hi mrong,
you can write the code as following

<asp:reapeater...>
<ItemTemplate>
<asp:checkbox id="chkId" ToolTip = '<%# DataBinder.Eval(container.DataItem, "MyId") %' >
</ItemTemplate>
</asp:reapeater...>

and in your update button click

looping through repeater
find check box control through repeater.findcontrol("chkId")
check that wheather checkbox is check or not and base on that
take the value of tooltip property as Id parameter for your update procedure

Regards,
NetSwap.
0
 
LVL 19

Assisted Solution

by:DreamMaster
DreamMaster earned 55 total points
ID: 18755831
You will have to check whether or not the checkbox was set. Did you use HTML checkboxes or the ASP:Checkbox versions?

I've done something similar where I was able to check the rights users had to certain pages in my application.

I used asp:Checkbox and then checked like this:

For i = 0 To PageRights.Items.Count - 1
   Dim attControl As CheckBox
   attControl = PageRights.Items(i).Cells(0).FindControl("viewable")
   If attControl.Checked = True Then
      viewable = 1
   Else
      viewable = 0
   End If
   attControl = PageRights.Items(i).Cells(1).FindControl("editable")
   If attControl.Checked = True Then
      editable = 1
   Else
      editable = 0
   End If
   _strSQLQuery = "INSERT INTO group_rights (pageID, groupID, [view], edit) VALUES (" & PageRights.DataKeys(i) & ", " & Request.QueryString("id") & ", " & viewable & ", " & editable & ")"
   _objDatabase.InsertQuery(_strSQLQuery, "group_rights")
Next

In my case I used a datagrind named PageRights..however the rest will be quite similar. You should set the datakey to the MyID field to get this to work.

Hope it helps,

Regards,
Max.
0
 

Author Comment

by:mrong
ID: 18756184
netswap,DreamMaster:
Thank you for your post. Below is what I have in my code-behind. Repeater1 is the name of my repeater and closeCase is the name of the checkbox.  "&Repeater1.Items(i).proposal(i) &" line gave me error. Could you guys kindly take a look at my code and let me know what is worng?

Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
        Dim i
        For i = 0 To Repeater1.Items.Count - 1
            Dim attControl As CheckBox
            Dim closeCase
            attControl = Repeater1.Items(i).FindControl("closeCase")
            If attControl.Checked = True Then
                closeCase = 1
            Else
                closeCase = 0
            End If

            Dim sqlText6 = "Update ae_p_phs_e set status_code='65-COMP' where proposal ='" & Repeater1.Items(i).proposal(i) & "' "
            Dim MyOracleConn6 As New System.Data.OracleClient.OracleConnection("Data Source=MyDB;Persist Security Info=True;User ID=ROOT;Password=MyPWD;Unicode=True")

            Dim MyOracleCMD6 As New System.Data.OracleClient.OracleCommand(sqlText6, MyOracleConn6)
            MyOracleConn6.Open()
            MyOracleCMD6.ExecuteNonQuery()
            MyOracleConn6.Close()
            Response.Redirect("Report.aspx?shop=" & Session("shop"))


        Next
    End Sub
0
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 19

Expert Comment

by:DreamMaster
ID: 18762554
Can you try changing " & Repeater1.Items(i).proposal(i) & " to " & Repeater1.Items(i).DataKeys(i) & " instead?

Hope it helps,

Regards,
Max.
0
 

Author Comment

by:mrong
ID: 18763010
DreamMaster,
Do I need  to set datakey to MyID somewhere?
Thanks.
0
 

Author Comment

by:mrong
ID: 18763900
I use the following sql stmt, it works. Now I need the sub. number '00161' with field name proposal in the repeater. Thanks.

Dim sqlText6 = "Update ae_p_pro_e set status_code='65-C' where proposal ='00161' "
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 18764115
You should be able to set that in your repeater. DataKeyField="MyID"

Hope it helps,

Regards,
Max.
0
 

Author Comment

by:mrong
ID: 18764243
Max,
Where and how can I set the DataKeyField in repeater?
Thank you for your patience.
0
 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 18764347
Hi mrong,

Dreammaster,
       There is no such property in repeater, its property of datagrid.

mrong,
       you can try my way by binding any property of checkbox with myid column and later on take id from checkbox's property and update your row, code guideline is given in my first comment.

Regards,
Netswap.
0
 

Author Comment

by:mrong
ID: 18764536
netswap,
in the form I have the code below as you recommended.
<td> <asp:CheckBox id="closeCase" Visible='<%# Trim(DataBinder.Eval(Container.DataItem,"status_code1"))= "65-C" OR  Trim(DataBinder.Eval(Container.DataItem,"status_code1")) = "70-F" OR Trim(DataBinder.Eval(Container.DataItem,"status_code1"))= "75-C"%>' runat="server" ToolTip = '<%# DataBinder.Eval(container.DataItem, "proposal") %>' ></asp:CheckBox></td>

in the code-behind I have sql below.How to take the value of tooltip property as parameter for my update procedure in the sql?

                Dim sqlText6 = "Update ae_p_pro_e set status_code='65-C' where proposal ='<proposal>?' "
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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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