update database if checkbox is checked


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: update database if checkbox is checked

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    19

    update database if checkbox is checked

    I assign the checkbox each row with specific values from field "pid" retrieved from database. Then when submit button is clicked. I loop through each row to check if checkbox is selected. If selected, then update the table where pid = pid from checkbox value. But somehow my code not working :P Please help:
    Code:
    <asp:TemplateColumn Visible="False" HeaderText="PeoplewiseID">
    							<ItemTemplate>
    								<asp:Label ID="lblPeoplewiseID" Text='<%# DataBinder.Eval(Container.DataItem, "Peoplewise_ID") %>' Runat="server" />
    							</ItemTemplate>
    						</asp:TemplateColumn>
    the code behind....
    Code:
    Public Sub UpdatePayment(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim update As New EventDetailBO
            Dim dgItem As DataGridItem
            Dim chkSelected As CheckBox
            Dim pplwiseID As Integer
            Dim strPID As String
            
            For Each dgItem In dgNotPaid.Items
                chkSelected = dgItem.FindControl("chkPayment")
                If chkSelected.Checked Then
                    strPID = CType(dgItem.FindControl("lblPeoplewiseID"), Label).Text
                    pplwiseID = pplwiseID.Parse(strPID)
                    'pplwiseID = (CType(dgItem.FindControl("chkPayment"), CheckBox).Checked) * -1
                    update.updatePayment(pplwiseID)
                    lblSelected.Text += pplwiseID.ToString()
                    lblSelected.Text += "<br>"
                End If
            Next
    
        End Sub
    The update statement...not sure if it's correct, the "sp_updatePayment" is a stored procedure:
    Code:
    Public Function updatePay(ByVal pplwise As Integer) As Integer
            Dim strpplwise As String
            Dim strConn As String = "workstation id=MYD525MG1S;packet size=4096;user id=sa;data source=MYD225MG1S;pers" & _
                            "ist security info=False;initial catalog=ems"
            Dim conn As New SqlConnection(strConn)
            Dim pplwiseID As New SqlParameter("@pplwsID", SqlDbType.Int)
            pplwiseID.Direction = ParameterDirection.Input
            pplwiseID.Value = pplwise
            Dim objCmd As SqlCommand = New SqlCommand("sp_updatePayment", conn)
            conn.Open()
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.Parameters.Add(pplwiseID)
            objCmd.ExecuteNonQuery()
            conn.Close()
            Return pplwise
        End Function

  2. #2
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    Is the value of the pplwise passed to the stored procedure right when you select a checkbox?
    Secondly this template column seems like a column in the grid , am i right? If so the right place to get the instance of the checkbox that was selected would be available int he itemcommand event of the grid, else where the event of the checkbox is not bubbled up if you dont have an event handler added to the checkbox
    Sri

  3. #3
    Join Date
    Dec 2005
    Posts
    19
    icic. I did tracing the code and found out that in this block of code:
    Code:
    For Each dgItem In dgNotPaid.Items
                chkSelected = dgItem.FindControl("chkPayment")
                If chkSelected.Checked Then
                    strPID = CType(dgItem.FindControl("lblPeoplewiseID"), Label).Text
                    pplwiseID = pplwiseID.Parse(strPID)
                    'pplwiseID = (CType(dgItem.FindControl("chkPayment"), CheckBox).Checked) * -1
                    update.updatePayment(pplwiseID)
                    lblSelected.Text += pplwiseID.ToString()
                    lblSelected.Text += "<br>"
                End If
            Next
    it's not detecting whether my checkbox is checked or not. This is my checkbox code in the ASP.NET page:
    Code:
    <asp:TemplateColumn HeaderText="Payment">
    							<ItemTemplate>
    								<asp:CheckBox ID="chkPayment" Runat="server" />
    							</ItemTemplate>
    						</asp:TemplateColumn>
    is it necessary for me to add in something else to detect the checked status? Thanks for your help in advance

  4. #4
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    I would defintlly like to help you but to know whats happening i need to know the control (is this a datagrid?) in which the column is present and also the event handler name in which this code is present?, because based on that i will know what to correct or remove in your code otherwise i will end up giving you a whole new piece of code which neither of us want

    Anyway see this useful article to know how to access values of controls inside a grid
    http://www.c-sharpcorner.com/Code/20...ataGridVal.asp
    http://www.dotnetjohn.com/articles.aspx?articleid=51
    Sri

  5. #5
    Join Date
    Dec 2005
    Posts
    19
    many thanks srinivas_s
    ya i'm using datagrid. below is the full code on my ASP page:
    Code:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="test.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    	<HEAD>
    		<title>WebForm1</title>
    		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    		<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    		<meta content="JavaScript" name="vs_defaultClientScript">
    		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    	</HEAD>
    	<body>
    		<form id="Form1" method="post" runat="server">
    			<P>&nbsp;</P>
    			<P><IMG alt="" src="file:///C:\Documents and Settings\SIMYSBH\My Documents\Visual Studio Projects\EventDetails1\picUp2.PNG"></P>
    			<P>&nbsp;</P>
    			<P>&nbsp;</P>
    			<P>Commitee:</P>
    			<P><asp:datagrid id="dgComm" runat="server" Width="866px"></asp:datagrid></P>
    			<P>Participants who have not paid:</P>
    			<P><asp:datagrid id="dgNotPaid" runat="server" Width="866px" AutoGenerateColumns="False">
    					<Columns>
    						<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
    						<asp:BoundColumn DataField="Tel" HeaderText="Tel"></asp:BoundColumn>
    						<asp:BoundColumn DataField="Email" HeaderText="Email"></asp:BoundColumn>
    						<asp:BoundColumn DataField="Department" HeaderText="Department"></asp:BoundColumn>
    						<asp:BoundColumn DataField="EmergencyCN" HeaderText="Emergency Contact Name"></asp:BoundColumn>
    						<asp:BoundColumn DataField="EmergencyTel" HeaderText="Emergency Tel"></asp:BoundColumn>
    						<asp:BoundColumn DataField="No_Of_Guest" HeaderText="No Guest"></asp:BoundColumn>
    						<asp:TemplateColumn Visible="False" HeaderText="PeoplewiseID">
    							<ItemTemplate>
    								<asp:Label ID="lblPeoplewiseID" Text='<%# DataBinder.Eval(Container.DataItem, "Peoplewise_ID") %>' Runat="server" />
    							</ItemTemplate>
    						</asp:TemplateColumn>
    						<asp:TemplateColumn HeaderText="Payment">
    							<ItemTemplate>
    								<asp:CheckBox ID="chkPayment" Enabled=true checked='<%# DataBinder.Eval(Container.DataItem, "Payment") %>' Runat="server" />
    							</ItemTemplate>
    						</asp:TemplateColumn>
    					</Columns>
    				</asp:datagrid></P>
    			<P>Participants who have paid:</P>
    			<P><asp:datagrid id="dgPaid" runat="server" Width="866px" AutoGenerateColumns="False">
    					<Columns>
    						<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
    						<asp:BoundColumn DataField="Tel" HeaderText="Tel"></asp:BoundColumn>
    						<asp:BoundColumn DataField="Email" HeaderText="Email"></asp:BoundColumn>
    						<asp:BoundColumn DataField="Department" HeaderText="Department"></asp:BoundColumn>
    						<asp:BoundColumn DataField="EmergencyCN" HeaderText="Emergency Contact Name"></asp:BoundColumn>
    						<asp:BoundColumn DataField="EmergencyTel" HeaderText="Emergency Tel"></asp:BoundColumn>
    						<asp:BoundColumn DataField="No_Of_Guest" HeaderText="No Guest"></asp:BoundColumn>
    					</Columns>
    				</asp:datagrid></P>
    			<P><asp:button id="Update" onclick="UpdatePayment" runat="server" Text="Update"></asp:button></P>
    			<P><asp:button id="Back" runat="server" Text="Back"></asp:button></P>
    			<P>&nbsp;</P>
    			<P><asp:label id="lblSelected" Runat="server">You have selected:</asp:label></P>
    		</form>
    		<P>&nbsp;</P>
    		<IMG alt="" src="file:///C:\Documents and Settings\SIMYSBH\My Documents\Visual Studio Projects\EventDetails1\coverDown2.PNG">
    		<P></P>
    	</body>
    </HTML>
    and this is the .net code behind it...
    Code:
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
    Imports System.Web.UI.WebControls
    
    Public Class WebForm1
        Inherits System.Web.UI.Page
    
    #Region " Web Form Designer Generated Code "
    
        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    
        End Sub
        Protected WithEvents Update As System.Web.UI.WebControls.Button
        Protected WithEvents Back As System.Web.UI.WebControls.Button
        Protected WithEvents dgComm As System.Web.UI.WebControls.DataGrid
        Protected WithEvents dgNotPaid As System.Web.UI.WebControls.DataGrid
        Protected WithEvents dgPaid As System.Web.UI.WebControls.DataGrid
        Protected WithEvents lblSelected As System.Web.UI.WebControls.Label
    
        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object
    
        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub
    
    #End Region
    
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            Dim ev As New EventDetailDAO
            Dim dsComm As DataSet
            Dim dsPartNotPaid As DataSet
            Dim dsPartPaid As DataSet
            Dim i As Integer
            
            dsComm = ev.getCommittee()
            dgComm.DataSource = dsComm
            dgComm.DataBind()
    
            dsPartNotPaid = ev.getPartNotPaid()
            dgNotPaid.DataSource = dsPartNotPaid
            dgNotPaid.DataBind()
    
            dsPartPaid = ev.getPartPaid()
            dgPaid.DataSource = dsPartPaid
            dgPaid.DataBind()
    
        End Sub
    
        Public Sub UpdatePayment(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim update As New EventDetailBO
            Dim dgItem As DataGridItem
            Dim chkSelected As CheckBox
            Dim pplwiseID As Integer
            Dim strPID As String
            
            For Each dgItem In dgNotPaid.Items
    
                If (CType(dgItem.FindControl("chkPayment"), CheckBox).Checked) Then
                    strPID = CType(dgItem.FindControl("lblPeoplewiseID"), Label).Text
                    pplwiseID = pplwiseID.Parse(strPID)
                    update.updatePayment(pplwiseID)
                    lblSelected.Text += pplwiseID.ToString()
                    lblSelected.Text += "<br>"
                End If
            Next
    
        End Sub
    End Class
    somehow right it's not detecting my checked checkboxes... I really wish i can run it in front of you then you'll know what's the error.

    btw I don't mind if you give me whole new code coz I'm new to .NET...I jus want it to work that's it the dotnetjohn link I have been there and was using the same way but somehow it doesn't work

  6. #6
    Join Date
    May 2007
    Posts
    3
    I was facing the same problem a while back until I noticed that before the actual event gets fired, the page_load executes.

    You are binding your grid on page load, so by the time the UpdatePayment code executes, your grid has re-bound itself and you have lost all the selections.

    The solution is to bind your grid only the first time the page loads. Wrap the code in your page_load with an "if !Page.IsPostBack".

Similar Threads

  1. How to update SQL Database from Flex Grid
    By ryanlcs in forum VB Classic
    Replies: 1
    Last Post: 06-13-2005, 11:42 AM
  2. ASP.NET - Update database with databinding
    By MichaelChoi in forum ASP.NET
    Replies: 2
    Last Post: 01-15-2003, 07:37 PM
  3. Replies: 0
    Last Post: 05-24-2001, 03:03 AM
  4. Dynamic Update of DAO Using Model Database
    By Larry Rebich in forum vb.announcements
    Replies: 0
    Last Post: 08-26-2000, 12:56 PM
  5. Multiple Database Architecture
    By mholt28 in forum VB Classic
    Replies: 1
    Last Post: 06-05-2000, 11:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center