PDA

Click to See Complete Forum and Search --> : SQLConnection Problem


mahesh_575
Mar 24th, 2004, 06:06 AM
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

nemaroller
Mar 24th, 2004, 09:46 PM
I'm a little surprised that iSystem.Data isn't referenced and imported by default in your project properties, but anyway:

Use:

Dim myConnection As New System.Data.SqlClient.SqlConnection

mahesh_575
Mar 25th, 2004, 04:39 AM
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

nemaroller
Mar 25th, 2004, 04:02 PM
Option strict On must be the first line in your code.
Imports must follow that and BEFORE any other code.