VB to C# conversion ODBC

A

acb

Hello,

I am a beginner in ASP.NET and C# having programmed in VB (not the .NET
flavour) in the past. I am looking for assistance in converting a
functional VB.NET aspx page to C#.

I am trying to extract data from a database using ODBC. A search on the
internet led me to the page at
http://forums.asp.net/27667/ShowPost.aspx that contained a functional
example in VB.NET. This worked on my computer. Hereunder is a snippet
of the code that can be used to demonstrate the problem:


<%@ Page Language="VB" Debug="True" CompilerOptions='/R:"C:\Program
Files\Microsoft.NET\Odbc.Net\Microsoft.Data.Odbc.dll"' %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Microsoft.Data.ODBC" %>
<script Language="VB" runat="server">

'DATABASE CONNECTION
Dim strConn As String = "DSN=Cache"

Public Sub Page_Load (obj As Object, e As EventArgs)
'DISPLAY RECORDS IN DATAGRID WHEN PAGE FIRST LOADS

End Sub

</script>

<html>
<head>
</head>
<body>
<asp:datagrid id="MyDataGrid" runat="server" />
</body>
</html>

My conversion of the above to C# is the following:

<%@ Page Language="C#" Debug="True" CompilerOptions='/R:"C:\Program
Files\Microsoft.NET\Odbc.Net\Microsoft.Data.Odbc.dll"' %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Microsoft.Data.ODBC" %>
<script Language="C#" runat="server">

// DATABASE CONNECTION
string strConn = "DSN=Cache";

void Page_Load (Object obj, EventArgs e)
{
// DISPLAY RECORDS IN DATAGRID WHEN PAGE FIRST LOADS
}

</script>

<html>
<head>
</head>
<body>
<asp:datagrid id="MyDataGrid" runat="server" />
</body>
</html>

My problem is that during compilation I keep getting the following
error:

Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

Compiler Error Message: CS0234: The type or namespace name 'ODBC' does
not exist in the class or namespace 'Microsoft.Data' (are you missing
an assembly reference?)

Source Error:


Line 1: <%@ Page Language="C#" Debug="True"
CompilerOptions='/R:"C:\Program
Files\Microsoft.NET\Odbc.Net\Microsoft.Data.Odbc.dll"' %>
Line 2: <%@ Import Namespace="System.Data" %>
Line 3: <%@ Import Namespace="Microsoft.Data.ODBC" %> <<--- Problem
line
Line 4: <script Language="C#" runat="server">
Line 5:


Can someone please help me.

Thank You
Chris
 
M

Mark Rae

<%@ Import Namespace="Microsoft.Data.ODBC" %>
<script Language="C#" runat="server">
My problem is that during compilation I keep getting the following
error:

Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

Compiler Error Message: CS0234: The type or namespace name 'ODBC' does
not exist in the class or namespace 'Microsoft.Data' (are you missing
an assembly reference?)

Several things:

1) Are you still using v1.0 of the Framework? If not, you don't need the
ODBC add-in, as it's part of the v1.1 Framework. Use System.Data.Odbc
instead.

2) C# is case-sensitive - have you tried referencing 'Microsoft.Data.Odbc'
instead of 'Microsoft.Data.ODBC'?

3) Why are you writing your code "in-line" rather than in a separate
code-behind class...?
 
A

acb

Hi Mark,

Thanks for your reply and the suggestions. Comments inline.
Several things:

1) Are you still using v1.0 of the Framework? If not, you don't need the
ODBC add-in, as it's part of the v1.1 Framework. Use System.Data.Odbc
instead.

Although I did find reference to System.Data.Odbc while searching the
net, I never quite arrived to the point of realising that one was more
recent; also the example I found was based on the
'Microsoft.Data.Odbc'.
2) C# is case-sensitive - have you tried referencing 'Microsoft.Data.Odbc'
instead of 'Microsoft.Data.ODBC'?

No I don't think I did; I've already spent ours trying to sort out bugs
that end up being case related; must get a tatto: "C# is case
sensitive, C# is case sensitive, ..."
3) Why are you writing your code "in-line" rather than in a separate
code-behind class...?

I'm following the book "ASP.NET by Danny and Tommy Ryan". While I have
Visual Studio 2003 installed, the book (up to now) uses notepad to
generate code. I found the book to be quite instructive and to my
liking; this is the first problem I got stuck on (mainly because I
don't use MS SQL databased used in the book.

Again thanks for helping get on thr right track

Take Care
Chris
 
C

Cor Ligthert [MVP]

Mark,
3) Why are you writing your code "in-line" rather than in a separate
code-behind class...?

Probably because that sadly enough the Microsoft ASPNET team shows allmost
all their samples in that way, while the designer from the ASPNET team does
use that even more in the version 2.0 than it did in 1.x.

(By the way, I could only recognise that in the import description from
Read, in my opinion is he using code behind)

Cor
 
M

Mark Rae

Although I did find reference to System.Data.Odbc while searching the
net, I never quite arrived to the point of realising that one was more
recent; also the example I found was based on the
'Microsoft.Data.Odbc'.

Well, fair enough, but it's important to realise that software moves on...
I've still got reference books which were written before even the first
version of .NET was released, but you can bet I'm not using them now...
No I don't think I did; I've already spent ours trying to sort out bugs
that end up being case related; must get a tatto: "C# is case
sensitive, C# is case sensitive, ..."

It's a well-known gotcha, especially for people (like me) coming from a VB
background....
I'm following the book "ASP.NET by Danny and Tommy Ryan". While I have
Visual Studio 2003 installed, the book (up to now) uses notepad to
generate code. I found the book to be quite instructive and to my
liking; this is the first problem I got stuck on (mainly because I
don't use MS SQL databased used in the book.

I don't even know where to start with this...
 
M

Mark Rae

Probably because that sadly enough the Microsoft ASPNET team shows allmost
all their samples in that way, while the designer from the ASPNET team
does use that even more in the version 2.0 than it did in 1.x.

Yes - I've never understood why they do that...
 
A

acb

Mark,
2) C# is case-sensitive - have you tried referencing 'Microsoft.Data.Odbc'
instead of 'Microsoft.Data.ODBC'?

For the record this ***was*** the problem.

Thank you for your answer.

I'm heading over to System.Data.Odbc land now; wish me luck :)

Regards,
Chris
 
A

acb

The following is the upgraded solution which works:

%@ Page Language="C#" Debug="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Odbc" %>
<script Language="C#" runat="server">

// DATABASE CONNECTION
string strConn = "DSN=Cache";

void Page_Load (Object obj, EventArgs e)
{
// DISPLAY RECORDS IN DATAGRID WHEN PAGE FIRST LOADS
}

</script>

<html>
<head>
</head>
<body>
<asp:datagrid id="MyDataGrid" runat="server" />
</body>
</html>

Thanks again
 

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