Using Database Connection

S

Simon Harris

Hi All,

I have this in an ASCX file:
<%@ Control Language="vb" %>
<%@import namespace="System.Data.OleDb"%>
<Script runat="Server">
Public Function GetConnection()
Dim Conn as OleDbConnection
Dim ConnStr as String
ConnStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and
Settings\Simon\My
Documents\Adotek\Projects\SkiPassDirect\private\skipassdirect_mainDB.mdb"
'For password protected DB's add "Userid=xxxx;Password=xxxxx" to the above
string
Conn=new OleDbConnection(ConnStr)
end Function
</Script>

I have successfully registered the class and opened the connection like
this:
<%@ Register TagPrefix="SPD" TagName="DBConnection"
src="/assets/classes/database_connection.ascx"%>
<SPD:DBConnection id="DBConnection" runat="Server"/>

Could someone tell me how I then make use of this connection? I have tried
the following:
Dim sqlCmd as New SQLCommand("SELECT CompanyName, ContactTitle, ContactName,
Country FROM Customers", Conn) <-- Errornous Line
DataGrid1.DataSource = sqlCmd.ExecuteReader();
DataGrid1.DataBind();

Which causes an error:
Compiler Error Message: BC30451: Name 'Conn' is not declared. (This refers
to the line marked above)

Also, do I need to close the connection explicitly? (As I did in 'classic'
ASP)

Thanks for your help - I am struggling to find a basic data access tutorial
to get me started, they all seem to assume alot of prior knowledge.

Simon.

--
-
* Please reply to group for the benefit of all
* Found the answer to your own question? Post it!
* Get a useful reply to one of your posts?...post an answer to another one
* Search first, post later : http://www.google.co.uk/groups
* Want my email address? Ask me in a post...Cos2MuchSpamMakesUFat!
 
A

avnrao

i dont think it is a good way of using user control for creating
connections.
rather you can create a class library which returns connection object and
use it all across your projects.

Av.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top