Results 1 to 4 of 4

Thread: [RESOLVED] Why cant sqlConnection and sqlDataAdapter variables be global ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    108

    Resolved [RESOLVED] Why cant sqlConnection and sqlDataAdapter variables be global ?

    Hi,

    When I tried to create a global variable of type sqlConnection and sqlDataAdapter, vb 2005 underlines it and says that the "type is not defined".

    The 2 declarations are:

    Dim myConnection As SqlConnection = New SqlConnection(..........)
    Dim myAdapter As SqlDataAdapter = New SqlDataAdapter(......,....)

    When they are added as global variables (outside of any private sub), vb 2005 highlights sqlConnection and sqlDataAdapter as errors. But when they are added inside a private sub procedure, like Private Sub Form1_Load, then there is no error.

    How come they cant be global variables? I need them to be freely accessed by the various Private Sub procedure inside the Form1 public class.

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Why cant sqlConnection and sqlDataAdapter variables be global ?

    Shouldn't be much different in 2005.

    Dim myConnection As New SqlClient.SqlConnection
    Dim myAdapter As New SqlClient.SqlDataAdapter
    VB 2005, Win Xp Pro sp2

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why cant sqlConnection and sqlDataAdapter variables be global ?

    You are mistaken. You must be doing something else that you aren't mentioning, like trying to declare them in a file that hasn't imported the appropriate namespace.
    Attached Images Attached Images  
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    108

    Re: Why cant sqlConnection and sqlDataAdapter variables be global ?

    I did add the system.data and system.data.sqlclient namespaces.

    I've removed the " = new sqlConnection" part and replaced it with dim myconnection as new sqlConnection. It works now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width