Ive created an database with MS Access... but for the life of me I can not remember how to connect to the database - AT ALL(by using code)
If I remember correctly I need to import the system.data.oledb .... but I can not remember how to use the imports statement and where to put it ect..... I havent used VS.net 2005 in quite some time....
Please could someone assist....
Then my next question is - Once ive connected to the database..... how to I COUNT the number of "Clients" i have in my Clients table...... Do I use the select query eg. SELECT COUNT(Clientid) as ClientNum, ..............
Ive created an database with MS Access... but for the life of me I can not remember how to connect to the database - AT ALL(by using code)
If I remember correctly I need to import the system.data.oledb .... but I can not remember how to use the imports statement and where to put it ect..... I havent used VS.net 2005 in quite some time....
Import System.Data.OleDb goes at the top of your page.
AxleWack
Then my next question is - Once ive connected to the database..... how to I COUNT the number of "Clients" i have in my Clients table...... Do I use the select query eg. SELECT COUNT(Clientid) as ClientNum, ..............
Yes. Then use Command.ExecuteScalar() to obtain the value from the database.
1 more thing. I do not want to connect to my database with a wizard - I would like to do it in my codebehind(Button click event ect) But can not remember how to do it
Ive created my connectionstring and put it into my web.config, and have put in the oledb namespace..... Im connecting through an Access Database.
If I for example want to display fields in a dataview(Through the code) what are the steps? Please could someone help - as Ive searched all over the internet and cannot find any sample code.
This is the coding I got to ... but my Detailsview is now showing any data :(
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
Dim connString As String = ConfigurationManager.AppSettings("SalesConn")
Using myConnection As New OleDbConnection(connString)
Dim Str As String = "SELECT * FROM Client"
Dim myCommand As New OleDbCommand(Str, myConnection)
Dim myDataSet As New Data.DataSet
Dim myAdapter As New OleDbDataAdapter(myCommand)
myAdapter.Fill(myDataSet)
DetailsView1.DataSource = myDataSet
DetailsView1.DataBind()
myConnection.Close()
End Using
End If
You forgot to add Handles Me.Load after Protected Sub Page_Load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Not Page.IsPostBack Then
Dim connString As String = ConfigurationManager.AppSettings("SalesConn")
Using myConnection As New OleDbConnection(connString)
Dim Str As String = "SELECT * FROM Client"
Dim myCommand As New OleDbCommand(Str, myConnection)
Dim myDataSet As New Data.DataSet
Dim myAdapter As New OleDbDataAdapter(myCommand)
myAdapter.Fill(myDataSet)
DetailsView1.DataSource = myDataSet
DetailsView1.DataBind()
''''myConnection.Close() - Remove - not needed
End Using
End If
End Sub
When Im using the Datasource through the wizzard I do yes(Then it fills the DetailsView) - But when Through code it should be... should it?(Doesnt Fill)
Ive done everything I can possibly think is needed to fill the dataset..... but still there is no information.
AxleWack
Member
370 Points
649 Posts
Accessing Data via MS Access
Nov 26, 2007 07:04 AM|LINK
Hi Really stupid Question :(
Ive created an database with MS Access... but for the life of me I can not remember how to connect to the database - AT ALL(by using code)
If I remember correctly I need to import the system.data.oledb .... but I can not remember how to use the imports statement and where to put it ect..... I havent used VS.net 2005 in quite some time....
Please could someone assist....
Then my next question is - Once ive connected to the database..... how to I COUNT the number of "Clients" i have in my Clients table...... Do I use the select query eg. SELECT COUNT(Clientid) as ClientNum, ..............
thank you for any help :)
jeroenm
Contributor
2812 Points
502 Posts
Re: Accessing Data via MS Access
Nov 26, 2007 09:14 AM|LINK
Hello AxleWack,
The following article on MSDN describes how to connect and retrieve data with ADO.NET: http://msdn2.microsoft.com/en-us/library/ms254937(VS.80).aspx
Jeroen Molenaar.
Mikesdotnett...
All-Star
154840 Points
19854 Posts
Moderator
MVP
Re: Accessing Data via MS Access
Nov 26, 2007 10:07 AM|LINK
http:www.mikesdotnetting.com/Article.aspx?Article=26
Import System.Data.OleDb goes at the top of your page.
Yes. Then use Command.ExecuteScalar() to obtain the value from the database.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
AxleWack
Member
370 Points
649 Posts
Re: Accessing Data via MS Access
Nov 26, 2007 12:09 PM|LINK
Hi - Got that sorted out thank you.
1 more thing. I do not want to connect to my database with a wizard - I would like to do it in my codebehind(Button click event ect) But can not remember how to do it
Ive created my connectionstring and put it into my web.config, and have put in the oledb namespace..... Im connecting through an Access Database.
If I for example want to display fields in a dataview(Through the code) what are the steps? Please could someone help - as Ive searched all over the internet and cannot find any sample code.
AxleWack
Member
370 Points
649 Posts
Re: Accessing Data via MS Access
Nov 26, 2007 01:02 PM|LINK
This is the coding I got to ... but my Detailsview is now showing any data :(
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
Dim connString As String = ConfigurationManager.AppSettings("SalesConn")
Using myConnection As New OleDbConnection(connString)
Dim Str As String = "SELECT * FROM Client"
Dim myCommand As New OleDbCommand(Str, myConnection)
Dim myDataSet As New Data.DataSet
Dim myAdapter As New OleDbDataAdapter(myCommand)
myAdapter.Fill(myDataSet)
DetailsView1.DataSource = myDataSet
DetailsView1.DataBind()
myConnection.Close()
End Using
End If
Mikesdotnett...
All-Star
154840 Points
19854 Posts
Moderator
MVP
Re: Accessing Data via MS Access
Nov 26, 2007 01:17 PM|LINK
You forgot to add Handles Me.Load after Protected Sub Page_Load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim connString As String = ConfigurationManager.AppSettings("SalesConn")
Using myConnection As New OleDbConnection(connString)
Dim Str As String = "SELECT * FROM Client"
Dim myCommand As New OleDbCommand(Str, myConnection)
Dim myDataSet As New Data.DataSet
Dim myAdapter As New OleDbDataAdapter(myCommand)
myAdapter.Fill(myDataSet)
DetailsView1.DataSource = myDataSet
DetailsView1.DataBind()
''''myConnection.Close() - Remove - not needed
End Using
End If
End Sub
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
AxleWack
Member
370 Points
649 Posts
Re: Accessing Data via MS Access
Nov 26, 2007 01:37 PM|LINK
I tried the exact coding you gave - but the Detailsview does not load...... Do you think this has anything to do with my connection string?
It is as follows:
<appSettings>
<!-- User application and configured property settings go here.-->
<!-- Example: <add key="settingName" value="settingValue"/> -->
<add key="SalesConn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\cheyne\Desktop\Sales\Sales05\Sales.mdb;Persist Security Info=False" />
</appSettings>
1. Im using an access DB
2. Using VS.NET 2005
jeroenm
Contributor
2812 Points
502 Posts
Re: Accessing Data via MS Access
Nov 26, 2007 01:49 PM|LINK
Can you add the declaration of the DetailsView to this thread?
Jeroen Molenaar.
Mikesdotnett...
All-Star
154840 Points
19854 Posts
Moderator
MVP
Re: Accessing Data via MS Access
Nov 26, 2007 02:02 PM|LINK
Have you got a <Fields> declaration in your DetailsView?
eg <Fields>
<asp:BoundField ..../>
<asp:BoundField.../>
etc?
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
AxleWack
Member
370 Points
649 Posts
Re: Accessing Data via MS Access
Nov 27, 2007 05:48 AM|LINK
When Im using the Datasource through the wizzard I do yes(Then it fills the DetailsView) - But when Through code it should be... should it?(Doesnt Fill)
Ive done everything I can possibly think is needed to fill the dataset..... but still there is no information.