Results 1 to 4 of 4

Thread: SQLConnection Problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    Tokyo, Japan
    Posts
    72

    Question SQLConnection Problem

    Hello

    I have written following code for getting connectivity with SQL Server. DB server is on separate pc. I am giving its IP address in connection string.

    Dim myConnectionString As String = "Provider=SQLOLEDB; Data Source=IP Addr of db server; Initial Catalog=DB Name; User Id=UserName; Password=Password;"

    Dim myConnection As New SqlConnection(myConnectionString)
    myConnection.Open()

    Dim myReadQuery As String = "select * from dbAkitaAdmin"

    Dim myCommand As New SqlCommand(myReadQuery)

    Dim adapter As New SqlDataAdapter()

    Dim myDataSet as New DataSet()

    adapter.SelectCommand = myCommand

    adapter.Fill(myDataSet)

    But it is giving me error as follows

    Compiler Error Message: BC30002: Type 'SqlConnection' is not defined.

    Pls suggest the way for this connectivity.

    Also pls kindly expln about usage of SqlCommand, SqlDataAdapter and SqlDataReader classes.

    Thanks
    Mahesh
    Everybody is always learning something new !

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I'm a little surprised that iSystem.Data isn't referenced and imported by default in your project properties, but anyway:

    Use:
    VB Code:
    1. Dim myConnection As New System.Data.SqlClient.SqlConnection

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    Tokyo, Japan
    Posts
    72
    Hello

    You are right, Imports statement must be there. I have added it as follows

    Imports System.Data
    Imports System.Data.SqlClient

    Sub Page_Load(sender As Object, e As EventArgs)

    Dim myConnectionString As String = "Server=IP Addr of DB Server;Database=DB Name;User ID=uname;Password=pwd;Integrated Security=SSPI"
    Dim myConnection As New SqlConnection(myConnectionString)
    myConnection.Open()

    Dim myReadQuery As String = "select * from dbAkitaAdmin"
    Dim myCommand As New SqlCommand(myReadQuery)
    Dim adapter As New SqlDataAdapter()
    Dim myDataSet as New DataSet()
    adapter.SelectCommand = myCommand
    adapter.Fill(myDataSet)

    gridTest.DataSource = dataset
    gridTest.DataBind
    rdrTest.Close
    cmdTest.Dispose
    conTest.Close

    End Sub

    But now it is giving me new error :

    Compiler Error Message: BC30465: 'Imports' statements must precede any declarations.

    Source Error:

    Line 2: <script runat="server">
    Line 3:
    Line 4: Imports System.Data
    Line 5: Imports System.Data.SqlClient
    Line 6:

    Then I have added Option Strict On, before Imports Statement. But still error is displayed.

    Compiler Error Message: BC30627: 'Option' statements must precede any declarations or 'Imports' statements.

    Source Error:

    Line 2: <script runat="server">
    Line 3:
    Line 4: Option Strict On
    Line 5:
    Line 6: Imports System.Data

    Please kindly suggest some solution.

    Thanks
    Mahesh
    Everybody is always learning something new !

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Option strict On must be the first line in your code.
    Imports must follow that and BEFORE any other code.

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