Step 12 on page 609 instructs me to click run to see the results of the previous 11 steps. The error message I get is shown in Section A below. The code that produced the error is shown in Section B below. As a companion to that, Section C is also enclosed; itâs the HTML from the exercise.
The error message is not fully clear to me. I suspect the issue is this â when it tries to execute line 61 and returns the error regarding âLine 1⦠near =â it may be referring to this line of code:
'Initialize the connection object
objConnection = New SqlConnection("Server=localhost;Initial Catalog=Pubs;User ID=dennis;Password=dennis;")
If it is, Iâm stumped. I tested the connection using Query Analyzer and the dennis/dennis combination worked.
Am I on the right track or has my train of thought jumped the rails? Either way, help!
Thank you!
Section A
Server Error in '/DataGridSorting' Application.
--------------------------------------------------------------------------------
Line 1: Incorrect syntax near '='.
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.Data.SqlClient.SqlException: Line 1: Incorrect syntax near '='.
Source Error:
Line 59: 'Initialize the DataSet object and fill it
Line 60: objDataSet = New DataSet
Line 61: objDataAdapter.Fill(objDataSet, "Authors")
Line 62:
Line 63: 'Declare a Dataview object, populate it, and sort the data in it
Source File: c:\inetpub\wwwroot\DataGridSorting\WebForm1.aspx.v b Line: 61
Stack Trace:
[SqlException: Line 1: Incorrect syntax near '='.]
System.Data.SqlClient.SqlCommand.ExecuteReader(Com mandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +723
System.Data.SqlClient.SqlCommand.ExecuteReader(Com mandBehavior behavior) +45
System.Data.SqlClient.SqlCommand.System.Data.IDbCo mmand.ExecuteReader(CommandBehavior behavior) +5
System.Data.Common.DbDataAdapter.FillFromCommand(O bject data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +304
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +77
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +36
DataGridSorting.WebForm1.BindGrid(String strSortField) in c:\inetpub\wwwroot\DataGridSorting\WebForm1.aspx.v b:61
DataGridSorting.WebForm1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\DataGridSorting\WebForm1.aspx.v b:39
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573
Section B
Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
'Declare a Connection object that is global in scope
Dim 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 grdAuthors 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 Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
'Initialize the connection object
objConnection = New SqlConnection("Server=localhost;Initial Catalog=Pubs;User ID=dennis;Password=dennis;")
'Only bind the data the first time the page is built
'Subsequent post backs will be to sort the data in the grid
If Not (IsPostBack) Then
BindGrid("Last Name")
End If
End Sub
Sub BindGrid(ByVal strSortField As String)
'Declare the objects
Dim objDataSet As DataSet
Dim objDataAdapter As SqlDataAdapter
'Set the SQL string
objDataAdapter = New SqlDataAdapter( _
"SELECT au_lname as 'Last Name', au_fname as 'First Name', " & _
"title as 'Book Title', price AS 'Retail Price' " & _
"FROM authors " & _
"JOIN titleauthor.title_id = titleauthor.au_id " & _
"JOIN titles ON titleauthor.title_id = titles.title_id " & _
"ORDER BY au_lname, au_fname", objConnection)
'Initialize the DataSet object and fill it
objDataSet = New DataSet
objDataAdapter.Fill(objDataSet, "Authors")
'Declare a Dataview object, populate it, and sort the data in it
Dim objDataView As DataView = objDataSet.Tables("Authors").DefaultView
objDataView.Sort = strSortField
'Bind the Dataview object to the DataGrid control
grdAuthors.DataSource = objDataView
grdAuthors.DataBind()
'Clean up
objDataView = Nothing
objDataSet = Nothing
objDataAdapter = Nothing
End Sub
Private Sub grdAuthors_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEvent Args) Handles grdAuthors.SortCommand
'Bind the datagrid using the sort column requested
BindGrid(e.SortExpression)
End Sub
End Class
Section C
<%@ Page Language="
vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.
vb" Inherits="DataGridSorting.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>DataGrid Sorting</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="grdAuthors" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 56px"
runat="server" Width="280px" Height="88px" AllowSorting="true" AlternatingItemStyle-BackColor="#00cc99"
AutoGenerateColumns="true" BackColor="#ffffff" CellPadding="3" Font-Name="Veranda" Font-Size="8pt"
GridLines="None" HeaderStyle-BackColor="#996666" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="#ffffff"></asp:DataGrid>
</form>
</body>
</HTML>