Link to home
Start Free TrialLog in
Avatar of mrong
mrong

asked on

How to update records by Checkbox in ASP.NET

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.
ASKER CERTIFIED SOLUTION
Avatar of Swapnil
Swapnil
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrong
mrong

ASKER

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
Can you try changing " & Repeater1.Items(i).proposal(i) & " to " & Repeater1.Items(i).DataKeys(i) & " instead?

Hope it helps,

Regards,
Max.
Avatar of mrong

ASKER

DreamMaster,
Do I need  to set datakey to MyID somewhere?
Thanks.
Avatar of mrong

ASKER

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' "
You should be able to set that in your repeater. DataKeyField="MyID"

Hope it helps,

Regards,
Max.
Avatar of mrong

ASKER

Max,
Where and how can I set the DataKeyField in repeater?
Thank you for your patience.
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.
Avatar of mrong

ASKER

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>?' "