Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

ok... vb is really not my favorite language, but I fond a code snipet on the web to create an odbc system dns... in VB so i tough I would at lest copy it to .net and try to make it work there before I try to change it to c#...

 

what I tryed seems to compile but im really not sure about the first 3 lines... and it does not work so theres probably an error somewhere...

 

original code as fond on http://home.pacbell.net/cetta/create-sys-DNS.htm

Option Explicit

Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
(ByVal hwndParent As Long, ByVal fRequest As Long, _
ByVal lpszDriver As String, ByVal lpszAttributes As String) _
As Long Private Const ODBC_ADD_SYS_DSN = 4

Public Function CreateSQLServerDSN(DSNName As String, _
ServerName As String, Database As String) As Boolean

Dim sAttributes As String

sAttributes = "DSN=" & DSNName & Chr(0)
sAttributes = sAttributes & "Server=" &
ServerName & Chr(0)
sAttributes = sAttributes & "Database=" &
Database & Chr(0)
CreateSQLServerDSN = CreateDSN("SQL Server", sAttributes)
End Function

Public Function CreateAccessDSN(DSNName As String, _
DatabaseFullPath As String) As Boolean

Dim sAttributes As String
'TEST TO SEE IF FILE EXISTS: YOU CAN
'REMOVE IF YOU DON'T WANT IT
If Dir(DatabaseFullPath) = "" Then Exit Function

sAttributes = "DSN=" & DSNName & Chr(0)
sAttributes = sAttributes & "DBQ=" &
DatabaseFullPath & Chr(0)
CreateAccessDSN = CreateDSN("Microsoft Access Driver (*.mdb)", _
sAttributes)
End Function

Public Function CreateDSN(Driver As String, Attributes As _
String) As Boolean
CreateDSN = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, _
Driver, Attributes)
End Function

 

Same code, translated to vb.net... by me

 

   Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long
   Private Const ODBC_ADD_SYS_DSN As Long = 4
   Public Function CreateSQLServerDSN(ByVal DSNName As String, ByVal ServerName As String, ByVal Database As String) As Boolean

       Dim sAttributes As String

       sAttributes = "DSN=" & DSNName & Chr(0)
       sAttributes = sAttributes & "Server=" & ServerName & Chr(0)
       sAttributes = sAttributes & "Database=" & Database & Chr(0)
       CreateSQLServerDSN = CreateDSN("SQL Server", sAttributes)
   End Function

   Public Function CreateAccessDSN(ByVal DSNName As String, ByVal DatabaseFullPath As String) As Boolean

       Dim sAttributes As String
       'TEST TO SEE IF FILE EXISTS: YOU CAN
       'REMOVE IF YOU DON'T WANT IT
       If Dir(DatabaseFullPath) = "" Then Exit Function

       sAttributes = "DSN=" & DSNName & Chr(0)
       sAttributes = sAttributes & "DBQ=" & DatabaseFullPath & Chr(0)
       CreateAccessDSN = CreateDSN("Microsoft Access Driver (*.mdb)", sAttributes)
   End Function

   Public Function CreateDSN(ByVal Driver As String, ByVal Attributes As String) As Boolean
       CreateDSN = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, Driver, Attributes)
   End Function

 

As you can see I didnt change many thing... only the declare thing

 

I tryed to ue it like this:

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       CreateAccessDSN("100232", "C:\\bd\\1002.mdb")
   End Sub

but the program throws an execption:

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication6.exe

Additional information: Object reference not set to an instance of an object.

now what I think is that I didnt translate it corectly, but if there's just anerror in the code, or if the problem is from the dll or anything like that, please tell me

Posted

Which line actually throws the exception? You should step through your code until you get to the actual line that throws the exception and then mouse-over or use the Watch window to test everything on that line to determine what is the null reference.

 

Also, VB6 was designed for 16-bit operating systems. If a variable was declared as Long in VB6 then it should be Integer in VB.NET. Likewise, a VB6 Integer is a VB.NET Short.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...