Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003
This is the forum to discuss the Wrox book Professional VB.NET 2003 by Bill Evjen, Billy Hollis, Rockford Lhotka, Tim McCarthy, Jonathan Pinnock, Rama Ramachandran, Bill Sheldon; ISBN: 9780764559921
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 16th, 2004, 01:28 AM
Registered User
 
Join Date: Jan 2004
Location: San Diego, CA, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapt. 17 DataGrid Updates

I am having problems with the DataGrid Updates exersise in chater 17 page 711.
I am not getting any errors but when I run the program I get a blank page.

Is there anybody who could help?
I an using Visual Basic.Net 2002
My code is pasted below.
Many thanks,
Glen Conaway


<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="DataGridUpdates.WebForm1"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title>DataGrid Updates</title>
        <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
        <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        <script runat="server" language="vb">
    ' Declare a Connection object that is global
    ' in scope to this script...
    Dim objConnection As SqlConnection

    Sub Page_Load(Sender As Object, E As EventArgs)
    objConnection = New _
    SqlConnection("Server=New\NetSdk;Initial Catalog=Pubs;Integrated Security=sspi")

    If Not (IsPostBack)
    BindGrid()
    End If
    End Sub

    Sub BindGrid()
    Dim objDataSet As DataSet
    Dim objDataAdapter As SqlDataAdapter

    objDataAdapter = New SqlDataAdapter( _
    "SELECT au_lname, au_fname, titles.title_id, title, price " & _
    "From authors " & _
    "JOIN titleauthor ON authors.au_id = titleauthor.id " & _
    "JOIN titles ON titleauthor.title_ID = titles.title_id " & _
    "ORDER BY au_lname, au_fname", _
    objConnection)
    objDataSet = New DataSet()
    objDataAdapter.Fill(objDataSet, "Authors")

    grdAuthors.DataSource = objDataSet
    grdAuthors.DataBind()
    End Sub

    Sub EditGridData(Sender As Object, E As DataGridCommandEventArgs)
    grdAuthors.EditItemIndex = CInt(E.Item.ItemIndex)
    BindGrid()
    End Sub

    Sub CancelGridData(Sender As Object, E As DataGridCommandEventArgs)
    grdAuthors.EditItemIndex = -1
    BindGrid()
    End Sub

    Sub UpdateGridData(Sender As Object, E As DataGridCommandEventArgs)
    Dim objCommand As SqlCommand
    Dim objTextBox As TextBox

    Dim strSQL As String = "UPDATE titles " & _
    "SET title = @Title, price = @Price " & _
    "WHERE title_id = @ID"

    objCommand = New SqlCommand(strSQL, objConnection)

    objCommand.Parameters.Add(New SqlParameter("@ID", _
    SqlDbType.VarChar, 6))
    objCommand.Parameters.Add(New SqlParameter("@Title", _
    SqlDbType.VarChar, 80))
    objCommand.Parameters.Add(New SqlParameter("@Price", _
    SqlDbType.Money, 8))

    objCommand.Parameters("@Id").Value = _
    grdAuthors.DataKeys(CInt(E.Item.ItemIndex))

    objTextBox = E.Item.FindControl("edit_title")
    objCommand.Parameters("@Title").Value = objTextBox.Text
    objTextBox = E.Item.FindControl("edit_price")

    If Left(objTextBox.Text,1) = "$" Then
    objTextBox.Text = Right(objTextBox.Text,Len(objTextBox.Text)-1)
    End If
    objCommand.Parameters("@Price").Value = objTextBox.Text

    objCommand.Connection.Open()

    objCommand.ExecuteNonQuery()

    grdAuthors.EditItemIndex = -1

    objCommand.Connection.Close()

    BindGrid()

    End Sub

        </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:DataGrid id="grdAuthors" style="Z-INDEX: 101; LEFT: 18px; POSITION: absolute; TOP: 14px" runat="server" AlternatingItemStyle-BackColor="WhiteSmoke" AutoGenerateColumns="false" BackColor="White" CellPadding="3" DataKeyField="title_id" Font-Name="Verdana" Font-Size="8pt" GridLines="None" HeaderStyle-BackColor="Maroon" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" OnEditCommand="EditGridData" OnCancelCommand="CancelGridData" OnUpdateCommand="UpdateGridData">
                <Columns>
                    <asp:EditCommandColumn EditText="Edit Row" CancelText="Cancel Edit" UpdateText="Update Row" Itemstyle-wrap="False"/>

                    <asp:BoundColumn DataField="title_id" Visible="False" />

                    <asp:BoundColumn DataField="au_lname" HeaderText="Last Name" ReadOnly="True" ItemStyle-Wrap="False"/>

                    <asp:BoundColumn DataField="au_fname" HeaderText="First Name" ReadOnly="True" ItemStyle-Wrap="False"/>

                    <asp:TemplateColumn HeaderText="Title" ItemStyle-Wrap="False">
                        <ItemTemplate>
                            <asp:Label Runat="server" Text='<%# DataBinder.Eval (Container.DataItem, "Title") %>'/>
                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:TextBox Runat="server" ID="edit_Title" Font-Name="Verdana" Font-Size="8pt" Width="400" Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'/>
                        </EditItemTemplate>
                    </asp:TemplateColumn>

                    <asp:TemplateColumn HeaderText="Price" HeaderStyle-Font-Bold="True" ItemStyle-HorizontalAlign="Right">
                        <ItemTemplate>
                            <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "price", "{0:C2}")%>'/>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox Runat="server" ID="edit_price" Font-Name="Verdana" Font-Size="8pt" Width="50" Text='<%# DataBinder.Eval(Container.DataItem, "price", "{0:C2}") %>'/>
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
        </form>
    </body>
</HTML>
  #2 (permalink)  
Old January 28th, 2004, 01:18 AM
Registered User
 
Join Date: Jan 2004
Location: San Diego, CA, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well I finally figured it out.
I forgot to take out the @ Page directive line which was preventing the Page_Load and Page_Unload events from firing.
Their were aslo a couple of typo's in the code I posted.

Life is good!
Glen Conaway
  #3 (permalink)  
Old January 28th, 2004, 12:30 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"JOIN titleauthor ON authors.au_id = titleauthor.id " & _
"JOIN titles ON titleauthor.title_ID = titles.title_id " & _
"JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
"JOIN titles ON titleauthor.title_id = titles.title_id " & _


delete row <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"

and that is where the hell begins:

Server Error in '/DataGrid Updates2' Application.

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30203: Identifier expected.

Source Error:


Line 30: Imports System.Web.UI.WebControls
Line 31:
Line 32: Namespace ASP
Line 33:
Line 34: <System.Runtime.CompilerServices.CompilerGlobalSco peAttribute()> _

Source File: http://localhost/DataGrid Updates2/WebForm1.aspx Line: 32


  #4 (permalink)  
Old January 30th, 2004, 09:21 AM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ADD row <%@ Import Namespace="System.Web.UI.Page" %>

It works only until I click »update row« in the Internet Explorer, then shows error:

Server Error in '/DataGrid Updates2' Application.

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.


  #5 (permalink)  
Old January 30th, 2004, 08:21 PM
Registered User
 
Join Date: Jan 2004
Location: San Diego, CA, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Matejx,
I don't know what the problem is but I would probably look in the UpdateGridData procedure for typos. A try and catch block could help you pinpoint the problem. Unfortunately I am not experienced enough to help you With this, as I only know how to use it in a very basic way. Anyway I have posted my code below, (which is working), to help you locate the problem.

I hope this helps,
Sincerely,
Glen Conaway

Here's my code:

<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title>DataGrid Updates</title>
        <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
        <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <script runat="server" language="vb">
        ' Declare a Connection object that is global
    ' in scope to this script...
    Dim objConnection As SqlConnection

    Sub Page_Load(Sender As Object, E As EventArgs)
    objConnection = New _
    SqlConnection("Server=MyComputer\NetSdk;Trusted_Co nnection=true;Initial Catalog=Pubs")

    ' I have found the following connections strings successfull in the datagrid sorting program:
        'objConnection = New SqlConnection("Server=MyComputer\NetSdk;database=p ubs;Integrated Security=sspi")
        'objConnection = New SqlConnection("Server=MyComputer\NetSdk;Trusted_Co nnection=True;database=pubs")

        'objConnection = New SqlConnection("Server=MyComputer\NetSdk;Trusted_Co nnection=True;Initial Catalog=pubs")


    If Not (IsPostBack)
    BindGrid()
    End If
    End Sub

    Sub BindGrid()
    Dim objDataSet As DataSet
    Dim objDataAdapter As SqlDataAdapter

    objDataAdapter = New SqlDataAdapter( _
    "SELECT au_lname, au_fname, titles.title_id, title, price " & _
    "From authors " & _
    "JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
    "JOIN titles ON titleauthor.title_id = titles.title_id " & _
    "ORDER BY au_lname, au_fname", _
    objConnection)
    objDataSet = New DataSet()
    objDataAdapter.Fill(objDataSet, "Authors")

    grdAuthors.DataSource = objDataSet
    grdAuthors.DataBind()
    End Sub

    Sub EditGridData(Sender As Object, E As DataGridCommandEventArgs)
    grdAuthors.EditItemIndex = CInt(E.Item.ItemIndex)
    BindGrid()
    End Sub

    Sub CancelGridData(Sender As Object, E As DataGridCommandEventArgs)
    grdAuthors.EditItemIndex = -1
    BindGrid()
    End Sub

    Sub UpdateGridData(Sender As Object, E As DataGridCommandEventArgs)
    Dim objCommand As SqlCommand
    Dim objTextBox As TextBox

    Dim strSQL As String = "UPDATE titles " & _
    "SET title = @Title, price = @Price " & _
    "WHERE title_id = @ID"

    objCommand = New SqlCommand(strSQL, objConnection)

    objCommand.Parameters.Add(New SqlParameter("@ID", _
    SqlDbType.VarChar, 6))
    objCommand.Parameters.Add(New SqlParameter("@Title", _
    SqlDbType.VarChar, 80))
    objCommand.Parameters.Add(New SqlParameter("@Price", _
    SqlDbType.Money, 8))

    objCommand.Parameters("@Id").Value = _
    grdAuthors.DataKeys(CInt(E.Item.ItemIndex))

    objTextBox = E.Item.FindControl("edit_title")
    objCommand.Parameters("@Title").Value = objTextBox.Text
    objTextBox = E.Item.FindControl("edit_price")

    If Left(objTextBox.Text,1) = "$" Then
    objTextBox.Text = Right(objTextBox.Text,Len(objTextBox.Text)-1)
    End If
    objCommand.Parameters("@Price").Value = objTextBox.Text

    objCommand.Connection.Open()

    objCommand.ExecuteNonQuery()

    grdAuthors.EditItemIndex = -1

    objCommand.Connection.Close()

    BindGrid()

    End Sub

    </script>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:DataGrid id="grdAuthors" style="Z-INDEX: 101; LEFT: 13px; POSITION: absolute; TOP: 12px" runat="server" AlternatingItemStyle-BackColor="WhiteSmoke" AutoGenerateColumns="false" BackColor="White" CellPadding="3" DataKeyField="title_id" Font-Name="Verdana" Font-Size="8pt" GridLines="None" HeaderStyle-BackColor="Maroon" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" OnEditCommand="EditGridData" OnCancelCommand="CancelGridData" OnUpdateCommand="UpdateGridData">
                <Columns>
                    <asp:EditCommandColumn EditText="Edit Row" CancelText="Cancel Edit" UpdateText="Update Row" Itemstyle-wrap="False" />
                    <asp:BoundColumn DataField="title_id" Visible="False" />
                    <asp:BoundColumn DataField="au_lname" HeaderText="Last Name" ReadOnly="True" ItemStyle-Wrap="False" />
                    <asp:BoundColumn DataField="au_fname" HeaderText="First Name" ReadOnly="True" ItemStyle-Wrap="False" />
                    <asp:TemplateColumn HeaderText="Title" ItemStyle-Wrap="False">
                        <ItemTemplate>
                            <asp:Label Runat="server"
                        Text='<%# DataBinder.Eval (Container.DataItem, "Title") %>'/>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox Runat="server"
                            ID="edit_Title"
                            Font-Name="Verdana"
                            Font-Size="8pt"
                            Width="400"
                            Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'/>
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Price" HeaderStyle-Font-Bold="True" ItemStyle-HorizontalAlign="Right">
                        <ItemTemplate>
                            <asp:Label Runat="server"
                            Text='<%# DataBinder.Eval(Container.DataItem, "price", "{0:C2}")%>'/>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox Runat="server"
                            ID="edit_price"
                            Font-Name="Verdana"
                            Font-Size="8pt"
                            Width="50"
                            Text='<%# DataBinder.Eval(Container.DataItem, "price", "{0:C2}") %>'/>
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
        </form>
    </body>
</HTML>
  #6 (permalink)  
Old January 31st, 2004, 02:55 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Still the same error!
The same error I get in chapter 16 on page 647 with BindingExample. That is why this example works only partially (some functions work others do not).

But I at least know the code is correct.

I guess the problem lies in SQL 2000 administration.

I have the same software as authors of this book: Windows server 2000 SP3, SQL 2000, and Visual studio .NET enterprise edition.
  #7 (permalink)  
Old March 9th, 2004, 12:28 AM
Registered User
 
Join Date: Mar 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Change the line

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="DataGridUpdate_ASPDataBinding.WebForm1"% >


to

<%@ Page Language="vb" AutoEventWireup="true" Codebehind="WebForm1.aspx.vb" Inherits="DataGridUpdate_ASPDataBinding.WebForm1"% >


  #8 (permalink)  
Old May 11th, 2004, 12:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Indianapolis, IN, USA.
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This seems to be a constant problem. Could one of you guys post what you have found to be the corrected code? Thanks for your hard work in finding these errors and solutions.

XUMUSKIEFAN
Wrox Moderator
  #9 (permalink)  
Old May 11th, 2004, 01:51 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No solution found!
I just skipped that part. I Still do not know how to solve the whole chapter!

  #10 (permalink)  
Old May 12th, 2004, 01:00 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Indianapolis, IN, USA.
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Are you still getting the same errors?

XUMUSKIEFAN
Wrox Moderator
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid Updates asp:textbox rjirak BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 May 1st, 2006 05:36 AM
Chapter 17 - Datagrid Updates Project ydv BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 December 27th, 2005 09:06 AM
Chapter 17 - DataGrid Sorting Example Appled BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 4 March 28th, 2005 03:13 PM
No Code for 'People Table' in Chapt 17 peterstandard BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 January 1st, 2005 01:07 AM
DataGrid Updates BLANK PAGE (Chapter 17) happyhorseygal BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 April 8th, 2004 01:11 PM



All times are GMT -4. The time now is 11:01 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.