need help converting an ASP example into .NET

2

2obvious

Imagine I have a database with four columns:
"ID" (the key)
"src"
"alt" and
"citation"

I've got images stored on a server, and I use the below ASP example to
pull up different images. (In the example, it only pulls up an image
with an ID of 2.)

<%@ Language = VBscript%>
<% Option Explicit %>
<%
Dim ID
ID = 2
%>
<%
Dim conn, strConnect

'Set connection string variable
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Server.MapPath("pix.mdb")


'Set Connection Properties and Execute Connection
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = strConnect
conn.Open


Dim strSQL, rs

strSQL = "SELECT * " _
& "FROM Images " _
& "WHERE imgID=" & ID

Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQL, conn

rs.Close
conn.Close
rs = Nothing
conn = Nothing
%>
<HTML><BODY>
<img src="<%=rs("src")%>.jpg" alt="<%=rs("alt")%>">
<p>
citation: <%=rs("citation")%>
</p>
</BODY></HTML>

I'm trying to make the leap to .NET, but I'm having difficulty finding
an example like this to study. All ADO.NET I've seen today deal with
DataSet spreadsheets and Web Controls that would otherwise be handled
in ASP with loops.

Here's what I've managed to cobble thus far:

<%@ page language="vb" runat="server" debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">
Sub Page_Load()

Dim ID As Integer = 2


Dim conn As OleDbConnection

conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
& "data source=" & server.mappath("pix.mdb"))
conn.Open


Dim cmd As OleDbCommand
Dim dr As OleDbDataReader

Dim strSQL As String

strSQL = "SELECT * " _
& "FROM Images " _
& "WHERE imgID=" & ID

cmd = New OleDbCommand(strSQL,conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

image1.Src = rs["src"] & ".jpg"
image1.Alt = rs["alt"]

label1.Text = rs["citation"]

dr.Close
End Sub
</script>
<html><body>
<img id="image1" runat="server" />
<p>
citation: <asp:Label id="label1" runat="server" />
</p>
</body></html>

Clearly, my understanding of .NET starts to fall apart when it comes
to extracting data indexed by column names /and/ the properties (or
objects, even) that I need to run at the server to get what I want.
Please take a look. It ain't that tough, and I'm a quick learner.
 
C

Cor Ligthert

Hi 2Obvious,

Why not using a dataset, because that is so simple to handle, I change your
code of the datareader to give you an idea, I did it typing in here so watch
typos.

Dim conn As OleDbConnection
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
& "data source=" & server.mappath("pix.mdb"))
Dim strSQL As String
trSQL = "SELECT * " _
& "FROM Images " _
& "WHERE imgID=" & ID
'(Instead of the ID in the select you can better use the oledbparameters
however start as you did just to get it running)
Dim ad As New OleDbAdapter(trSQL,conn)
Dim ds As New Dataset
ad.fill(ds)
image1.Src = ds.tables(0).rows(0).item("srs") & img
'I assume that this is not a blob but an url to the image?
image1.Alt = ds.tables(0).rows(0).item("alt")
label1.Text = ds.tables(0).rows(0).item("citation")
End Sub
</script>

I hope this helps?

Cor
 
2

2obvious

Well, this certainly gave me something to think about.
Why not using a dataset

I thought the dataset was for cases where you want to hold onto a
table or more of information without directly manipulating the content
of the database. Whereas, the datareader was handy for read-only
situations where you go through the records returned only once. At
least, this is the impression I gathered from what I read.
'(Instead of the ID in the select you can better use the oledbparameters
however start as you did just to get it running)

I'm not sure what OleDbParameters are, but based on their name alone,
I would assume that it deals with data pulled from the database. I
use ID because I'm actually getting this tidbit through the
querystring, not out of the database.
Dim ad As New OleDbAdapter(trSQL,conn)

Why would I even need an adapter? Why can't I just use my SQL string
to declare and initialize the dataset?

I looked up Microsoft documentation on the OleDbAdapter, and in
addition to a vastly different interface, there was nothing in the
description that made it seem useful to this particular case.
image1.Src = ds.tables(0).rows(0).item("srs") & img

Having to specify all the empty tables and rows that came from using a
dataset to hold one measly record seems verbose. I know that with
..NET we're dealing with different objects and different logic, but I
would think there is a cleaner way to do this task that was once so
easy in ASP.

I still can't get this thing working. Anyone?
 

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