View Full Version : Updating table based on checkbox state in DataGrid

07-15-2004, 02:18 PM
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:

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


End If

End Sub

Sub bindData()

myconnection = New


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


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"


updval = "False"

End If

If e.CommandName = "Update" Then

myconnection = New


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)




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


<asp:TemplateColumn HeaderText="Approve Account">


<asp:CheckBox id=chk runat="server" Text =<%# DataBinder.Eval(Container.DataItem, "AccountID") %> checked=<%# DataBinder.Eval(Container.DataItem, "Approved") %> />




<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>



07-15-2004, 02:40 PM
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?

07-15-2004, 02:48 PM
Ok.. I am writing this in ASP.NET and the DB is SQL server2000

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum