Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error on connecting to sql server - but not to access db

Status
Not open for further replies.

wvdba

IS-IT--Management
Jun 3, 2008
465
US
hi.
i'm getting an error when connecting to sql server db but not when connecting to access db.
here's my code:
Code:
<%@ Import Namespace=&quot;System.Data.OleDb&quot; %>
<script runat="server">
sub Page_Load
Dim dbconn, sql, dbcomm, dbread
dbconn = "Provider=SQLNCLI10;Server=A20ootb05l01457;Database=master;Trusted_Connection=yes;"
        dbconn.Open()
        sql = "SELECT * FROM persons"
        dbcomm = New OleDbCommand(sql, dbconn)
        dbread = dbcomm.ExecuteReader()
        rep_info.DataSource = dbread
        rep_info.DataBind()
        dbread.Close()
        dbconn.Close()
    End Sub 
</script>
the error occurs on line 13:
line 13: dbconn.open()
ther error is: Public member 'Open' on type 'String' not found
any ideas?
thanks.
 
First, you should be using the code-behind page for you code and not putting in the HTML, that is classic ASP code.
Second, use the ADO.NET objects for accesing the database, not the old OLE objects.

There are plenty of examples on line. As for the connection to the DB, check out how to properly format the connnection string here:
 
also, you should not be connecting to the master database.

and there is never a need to pose the server/database name publicly. assistance can be provided without posting private information.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
jmeckley,
thanks for the reply. that "server=" is on my local machine with instance of sql. no such server exists per se.

i found out what it should be, as you mentioned, there are many examples of it online instead of OleDb, i used sqlclient:
Code:
<%@ Import Namespace="System.Data.SqlClient" %>

and then this one for connection:
Code:
sub Page_Load
 
        Dim dbconn, sql, dbcomm, dbread 
        dbconn = New SqlConnection("Server=A20ootb05l01457;Database=master;Trusted_Connection=yes;")
        dbconn.Open()
        sql = "SELECT * FROM persons order by last_name, first_name"
        dbcomm = New SqlCommand(sql, dbconn)
        dbread = dbcomm.ExecuteReader()
        rep_info.DataSource = dbread
        rep_info.DataBind()
        dbread.Close()
        dbconn.Close()

    End Sub
it worked ok.
thanks again.
 
The actual error you were receiving was to do with how you declared your objects, as you didn't give them a type so it tried to guess what type of object you wanted (and failed as it guessed it should be a string).

You should have (and still should even in your new code) state exactly what type they are e.g.
Code:
Dim dbconn As SqlConnection
Dim sql As String

etc...


Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top