Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Updating table based on checkbox state in DataGrid

    I basically copied this code from someone, replaced their table and column names with mine, and this still does not update my table. I can't seem to figure out what's wrong!! Here are the code parts that matter:


    Code:
    Dim myconnection As SqlConnection
    
    Dim myda As SqlDataAdapter
    
    Dim mycmd As SqlCommand
    
    Dim ds As DataSet
    
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    If Not Page.IsPostBack Then
    
    	bindData()
    
    End If
    
    End Sub
    
    Sub bindData()
    
            myconnection = New 
    
    
    
    SqlConnection("Server=localhost;UID=3532535r;PWD=345345;Database=54325")
    
            myda = New SqlDataAdapter("SELECT dbo.AccountTransfers.AccountID, dbo.AccountTransfers.Approved, 
    
    
    
    dbo.Accounts.AccountName, dbo.Territories.TerritoryName, Territories_1.TerritoryName AS Expr1, 
    
    
    
    AccountTransfers.EffectiveDate, AccountTransfers.Explanation FROM dbo.AccountTransfers INNER JOIN 
    
    
    
    dbo.Territories ON dbo.AccountTransfers.FROMTerritoryID = dbo.Territories.TerritoryID INNER JOIN 
    
    
    
    dbo.Territories Territories_1 ON dbo.AccountTransfers.ToTerritoryID = Territories_1.TerritoryID INNER JOIN 
    
    
    
    dbo.Accounts ON dbo.AccountTransfers.AccountID = dbo.Accounts.AccountID WHERE (effectiveDate = 
    
    
    
    convert(datetime,convert(nvarchar,getdate(),101))-day(getdate())+1 AND (Approved = 0))", myconnection)
    
            ds = New DataSet()
    
            myda.Fill(ds, "AllTables")
    
            dg_approval.DataSource = ds
    
            dg_approval.DataBind()
    
    End Sub
    
    
    
    Private Sub dg_approval_ItemCommand(ByVal source As Object, _
    
        ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
    
    Dim prodchknew As CheckBox = e.Item.FindControl("chk")
    
    Dim updval As String
    
    If prodchknew.Checked Then
    
                updval = "True"
    
    Else
    
                updval = "False"
    
    End If
    
    
    
    If e.CommandName = "Update" Then
    
                myconnection = New 
    
    
    
    SqlConnection("Server=localhost;UID=EndoscopySqlUser;PWD=2er@9b76m;Database=Endoscopy")
    
                Dim strsql As String = "Update AccountTransfers set Approved='" & updval & "'" & _
    
             " where AccountID=" & dg_approval.DataKeys(CInt(e.Item.ItemIndex))
    
                ' Response.Write(strsql)
    
                mycmd = New SqlCommand(strsql, myconnection)
    
                myconnection.Open()
    
                mycmd.ExecuteNonQuery()
    
                bindData()
    
    End If
    
    End Sub
    
    
    
    
    And the datagrid: 
    
    
    
    <asp:datagrid id="dg_approval" runat="server" AutoGenerateColumns="False" DataKeyField="AccountID"  Font-Names="Verdana" Font-Size="xx-small">
    
    <Columns>
    
    	<asp:TemplateColumn HeaderText="Approve Account">
    
    	<ItemTemplate>
    
    	    <asp:CheckBox id=chk runat="server" Text =<%# DataBinder.Eval(Container.DataItem, "AccountID") %> checked=<%# DataBinder.Eval(Container.DataItem, "Approved") %> />
    
    	    </asp:CheckBox>
    
    
    
    	</ItemTemplate>
    
    	</asp:TemplateColumn>
    
    	
    
                        <asp:BoundColumn DataField="accountname" HeaderText="Account Name"></asp:BoundColumn>
    
                        <asp:BoundColumn DataField="TerritoryName" HeaderText="From Territory"></asp:BoundColumn>
    
                        <asp:BoundColumn DataField="Expr1" HeaderText="To Territory"></asp:BoundColumn>
    
    		    <asp:BoundColumn DataField="Explanation" HeaderText="Explanation"></asp:BoundColumn>
    
                  	    <asp:BoundColumn DataField="effectivedate" HeaderText="Effective Date"></asp:BoundColumn>
    
    		    <asp:BoundColumn DataField="approved" HeaderText="Approved"></asp:BoundColumn>	
    
    </Columns>
    
    </asp:datagrid>

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ok,
    lets start from the beginning,
    u need to do this in asp.net? or just asp?

    also what kind of data base are you wanting to update?
    sql, mysql, access?

  • #3
    New Coder
    Join Date
    Jun 2004
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thank you very much for helping me...

    Ok.. I am writing this in ASP.NET and the DB is SQL server2000


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •