Link to home
Start Free TrialLog in
Avatar of Anadaboy
Anadaboy

asked on

What's the VB.NET EQUIVALENT CODE for this VB6 code?

The following is some VB 6.0 code.  I need the equivalent code in VB.NET to accomplish the SAME EXACT thing IN AS FEW LINES OF CODE AS POSSIBLE.

I WILL PASTE YOUR SUPPLIED CODE DIRECTLY INTO MY VB.NET ENVIRONMENT TO TEST IT SO PLEASE MAKE SURE IT'S COMPLETE.  THANKS!

Set Con1 = New ADODB.Connection
Con1.Open "Provider=SQLOLEDB.1;Password=ThePassword;Persist Security Info=True;User ID=TheUser;initial Catalog=TheDatabaseName;Data Source=111.111.111.111"
Set RS1 = New ADODB.Recordset
RS1.Source = "SELECT * FROM EmployeeMaster Where LastName= '"& Smith &"'
RS1.ActiveConnection = Con1
RS1.Open
While Not RS1.EOF    
    label1.caption=RS1("FirstName")
Wend
RS1.Close
Set RS1=Nothing
Con1=Close
Set Con1=Nothing
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

       Dim oConn As New SqlClient.SqlConnection("Password=ThePassword;Persist Security Info=True;User ID=TheUser;Initial Catalog=TheDataBaseName;Data Source=111.111.111.111")
        oConn.Open()

        Dim oCmd As New SqlClient.SqlCommand("SELECT * FROM EmployeeMaster WHERE LastName='Smith'")
        Dim oRS As SqlClient.SqlDataReader = oCmd.ExecuteReader(CommandBehavior.CloseConnection)

        While (oRS.Read())
            Label1.Text = oRS("FirstName")
        End While

        oRS.Close()
        oConn.Close()
Avatar of Anadaboy
Anadaboy

ASKER

I tried what was provided from Carl Tawn and got this error:  

An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: ExecuteReader: Connection property has not been initialized.

What am I missing?
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial