Shurikn Posted July 7, 2005 Posted July 7, 2005 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 Quote
jmcilhinney Posted July 8, 2005 Posted July 8, 2005 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. Quote
IngisKahn Posted July 8, 2005 Posted July 8, 2005 Uh, VB6 came out long after the death of 16 bit and handles 32-bit operations just fine, but ya, the variable sizes have changed. Quote "Who is John Galt?"
jmcilhinney Posted July 8, 2005 Posted July 8, 2005 Sorry, VB6 was an evolution of a language that was designed for 16-bit operating systems. That's why the standard number type, i.e. Integer, is a 16-bit number. Quote
Shurikn Posted July 8, 2005 Author Posted July 8, 2005 I changed every Long to Integer and it worked, so you were right :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.