By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

Using Database Connection

P: n/a
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\sk ipassdirect_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!
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

"Simon Harris" <to*********@makesyoufat.com> wrote in message
news:Ot**************@TK2MSFTNGP10.phx.gbl...
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\sk ipassdirect_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!

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.