Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > Visual Studio 2005
| Search | Today's Posts | Mark Forums Read
Visual Studio 2005 For discussing Visual Studio 2005. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2005 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 March 26th, 2007, 09:49 AM
Registered User
Join Date: Mar 2007
Location: Pretoria, , South Africa.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default ExecuteReader: Connection property has not been in

Hi guys
I hope you guys can help me. This is my first post.
Iam having trouble with this error ExecuteReader: Connection property has not been initialized.

My code is as follows.

 Dim reader As SqlClient.SqlDataReader
        Dim SqlCommand1 As New SqlClient.SqlCommand
        Dim Con As New SqlClient.SqlConnection
        Dim strsql As String
        Dim server As String
        Dim database As String
        Dim user As String
        Dim pass As String

        server = "GREEDY"
        database = "Northwind"
        user = "sa"
        pass = ""
        If Con.State = 1 Then Con.Close()

   Con.ConnectionString() = "Data Source=" & server & ";Initial Catalog=" & database & ";User Id=" & user & ";Password=" & pass


        strsql = "Select customerid,orderid,employeeid From Orders"

        SqlCommand1.CommandText = strsql

        reader = SqlCommand1.ExecuteReader

        'This sets the columns of the ListView to the same column names of the table in the database
        With lstTest
            .Columns.Add(reader.GetName(0), 80, HorizontalAlignment.Center)
            .Columns.Add(reader.GetName(1), 80, HorizontalAlignment.Center)
            .Columns.Add(reader.GetName(2), 80, HorizontalAlignment.Center)
        End With

        'Now scrolling through the DataReader and populating the ListView with the data
        While reader.Read
            Dim ls As New ListViewItem(reader.Item("customerid").ToString()) ' you can also use reader.GetSqlValue(0)
            ls.SubItems.Add(reader.Item("employeeid").ToString ())
        End While


Old March 26th, 2007, 10:03 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

O.o first to answer your question.

You have never supplied the Connection to the SqlCommand so it has no idea what it is supposed to connect to! You need to do something like: SqlCommand1.Connection = con

Also, am I right in assuming that you are fairly new to programming?? The reason I ask is because this code does not lend itself well to reuseability at all since everything is hardcoded.

I would suggest adding a class to your solution that handles all of your database functionality and you just return the necessary objects from that class and work with those objects in your calling class.

Also, you should never never never leave SQL Servers SA account setup with a blank password, that is asking for all sorts of trouble.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
================================================== =========
Why can't Programmers, program??
================================================== =========
Old March 27th, 2007, 01:20 AM
Registered User
Join Date: Mar 2007
Location: Pretoria, , South Africa.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Hi dparsons

I really do appreciate it alot. I sorted out the error. I am fairly new to programming but i really enjoy it so much. Again thank you for the very useful advice.

Old October 14th, 2008, 04:06 AM
Posts: n/a

check this information about executereader()



Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection property has not been initialized sasi.vempalli ASP.NET 1.0 and 1.1 Basics 0 September 19th, 2006 03:03 AM
MySQL V.Net Connection property has not been initi Wilco VB.NET 0 August 21st, 2006 03:02 AM
Specify the connection string property at runtime Lawrence C. Zauberis BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 1 July 7th, 2006 09:40 AM
Problem in Connection Property of Crystal report 9 polravi Pro VB 6 0 February 8th, 2006 09:59 AM
Connection Property:Has not been initialized louie001 ASP.NET 1.0 and 1.1 Basics 1 October 17th, 2005 11:46 PM

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