convert asp to .net

Hocke_sweden
Hocke_sweden used Ask the Experts™
on
I have a application with a lot of pagesI want to convert from asp to .net

I have tried to convert it to with the program asp2aspx from Netcoole, but the code don't work, the big problem is the database connection. Can someone help me to convert these code to asp.net?

Im a beginner in .net
<%
    connectstring = "server=testserver;Provider=SQLOLEDB;uid=sa;pwd=1234;Trusted_Connection=True;database=testdatabase"
    Dim rs
    Dim Conn

Class Datebase_Connections
	function connOpen()
		set Conn = Server.CreateObject("ADODB.Connection")
		Conn.open connectstring
	end function
	
	function connClose()
		Conn.Close
		set Conn = nothing
	end function
	
end class

Set Connections = New Datebase_Connections


Call Connections.ConnOpen
sql="select top 10 * from users"
set rs=conn.execute(sql)
do until rs.eof
    response.write ("<br>Long:"&rs("login"))
rs.movenext
loop

Call Connections.ConnClose
%>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
Start with ADO.net
Read data from SqlConnection (VB.net)
http://www.java2s.com/Code/ASP/ADO.net-Database/ReaddatafromSqlConnectionVBnet.htm
I am not sure if you are asking for VB.NET or C# but try this anyway (VB.NET).  Since you are just retrieving data, I used an ADO.NET DataReader to directly convert your ADO code.  You need to import System.Data.SqlClient for this to work.
Dim SQL As String = String.Empty
       
Dim rs As SqlClient.SqlDataReader
Dim Conn As New SqlClient.SqlConnection("user id=sa;data source=testserver;persist security info=True;initial catalog=testdatabase;password=1234")

SQL = "select top 10 * from users"
        
Dim Cmd As New SqlClient.SqlCommand(SQL)
    
If Conn.State <> ConnectionState.Open Then
   Conn.Open()
End If

Cmd.CommandType = CommandType.Text
Cmd.Connection = Conn
            
rs = Cmd.ExecuteReader()
           
While rs.Read()
    Response.Write(<br>Long:" & rs("login").ToString) 
End While

rs.Close()
              
If Conn.State = ConnectionState.Open Then
    Conn.Close()
End If

Open in new window

Author

Commented:
Thanks it works good, it was for vb.net, I post the code again, it was some small error in the code.

Is it possible to make a function to open the database with the connection string, so i don't need to write it everywhere?



<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>

<%
Dim SQL As String = String.Empty
Dim rs As SqlClient.SqlDataReader
Dim Conn As New SqlClient.SqlConnection("user id=sa;data source=testserver;persist security info=True;initial catalog=testdatabase;password=1234")

SQL = "select top 10 * from users"
        
Dim Cmd As New SqlClient.SqlCommand(SQL)
    
If Conn.State <> ConnectionState.Open Then
   Conn.Open()
End If

Cmd.CommandType = CommandType.Text
Cmd.Connection = Conn
            
rs = Cmd.ExecuteReader()
           
While rs.Read()
        Response.Write("<br>Login:" & rs("login").ToString)
End While

rs.Close()
              
If Conn.State = ConnectionState.Open Then
    Conn.Close()
End If
%>

Open in new window

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Why don't you put your function in a data access layer (DAL) class project to make it common to the rest of the pages?

Check this out:

http://msdn.microsoft.com/en-us/library/aa581778.aspx

http://www.15seconds.com/issue/030317.htm 

Author

Commented:
The asp project is big and complicated, and the work to convert and got it work with vb.net is big enough for the moment. I want to do it so easy I can for me so I can get the project in goal.
When it works in vb.net I will probably take the advantages of vb.net, now Im glad if I can do it "fast and dirty"

I read about it in the url you posted and like the idea of DAL, its smart!

But I want to start with functions first :)

OK.  How about creating a shared function in a class file?  You could move your function in a class file and let your pages call directly this shared function.  Check this out:

Check lesson 3 of the link below:

http://www.codeproject.com/KB/vb/OOPS_In_VBNET.aspx

Check this as well:

http://tutorials.beginners.co.uk/vb-net-programming-part-4-shared-or-class-members.htm

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial