Wrox Programmer Forums
|
BOOK: Beginning ASP.NET Databases Also see the forum ASP Databases for more general discussions of ASP database issues not directly related to these books.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 15th, 2004, 11:20 AM
Registered User
 
Join Date: Nov 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default [?]Chp 7 datagrid not work???

I have encounter the problem at chp7 editing datagrid...?
Please feel free to let me know the error.
The coding as below
************************************************** *******************
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ExchangeRate.aspx.vb" Inherits="authorization.ExchangeRate"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title>ExchangeRate</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 MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:datagrid id="dgProducts" style="Z-INDEX: 101; LEFT: 64px; POSITION: absolute; TOP: 40px"
                runat="server" Width="456px" Height="344px" AutoGenerateColumns="False" OnUpdateCommand="Updaterecord" OnEditCommand="EditRecord" OnCancelCommand="CancelEdit">
                <Columns>
                    <asp:BoundColumn DataField="C_Index" ReadOnly="True" HeaderText="C_Index" Visible=False></asp:BoundColumn>
                    <asp:BoundColumn DataField="Currency" ReadOnly="True" HeaderText="Currency"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Buy" ReadOnly="True" HeaderText="Buy"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Sell" ReadOnly="True" HeaderText="Sell"></asp:BoundColumn>
                    <asp:BoundColumn DataField="InterBuy" ReadOnly="True" HeaderText="InterBuy"></asp:BoundColumn>
                    <asp:BoundColumn DataField="InterSell" ReadOnly="True" HeaderText="InterSell"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Tolerance" ReadOnly="True" HeaderText="Tolerance"></asp:BoundColumn>
                    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Save" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
                </Columns>
            </asp:datagrid></form>
    </body>
</HTML>
Imports System.Data
Imports System.Data.SqlClient


Public Class ExchangeRate
    Inherits System.Web.UI.Page
    Private strConnection As String = "server=(local)\NetSDK;Integrated Security=SSPI;database=pubs"
    Private strSQLSelect As String = "SELECT C_Index,Currency, Buy,Sell,InterBuy,InterSell,Tolerance FROM ExchangeRate"
    Private ProductTableName As String = "ExchangeRate"
    Private objConnection As SqlConnection


#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 dgProducts As System.Web.UI.WebControls.DataGrid

    '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 Connect()
        If objConnection Is Nothing Then
            objConnection = New SqlConnection(strConnection)
        End If

        If objConnection.State = ConnectionState.Closed Then
            objConnection.Open()
        End If
    End Sub

    Private Sub Disconnect()
        objConnection.Close()
    End Sub

    Public Sub EditRecord(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
        dgProducts.EditItemIndex = E.Item.ItemIndex
        LoadGrid()
    End Sub

    Public Sub CancelEdit(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
        dgProducts.EditItemIndex = -1
        LoadGrid()
    End Sub
    Public Sub UpdateRecord(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)

        ' Retrieve the field values in the edited row
        Dim C_Index As Int32 = Convert.ToInt32(E.Item.Cells(0).Text)
        Dim CurrencyTextBox As TextBox = CType(E.Item.Cells(2).Controls(0), TextBox)
        Dim Currency As String = CurrencyTextBox.Text
        Dim BuyTextBox As TextBox = CType(E.Item.Cells(3).Controls(0), TextBox)
        Dim Buy As Decimal = Convert.ToDecimal(BuyTextBox.Text)
        Dim SellTextBox As TextBox = CType(E.Item.Cells(4).Controls(0), TextBox)
        Dim Sell As Decimal = Convert.ToDecimal(SellTextBox.Text)
        Dim InterBuyTextBox As TextBox = CType(E.Item.Cells(5).Controls(0), TextBox)
        Dim InterBuy As Decimal = Convert.ToDecimal(InterBuyTextBox.Text)
        Dim InterSellTextBox As TextBox = CType(E.Item.Cells(6).Controls(0), TextBox)
        Dim InterSell As Decimal = Convert.ToDecimal(InterSellTextBox.Text)
        Dim ToleranceTextBox As TextBox = CType(E.Item.Cells(7).Controls(0), TextBox)
        Dim Tolerance As Decimal = Convert.ToDecimal(ToleranceTextBox.Text)

        dgProducts.EditItemIndex = -1
        UpdateProduct(C_Index, Currency, Buy, Sell, InterBuy, InterSell, Tolerance)
    End Sub

    Private Sub UpdateProduct(ByVal C_Index As Integer, ByVal Currency As String, ByVal Buy As Decimal, ByVal Sell As Decimal, ByVal InterBuy As Decimal, ByVal InterSell As Decimal, ByVal Tolerance As Decimal)

        ' Create and load a DataSet with records from Northwind.Products table
        Connect()
        Dim adapter As New SqlDataAdapter(strSQLSelect, objConnection)
        Dim ds As New DataSet
        adapter.Fill(ds, ProductTableName)
        Disconnect()

        ' Modify the in-memory records in the DataSet
        Dim tbl As DataTable = ds.Tables(ProductTableName)
        tbl.PrimaryKey = New DataColumn() _
                         { _
                           tbl.Columns("C_Index") _
                         }
        Dim row As DataRow = tbl.Rows.Find(C_Index)
        row.Item("Currency") = Currency
        row.Item("Buy") = Buy
        row.Item("Sell") = Sell
        row.Item("InterBuy") = InterBuy
        row.Item("InterSell") = InterSell
        row.Item("Tolerance") = Tolerance

        ' Reconnect the DataSet and update the database
        Dim cb As New SqlCommandBuilder(adapter)
        Connect()
        adapter.Update(ds, ProductTableName)
        Disconnect()

        dgProducts.DataSource = ds.Tables(ProductTableName)
        dgProducts.DataBind()
    End Sub

    Private Sub LoadGrid()
        Connect()
        Dim adapter As New SqlDataAdapter(strSQLSelect, objConnection)
        Dim ds As New DataSet
        adapter.Fill(ds, ProductTableName)
        Disconnect()

        dgProducts.DataSource = ds.Tables(ProductTableName)
        dgProducts.DataBind()
    End Sub
    Private Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            LoadGrid()
        End If
    End Sub


End Class


************************************************** *****************
END






Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does this code work (chp.6)? wrockinator BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 3 December 3rd, 2008 03:16 AM
Paging of Datagrid cannot work annie_stwg ASP.NET 1.0 and 1.1 Basics 5 March 21st, 2006 07:29 PM
Chp 14 e-commerce JamesA BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 September 3rd, 2004 06:14 PM
datareader/datagrid will not work eon ASP.NET 1.0 and 1.1 Basics 2 January 28th, 2004 11:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.