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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connect to Access database using ASP.NET

Status
Not open for further replies.

martinb7

Programmer
Jan 5, 2003
235
0
0
GB
Hi, i was wondering, how do you connect to an access database in asp.net?

is it the same way as in asp or different?

could someone give me the code pls ^_^

Thanx

Martin
 
Here's a simple drilling of an access database using a reader:

'************ Get Site Records ****************

'get site information...
Dim dbconnSiteRecs As OleDbConnection = New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & Server.MapPath(".\fpdb\Sites.mdb;"))
Dim DBCommSites = New OleDbCommand _
("SELECT HUC11, County, Description, Latitude, Longitude, Waterbody_Name, ChemCt FROM WebMasterSites WHERE AwwSiteCode ='" & Request.QueryString("AwwSiteCode") & "'", dbconnSiteRecs)
Dim Myreader As OleDbDataReader
Try
dbconnSiteRecs.Open()
Myreader=DBCommSites.ExecuteReader()
While Myreader.Read()
lblHUC11.Text = Myreader("HUC11")
lblCounty.Text = Myreader("County")
lblSite.Text = Myreader("Description")
lblWaterbodyName.Text = Myreader("Waterbody_Name")
lblLatitude.Text = Myreader("Latitude")
lblLongitude.Text = Myreader("Longitude")
lblChemCt.Text = Myreader("ChemCt")
End While
Myreader.Close()

Catch err As Exception
lblResults.Text = "Error reading list of names. "
lblResults.Text &= err.Message
Finally
If (Not dbconnSiteRecs Is Nothing) Then
dbconnSiteRecs.Close()
End If
End Try
 
There's a problem with your code: i have highlighted it in red!

Compiler Error Message: BC30002: Type 'OleDbConnection' is not defined.


'************ Get Site Records ****************

'get site information...
Dim dbconnSiteRecs As OleDbConnection = New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & Server.MapPath(".\fpdb\Sites.mdb;"))
Dim DBCommSites = New OleDbCommand _
("SELECT HUC11, County, Description, Latitude, Longitude, Waterbody_Name, ChemCt FROM WebMasterSites WHERE AwwSiteCode ='" & Request.QueryString("AwwSiteCode") & "'", dbconnSiteRecs)
Dim Myreader As OleDbDataReader
Try
dbconnSiteRecs.Open()
Myreader=DBCommSites.ExecuteReader()
While Myreader.Read()
lblHUC11.Text = Myreader("HUC11")
lblCounty.Text = Myreader("County")
lblSite.Text = Myreader("Description")
lblWaterbodyName.Text = Myreader("Waterbody_Name")
lblLatitude.Text = Myreader("Latitude")
lblLongitude.Text = Myreader("Longitude")
lblChemCt.Text = Myreader("ChemCt")
End While
Myreader.Close()

Catch err As Exception
lblResults.Text = "Error reading list of names. "
lblResults.Text &= err.Message
Finally
If (Not dbconnSiteRecs Is Nothing) Then
dbconnSiteRecs.Close()
End If
End Try

I changed all the code so it matched my database;

version info if you need it:

Version Information: Microsoft .NET Framework Version:1.0.3705.288; ASP.NET Version:1.0.3705.288

is the url

Any ideas anybody??

Thanx

Martin
 
You probably left out the OLEDb reference, q.v.,

<%@ Page Language=&quot;VB&quot; Debug=&quot;true&quot;%>
<%@Import Namespace = &quot;Microsoft.VisualBasic&quot;%>
<%@Import Namespace = &quot;System&quot;%>
<%@Import Namespace = &quot;System.Web&quot;%>
<%@Import Namespace = &quot;System.Web.UI&quot;%>
<%@Import Namespace = &quot;System.Web.UI.WebControls&quot;%>
<%@Import Namespace = &quot;System.Web.UI.HtmlControls&quot;%>
<%@Import Namespace = &quot;System.Data&quot;%>
<%@Import Namespace = &quot;System.Data.OleDb&quot;%>

...the last one...
 

Hi there...

See if your webform has a label called lblID else insert another label a test with it.

Good Luck!


 
the labels were pretty much extra, no need, they must however physcially exists, here they are invisible on the form, and serve to catch the values coming out of the table. I posted thinking only the reference connection string was all that was needed.
 
Martin, lost track of exactly what page that came out of. The asp labels are only there to &quot;catch&quot; the reader values coming out the table. What I do in cases like this is to create asp labels and set their Visible property to &quot;false&quot; and then use them to store information between posts, etc, and prior to sending next Querystring.

Here is a complete page, with the Try...Catch error trapping during db connection: Also a couple of other things going on - send this page to the printer, and the rest should jump out at ya - nuthin but VB and a little java --

<%@ Page Language=&quot;VB&quot; Debug=&quot;false&quot; %>
<%@Import Namespace = &quot;Microsoft.VisualBasic&quot;%>
<%@Import Namespace = &quot;System&quot;%>
<%@Import Namespace = &quot;System.Web&quot;%>
<%@Import Namespace = &quot;System.Web.UI&quot;%>
<%@Import Namespace = &quot;System.Web.UI.WebControls&quot;%>
<%@Import Namespace = &quot;System.Web.UI.HtmlControls&quot;%>
<%@Import Namespace = &quot;System.Data&quot;%>
<%@Import Namespace = &quot;System.Data.OleDb&quot;%>
<script runat=&quot;server&quot;>
Public strPath, strPaths As String
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
radCounty.Attributes.Add(&quot;onMouseOut&quot;, &quot;RetImage()&quot;)
radWShed.Attributes.Add(&quot;onMouseOut&quot;, &quot;RetImage()&quot;)
radWbody.Attributes.Add(&quot;onMouseOut&quot;, &quot;RetImage()&quot;)
radGIS.Attributes.Add(&quot;onMouseOut&quot;, &quot;RetImage()&quot;)
radZIP.Attributes.Add(&quot;onMouseOut&quot;, &quot;RetImage()&quot;)
radGroup.Attributes.Add(&quot;onMouseOut&quot;, &quot;RetImage()&quot;)
radTSR.Attributes.Add(&quot;onMouseOut&quot;, &quot;RetImage()&quot;)
radCounty.Attributes.Add(&quot;onMouseOver&quot;, &quot;switchpic('images/Counties.gif')&quot;)
radWShed.Attributes.Add(&quot;onMouseOver&quot;, &quot;switchpic('images/WShed.gif')&quot;)
radWbody.Attributes.Add(&quot;onMouseOver&quot;, &quot;switchpic('images/Wbody.gif')&quot;)
radZIP.Attributes.Add(&quot;onMouseOver&quot;, &quot;switchpic('images/ZIP.gif')&quot;)
radGIS.Attributes.Add(&quot;onMouseOver&quot;, &quot;switchpic('images/GIS.gif')&quot;)
radGroup.Attributes.Add(&quot;onMouseOver&quot;, &quot;switchpic('images/Group.gif')&quot;)
radTSR.Attributes.Add(&quot;onMouseOver&quot;, &quot;switchpic('images/TSR.gif')&quot;)
End If
End Sub

Sub GetCounties()
'open database...
Dim cmdSelect As OLEDbCommand
Dim dbconn As OleDbConnection = New OleDbConnection( _
&quot;Provider=Microsoft.Jet.OLEDB.4.0; &quot; & _
&quot;Data Source=&quot; & Server.MapPath(&quot;.\fpdb\Aerials.mdb;&quot;))
cmdSelect = New OLEDbCommand(&quot;SELECT DISTINCT County FROM tblFieldData ORDER BY County&quot;, dbconn)
Try
dbconn.Open()
ddCty.DataSource = cmdSelect.ExecuteReader()
ddCty.DataBind()
dbconn.Close()
Catch err As Exception
lblResults.Text = &quot;Error:&quot;
lblResults.Text &= err.Message
Finally
If (Not dbconn Is Nothing) Then
dbconn.Close()
End If
End Try
End Sub

Sub GetZipCodes()
Dim dbconnSiteRecs As OleDbConnection = New OleDbConnection( _
&quot;Provider=Microsoft.Jet.OLEDB.4.0; &quot; & _
&quot;Data Source=&quot; & Server.MapPath(&quot;.\fpdb\Sites.mdb;&quot;))
Try
dbconnSiteRecs.Open()
Dim DBCommand = New OleDbCommand (&quot;SELECT DISTINCT tblZipCodes.City, tblZipCodes.County, WebMasterSites.ZipCode & ' ' & tblZipCodes.County & ' Co., ' & tblZipCodes.City & ', ' & tblZipCodes.State As ZipInfo FROM WebMasterSites LEFT JOIN tblZipCodes ON WebMasterSites.ZipCode = tblZipCodes.Zip_Code WHERE (((WebMasterSites.ZipCode)<>'NV')) ORDER BY tblZipCodes.County, tblZipCodes.City&quot;, dbconnSiteRecs)
Dim reader As OleDbDataReader
ddCty.DataSource = DBCommand.ExecuteReader()
ddCty.DataTextField = &quot;ZipInfo&quot;
ddCty.DataBind()
dbconnSiteRecs.Close()
Catch err As Exception
lblResults.Text = &quot;Error:&quot;
lblResults.Text &= err.Message
Finally
If (Not dbconnSiteRecs Is Nothing) Then
dbconnSiteRecs.Close()
End If
End Try
ddCty.SelectedItem.Text = &quot;Select Zip Code...&quot;
End Sub

Sub GetCountySites (sender As object, e As EventArgs)
If pnImage.Visible = True Then
pnImage.Visible = False
btnPhotos.Visible = False
SitePanel.Visible = False
End If

dlSites.Items.Clear()
Dim cmdSelect As OLEDbCommand
Dim dbconn As OleDbConnection = New OleDbConnection( _
&quot;Provider=Microsoft.Jet.OLEDB.4.0; &quot; & _
&quot;Data Source=&quot; & Server.MapPath(&quot;.\fpdb\Aerials.mdb;&quot;))
cmdSelect = New OleDbCommand (&quot;SELECT Site_ID, WebID FROM tblFieldData WHERE County ='&quot; & ddCty.SelectedItem.Text & &quot;'&quot; & &quot;ORDER BY SITE_ID&quot;, dbconn)
Dim reader As OleDbDataReader
Try
dbconn.Open()
reader = cmdSelect.ExecuteReader()
Do While reader.Read()
Dim NewItem As New ListItem()
NewItem.Value = reader(&quot;Site_ID&quot;)
NewItem.Text = reader(&quot;WebID&quot;)
dlSites.Items.Add(NewItem)
Loop
reader.Close()
Catch err As Exception
lblResults.Text = &quot;Error:&quot;
lblResults.Text &= err.Message
Finally
If (Not dbconn Is Nothing) Then
dbconn.Close()
End If
End Try
End Sub

Private Sub btnSites_Click(sender As object, e As EventArgs)
'Make sure a site is selected...
If dlSites.SelectedIndex <> -1 Then
lblResults.Text = &quot;&quot;
SitePanel.Visible = True
'get site details...
Dim cmdSelect As OLEDbCommand
Dim dbconn As OleDbConnection = New OleDbConnection( _
&quot;Provider=Microsoft.Jet.OLEDB.4.0; &quot; & _
&quot;Data Source=&quot; & Server.MapPath(&quot;.\fpdb\Aerials.mdb;&quot;))
cmdSelect = New OleDbCommand (&quot;SELECT WebID, NumPhotos, Type, Activity, Latitude, Longitude, PPP, River_Basin, Waterbody, Description, Notes FROM tblFieldData WHERE Site_ID ='&quot; & dlSites.SelectedItem.Value & &quot;'&quot;, dbconn)
Dim reader As OleDbDataReader
Try
dbconn.Open()
reader = cmdSelect.ExecuteReader()
While reader.Read()
lblWebID.Text = Left(reader(&quot;WebID&quot;),3)
lblNumPhotos.Text = reader(&quot;NumPhotos&quot;)
lblType.Text = reader(&quot;Type&quot;)
lblActivity.Text = reader(&quot;Activity&quot;)
lblLatitude.Text = reader(&quot;Latitude&quot;)
lblLongitude.Text = reader(&quot;Longitude&quot;)
lblPPP.Text = reader(&quot;PPP&quot;)
lblRiverBasin.Text = reader(&quot;River_Basin&quot;)
lblWaterbody.Text = reader(&quot;Waterbody&quot;)
lblDescription.Text = reader(&quot;Description&quot;)
lblNotes.Text = reader(&quot;Notes&quot;)
End While
reader.Close()
Catch err As Exception
lblResults.Text = &quot;Error:&quot;
lblResults.Text &= err.Message
Finally
If (Not dbconn Is Nothing) Then
dbconn.Close()
End If
End Try
Else
lblResults.text = &quot;Choose a site!&quot;
Exit Sub
End If
Dim strSite As String = Right(dlSites.SelectedItem.Value, 3)
Dim intSite As Integer = CInt(strSite)
lblintID.Text = intSite
Dim s As String
Dim i As Integer = 0
strPath = &quot;.\AerialPhotos\&quot; & ddCty.SelectedItem.Text & &quot;\Site&quot; & intSite & &quot;\File0001.jpg&quot;
Dim IMAGE_DIRECTORY As String = &quot;.\AerialPhotos\&quot; & ddCty.SelectedItem.Text & &quot;\Site&quot; & intSite & &quot;\&quot;
For Each s In Directory.GetFiles(Server.MapPath(IMAGE_DIRECTORY), &quot;*s.jpg&quot;)
i = i + 1
next
strPaths = &quot;.\AerialPhotos\&quot; & ddCty.SelectedItem.Text & &quot;\Site&quot; & intSite & &quot;\File0001s.jpg&quot;
if i = 0 then
GenerateThumbnail(strPath, strPaths)
end if
btnPhotos.Visible = True
pnImage.Visible = True
End Sub

Sub GenerateThumbnail(strPath As String, strPaths As String)
Dim g, g2 As System.Drawing.Image
g = System.Drawing.Image.FromFile(Server.MapPath(strPath))
Dim imgw As Integer = g.Width
Dim imgh As Integer = g.height
If imgw >= imgh Then 'horiz...
imgw = 150
imgh = 99
Else 'vertical...
imgW = 99
imgH = 150
End If
g2 = g.GetThumbnailImage(imgw, imgh, Nothing, IntPtr.Zero)
g2.Save(Server.MapPath(strPaths))
g2.Dispose()
End Sub

Private Sub btnPhoto_Click(sender As object, e As EventArgs)
Response.Redirect(&quot;ForSites.aspx?County=&quot; & ddCty.SelectedItem.Text & &quot;&ID=&quot; & lblintID.Text & &quot;&SiteID=&quot; & dlSites.SelectedItem.Value & &quot;'&quot;)
End Sub

</script>
<html>
<HEAD>
<title>Search AWW Photo Gallery</title>
Code:
<Script type=&quot;text/JavaScript&quot;>
  function switchpic(imgsrc){
    document.images['first'].src=imgsrc;
  }
  function RetImage() { 
    document.images['first'].src = &quot;images/State.gif&quot;;
  }
  function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf(&quot;#&quot;)!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
  }
</script>
</HEAD>
<body background=&quot;images/peach.jpg&quot; onLoad=&quot;MM_preloadImages('images/State.gif','images/ZIP.gif','images/Group.gif','images/TSR.gif','images/Counties.gif','images/GIS.gif','images/Wbody.gif','images/WShed.gif')&quot;>
<form id=&quot;Form1&quot; runat=&quot;server&quot;>
  <table border=&quot;1&quot; cellpadding=&quot;1&quot; cellspacing=&quot;1&quot; width=&quot;690&quot; height=&quot;445&quot;>
    <tr>
      <td width=&quot;310&quot; height=&quot;445&quot; align=&quot;middle&quot; Valign=&quot;center&quot;>
      <p></p>
      <p></p>
      <p></p>
      <p>
      <asp:Label id=&quot;lblOption&quot; runat=&quot;server&quot; Forecolor=&quot;Navy&quot; Font-bold=&quot;true&quot; Visible=&quot;false&quot;/></p>
      <p>
      <asp:DropDownList id=&quot;ddCty&quot; runat=&quot;server&quot; DataTextField = &quot;County&quot; width=&quot;130px&quot; Visible=&quot;false&quot;/>
       <asp:Button id=&quot;btnGo&quot; runat=&quot;server&quot; Type=&quot;submit&quot; Text=&quot;Go!&quot; Forecolor=&quot;red&quot; Visible=&quot;false&quot;/></p>
      <p>
      <asp:Label id=&quot;lblSite&quot; runat=&quot;server&quot; Forecolor=&quot;Navy&quot; Text=&quot;Search photo gallery by...&quot; Font-bold=&quot;true&quot;/></p>
      <p>
      <asp:Listbox id=dlSites runat=&quot;server&quot; width=&quot;260px&quot; height=&quot;240px&quot; Visible=&quot;false&quot;/>
      <p align=&quot;left&quot;>
           <asp:Label id=&quot;lblAlabama&quot; runat=&quot;server&quot; Width=&quot;110px&quot; GroupName=&quot;OptSite&quot; Forecolor=&quot;Navy&quot; Text=&quot; County&quot;/><br>
           <asp:Label id=&quot;lblCahaba&quot; runat=&quot;server&quot; Width=&quot;110px&quot; GroupName=&quot;OptSite&quot; Forecolor=&quot;Navy&quot; Text=&quot; Watershed&quot;/><br>
           <asp:Label id=&quot;lblCoastal&quot; runat=&quot;server&quot; Width=&quot;110px&quot; GroupName=&quot;OptSite&quot; Forecolor=&quot;Navy&quot; Text=&quot; Waterbody&quot;/><br>
           <asp:Label id=&quot;lblCoosa&quot; runat=&quot;server&quot; Width=&quot;110px&quot; GroupName=&quot;OptSite&quot; Forecolor=&quot;Navy&quot; Text=&quot; Zip Code&quot;/><br>
           <asp:Label id=&quot;lblTallapoosa&quot; runat=&quot;server&quot; Width=&quot;110px&quot; GroupName=&quot;OptSite&quot; Forecolor=&quot;Navy&quot; Text=&quot; GIS radius&quot;/><br>
           <asp:Label id=&quot;lblTennessee&quot; runat=&quot;server&quot; Width=&quot;110px&quot; GroupName=&quot;OptSite&quot; Forecolor=&quot;Navy&quot; Text=&quot; AWW Site&quot;/><br>
           <asp:Label id=&quot;lblWarrior&quot; runat=&quot;server&quot; Width=&quot;170px&quot; GroupName=&quot;OptSite&quot; Forecolor=&quot;Navy&quot; Text=&quot; Town-Sec-Range&quot;/></p>
      <p align=&quot;center&quot;>
      <asp:Button id=&quot;btnOpt&quot; runat=&quot;server&quot; Type=&quot;submit&quot; Text=&quot;Go!&quot; Forecolor=&quot;red&quot; width=&quot;40px&quot;/></p>
      </p>
      <br>
      <asp:Button id=&quot;btnSites&quot; runat=&quot;server&quot; Type=&quot;submit&quot; OnClick=&quot;btnSites_Click&quot; Text=&quot;Select&quot; Forecolor=&quot;red&quot; width=&quot;130px&quot; Visible=&quot;false&quot;/>
      <p><a href=&quot;Index.aspx&quot;>Data Forum</a><br>
      <asp:Label id=&quot;lblResults&quot; Font-Bold=&quot;true&quot; runat=&quot;server&quot; ForeColor=&quot;Red&quot;/></p></td>
      <td Valign=&quot;bottom&quot; align=&quot;center&quot;>
      <p>
      <img name=&quot;first&quot; border=&quot;0&quot; src=&quot;images/State.gif&quot; width=&quot;304&quot; height=&quot;407&quot;></p>
      <asp:Panel id=&quot;pnImage&quot; runat=&quot;server&quot; Visible=&quot;False&quot;>
      <p></p>
      <p></p>
      <img border=&quot;2px border-color=&quot;#0000FF&quot; src=&quot;<%=strPaths%>&quot;>
      </asp:Panel>
      <p></p>
      <p></p>
      <asp:Button id=&quot;btnPhotos&quot; OnClick=&quot;btnPhoto_Click&quot; runat=&quot;server&quot; type=&quot;submit&quot; Text=&quot;Get Photos&quot; ForeColor=&quot;Red&quot; Visible=&quot;False&quot;/></p>
      <p align=&quot;center&quot;>Coming Soon!</p>     
     </td>     
    </tr>
   </table>  
   <asp:Textbox id=&quot;Sites&quot; runat=&quot;server&quot; Visible=&quot;false&quot;/>
   <asp:Label id=&quot;lblintID&quot; runat=&quot;server&quot; Visible=&quot;false&quot;/>
 </form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top