PDA

View Full Version : trouble talking to ms access database in .net


chris_angell
01-28-2004, 05:05 PM
hello. I have been trying to work out how to talk to a simple ms access database in asp.net.. in asp it is pretty simple.. but in asp.net I can't work it out...

I am having trouble just talking to the database less alone trying to write to it and display info on my page.... if you could give me some advice on what I am doing wrong I would appriciate it very much ...

here is the code I am using

<%@ Page language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

vb sub

Sub Page_Load(Sender as Object, E as EventArgs)
Dim oConn As OleDbConnection
Dim oComm As OleDbDataAdapter
Dim sConn As String
Dim sComm As String
Dim oDataSet As New DataSet

sConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
sConn += "Data Source=C:\Inetpub\wwwroot\ImagePhoto\db\russ.mdb;"
sConn += "Persist Security Info=False"

sComm = "SELECT ImageTbl.ImageName, "
sComm += "Products.ImageAlt, "
sComm += "FROM ImageTbl"

oConn = New OleDbConnection(sConn)
oComm = New OleDbDataAdapter(sComm, oConn)

oComm.Fill(oDataSet, "ImageTbl")

oGrid.DataSource=oDataSet.Tables("ImageTbl").DefaultView
oGrid.DataBind()

End Sub


aspx page


<body>
<asp:DataGrid id="oGrid" runat="server" />
</body>
</html>


many thanks chris
:eek:

Roelf
01-29-2004, 11:28 AM
this works for me"

<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

Sub Page_Load(Sender As Object, E As EventArgs)

' TODO: Update the ConnectionString and CommandText values for your application
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\db1.mdb;"
ConnectionString += "Persist Security Info=False"
Dim CommandText As String = "select * from TableName"

Dim myConnection As New OleDbConnection(ConnectionString)
Dim myCommand As New OleDbCommand(CommandText, myConnection)

myConnection.Open()

DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
DataGrid1.DataBind()

End Sub

</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>Simple Data Report
</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="DataGrid1" runat="server">
</asp:datagrid>
</form>
</body>
</html>

chris_angell
01-29-2004, 03:45 PM
I will give it a go.. thanks for the code :)

chris_angell
01-29-2004, 03:53 PM
brilliant, it works.. I have been trying to long to make it work :)

thanks

now I have this I have another question .. in good old normal asp you could open the record set like so.. see below

ie

<% rst.open
if not(rst.eof) then
rst.movefirst%>

then take values out like
<%=rst("ImageName")%>

then close the record set

<%end if
rst.close %>


I am confused at how I could do this in asp.net....

any ideas.. or any ideas how to take simple value from the database ie and have more control would bew much appriciated

<%=rst("ImageName")%>


????? hmmmmmmmmm

allida77
01-29-2004, 03:57 PM
google on DataReaders or Datasets

angiras
01-29-2004, 04:18 PM
Dim __command As OleDbCommand= New OleDbCommand(__Query, __connexion)

Dim _reader As OleDbDataReader = __command.ExecuteReader(CommandBehavior.CloseConnection)



If _reader.HasRows Then
While _reader.Read()
_value = _reader("ImageName").ToString
End While
End If

_reader.Close()

chris_angell
01-29-2004, 04:28 PM
sounds fun.."DataReaders or Datasets" :) thanks for your help

chris_angell
01-29-2004, 04:43 PM
another question ????

do I add information in the same way ????

sorry I keep coming up with questions.. I am reading about datasets now... but if someone could aid me on adding records that would be nice :)

thanks again

angiras
01-29-2004, 08:59 PM
you don't need a dataset (it's heavy) for one table or just for reading datas
you need a dataset for following reasons :
1) you are too lazy to use something else
2) you want to store datas and update , insert, delete them deconnected from database
3) you have to display datas from many tables

otherwise it's better to use a datatable or if you have just to read : a datareader

so to add datatas in the same way you cannot use a datareader

you just use a OleDbCommand to execute query