Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Oledb conversion from VB to C#

  Asked By: Emily    Date: Jun 28    Category: Asp.net    Views: 999
  

I am working with OLEDB for Access, my following code in VB worked
but the same code (converted) in C# isnt working.


Code in VB
<% @ Page Language="VB" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OLEDB" %>
<html>
<script runat = server>

Sub Page_Load(Src As Object, E As EventArgs)
Dim strConn as string ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=" & server.mappath("dbase3.mdb") & ";"
Dim strSQL as string ="select * from Factory1 order by Period"
Dim Conn as New OLEDBConnection(strConn)
Dim Cmd as New OLEDBCommand(strSQL,Conn)
Dim Rdr as OLEDBDataReader

Conn.Open()
Rdr=Cmd.ExecuteReader()
MyDataGrid.DataSource = Rdr
MyDataGrid.DataBind()

Rdr.close()
Conn.close()
End Sub
</script>

Code in C#
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>

<html>
<script language="C#" runat="server">
void Page_Load(Object Src,EventArgs E) {
string strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=dbase3.mdb;" ;
string strSQL ="select * from Factory1 order by Period";
OleDbConnection Conn = new OleDbConnection(strConn);

OleDbCommand Cmd = new OleDbCommand(strConn, Conn);
OleDbDataReader Rdr;
Conn.Open();
Rdr=Cmd.ExecuteReader();
MyDataGrid.DataSource = Rdr;
MyDataGrid.DataBind();

Rdr.close();
Conn.close();
}
</script>


When I am trying to execute the C# pgm removing some statements, my
program is working correctly until the statement above Conn.Open();
Apparently, the error should be somewhere in Conn.Open();
But I couldnt figure out as the syntax is fine.
Hope someone can help me.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Nina Garcia     Answered On: Jun 28

You forgot to do a server.mappath in c# for the connection string


string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Serevr.MapPath("dbase3.mdb");

 
Answer #2    Answered By: Wilbert Patterson     Answered On: Jun 28

I myself figured it out anyway !!! the connection string  should be:


string strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("dbase3.mdb") + ";";

 
Didn't find what you were looking for? Find more on Oledb conversion from VB to C# Or get search suggestion and latest updates.




Tagged: