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