Creating ODBC DSN programmatically with VB.Net

I have a VB.Net app that I wrote three years ago (2008) which creates ODBC entries for SQL Server 2000 and SQL Server 2005.  The original source found at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=280066&SiteID=1

The apps runs fine on my computer, but when I run it on our test machine (Windows XP) or a users machine (Windows 7) it crashes.  Some users will not have SQL Native Client available even manually creating the ODBC DSN.

The error message received is:
Unable to find an entry point name 'SQLConfigDataSource32bit" in DLL 'ODBCCP32.DLL'
Below is the "important" code which is used to create ODBC entry.

I am so out of my league here, so be gentle and understanding! :) Thanks!


Private Declare Function SQLConfigDataSource32bit Lib "ODBCCP32.DLL" _
     (ByVal hwndParent As Integer, ByVal ByValfRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer

  Private Const vbAPINull As Integer = 0 ' NULL Pointer
  Private Const ODBC_ADD_DSN As Short = 1 ' Add data source

  Friend DriverName As String

....


Private Sub btnCreateDSN_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnCreateDSN.Click

    If Len(Me.txtUsername.Text) = 0 Then
      MsgBox("Username cannot be blank")
      Exit Sub
    End If

    Dim intRet As Integer
    Dim Attributes As String

    'Set the attributes delimited by null.
    'See driver documentation for a complete
    'list of supported attributes.
    Attributes = "SERVER=" & Me.txtServer.Text & Chr(0)
    Attributes = Attributes & "DATABASE=" & Me.txtDBName.Text & Chr(0)
    Attributes = Attributes & "DESCRIPTION=" & Me.txtDesc.Text & Chr(0)
    Attributes = Attributes & "DSN=" & Me.txtDSN.Text & Chr(0)
    Attributes = Attributes & "TRUSTED_CONNECTION=YES" & Chr(0)

    'To show dialog, use Form1.Hwnd instead of vbAPINull.
    intRet = SQLConfigDataSource32bit(vbAPINull, ODBC_ADD_DSN, DriverName, Attributes)

    If intRet <> 0 Then
      MsgBox("DSN Created")
    Else
      MsgBox("Create Failed")
    End If

  End Sub 'Private Sub btnCreateDSN_Click

Open in new window

CMI_ITAsked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
CMI_ITAuthor Commented:
No, I didn't see this.  It didn't come up in my Google search.  I will take a look at it (it looks promising! :) ).
0
 
CMI_ITAuthor Commented:
I have looked at your code/app.  You are also using the 'ODBCCP32.DLL'.  Other than the order of the attributes the proccess is the same (see my code above).

If 'ODBCCP32.DLL' is a Windows DLL and is present on the target machine why am I getting the error:
Unable to find an entry point name 'SQLConfigDataSource32bit" in DLL 'ODBCCP32.DLL'
0
 
CMI_ITAuthor Commented:
I have fixed my own problem.  It was the "32bit" that had at some point been added to the 'SQLConfigDataSource'.  Removed it and now it runs just fine.

I am going to award the points to emoreau because his solution is more dynamic and has more features, which I plan on implementing.  If I hadn't looked at his solution again and wrote my last comment then I wouldn't have found the problem.  Thanks, emoreau!
0
 
CMI_ITAuthor Commented:
I have fixed my own problem.  It was the "32bit" that had at some point been added to the 'SQLConfigDataSource'.  Removed it and now it runs just fine.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.