Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 January 16th, 2006, 03:34 PM
Authorized User
 
Join Date: Jan 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Declaration Expected

VB.net visual studio 2003
In my code below, I put parenthesis behind "Dim ds As New DataSet()"
but when I change lines it removes the parenthesis
AND
    dataAdapter.Fill(ds, "prog")
has squiggly lines underneath and it states "Declaration expected
What AM I doing wrong?
****************************
****************************

Imports System.Data.SqlClient

Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    '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
    Dim connectionString As String = "workstation id=DOZER;packet size=4096;integrated security=SSPI;data source=Dozer;" & _
    "persist security info=False;initial catalog=programmersheaven"

    Dim conn As New SqlConnection(connectionString)
    conn.open

    Dim commandString As String = "SELECT " + _
                          "artId, title, topic, " + _
                           "article.authorId as authorId, " + _
                           "name, lines, dateOfPublishing " + _
                           "FROM " + _
                           "article, author " + _
                           "WHERE " + _
                           "author.authorId = article.authorId"
    Dim dataAdapter As New SqlDataAdapter(commandString, conn)
    'Dim ds As New DataSet
    Dim ds As New System.Data.DataSet
    dataAdapter.Fill(ds, "prog")
    conn.close



    Dim dataTable As dataTable = ds.Tables("prog")


 
Old January 16th, 2006, 03:55 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The reason you get this error is that your code is not inside a method; it's placed directly within the class declaration.

For this function properly, move it to a separate method, like Page_Load or a custom method called Load_Data.

Apparently, VB.NET doesn't like parentheses after a New declaration. It has always surprised me a little. Not sure why it does it, but it still results in valid code:

Dim ds As New DataSet

runs fine and results in a new instance of the DataSet class.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 16th, 2006, 04:00 PM
Authorized User
 
Join Date: Jan 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks,

 I am really new at this.... should I move all the code below to the page_load?

Dim connectionString As String = "workstation id=DOZER;packet size=4096;integrated security=SSPI;data source=Dozer;" & _
    "persist security info=False;initial catalog=programmersheaven"

    Dim conn As New SqlConnection(connectionString)
    conn.open

    Dim commandString As String = "SELECT " + _
                          "artId, title, topic, " + _
                           "article.authorId as authorId, " + _
                           "name, lines, dateOfPublishing " + _
                           "FROM " + _
                           "article, author " + _
                           "WHERE " + _
                           "author.authorId = article.authorId"
    Dim dataAdapter As New SqlDataAdapter(commandString, conn)
    'Dim ds As New DataSet
    Dim ds As New System.Data.DataSet
    dataAdapter.Fill(ds, "prog")
    conn.close



    Dim dataTable As dataTable = ds.Tables("prog")


 
Old January 16th, 2006, 04:09 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It depends. Do you want it to fire on page load? If so, move it to Page_Load.

A better solution is to move it to a sub called LoadData (for example). Then you can call this LoadData method from other methods when necessary. For example, if you want to execute this only when the page loads the first time, and not when it posts back, you can do something like this:
Code:
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles Me.Load
    If Not Page.IsPostBack Then
      LoadData()
    End If
  End Sub

  Private Sub LoadData()
     ' Your code here
  End Sub
  If you're really new at this, I suggest you get a couple of good books to help you get started....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 16th, 2006, 04:21 PM
Authorized User
 
Join Date: Jan 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Getting closer!!! Thanks

I am not sure if I sShould I move all my code I wrote to the sub loadData?
Or do I leave some under the class declaration?
Lke the connection inforamtion?



 
Old January 16th, 2006, 06:01 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Again, it all depends.

It's probably better to move the connection string away from the method, as it's likely you need it more often.

You could define it at the class level, and mark it private. That way, all other methods in your class have access to it.

However, a much better solution is to store the connection string in the web.config file. Then your data access methods can extract it or, even better, you have a separate Shared class (a class with shared methods and properties) that provide access to the string. E.g.

Public Class AppConfiguration
  Public Shared ReadOnly Property ConnectionString
     Return connection string from web.config file.
  End Property
End Class

Then your DAL methods can access the connection string like this:

Dim conn As New SqlConnection(AppConfiguration.ConnectionString)

If you need to change the connection string, because you want to change the database for example, you do it at exactly one place, the right place: the web.config file.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 16th, 2006, 07:34 PM
Authorized User
 
Join Date: Jan 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again... getting closer!
I run and the browser is blank.
I now do not have any errors but when I run it, The browser is blank.

here is how I have the code now:

Imports System.Data.SqlClient
Imports System.Data

Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    '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
    Dim connectionString As String = "workstation id=DOZER;packet size=4096;integrated security=SSPI;data source=Dozer;" & _
     "persist security info=False;initial catalog=programmersheaven"

    Dim conn As New SqlConnection(connectionString)


    Dim commandString As String = "SELECT " + _
    "artId, title, topic, " + _
    "article.authorId as authorId, " + _
    "name, lines, dateOfPublishing " + _
    "FROM " + _
    "article, author " + _
    "WHERE " + _
    "author.authorId = article.authorId"
    Dim dataAdapter As New SqlDataAdapter(commandString, conn)





    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim ds As New DataSet
        dataAdapter.Fill(ds, "prog")
        Dim dataTable As DataTable = ds.Tables("article, author")



    End Sub

End Class


 
Old January 17th, 2006, 03:58 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Personally, I think this is worse than it was before; first you had all the code in a single method, which means the method serves as a black box. While this can certainly be improved, I think your current set up isn't any better and may even be worse.

For example, it looks and feels odd to have the command string defined at the class level. I am sure your class will do more than just carry out that single select statement so IMO, it's cleaner to define the command text inside the method that executes the command or define it externally.

Anyway, you say the page is blank. But why wouldn't it be? All you do is retrieve some data from the database, and that's it. I don't see any code where you bind this data to controls in the page to display the data i holds....

Quote:
quote:If you're really new at this, I suggest you get a couple of good books to help you get started....
I still suggest you look into this....
If this is really your start in .NET, then this is probably not going to work. You'll go from problem to problem, each time posting here whatever you run in to. While it might work because there are a lot of people here that can help you, it's probably a slow method to learn. A good book about ASP.NET combined with one about databases is certainly going to speed up your learning experience....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
What is <...T> declaration? wrockinator BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 1 November 30th, 2008 12:26 PM
standalone declaration Jay Frank XML 5 September 28th, 2007 08:57 AM
xml declaration chroniclemaster1 XML 0 September 20th, 2007 02:00 PM
Declaration for DuplicateTokenEx BrianWren Pro VB 6 0 April 6th, 2007 03:54 PM
Declaration Difference anubhav.kumar Pro VB.NET 2002/2003 2 July 5th, 2005 10:19 AM





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