Friday, February 8, 2008

Creating a system DSN entry with VB.Net 2005

I needed to create a ODBC driver that will attach to the database I need to access for my program. The goal is to do as little manually as possible, which means I need to write code to create this ODBC driver.

First of all, many thanks to Paul Clement whose post was able to get my last bug out and the code working correctly. (See his post at http://www.mcse.ms/message1068199.html)

Here is my code:



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


Private Const ODBC_ADD_SYS_DSN As Short = 4


Private Const vbAPINull As Int32 = 0



Public Function CreateODBC(ByVal szDSNName As String, ByVal szServerName As String, ByVal szDatabase As String) As Boolean


Dim szAttributes As String = ""


Dim szDriver As String = "SQL Server" '//Set to SQL Server as it is the most common


Dim lReturnValue As Int32 = 0



Try


szAttributes = szAttributes & "Server=" & szServerName & Chr(0)


szAttributes = szAttributes & "DESCRIPTION=Test" & Chr(0)


szAttributes = szAttributes & "DSN=" & szDSNName & Chr(0)



szAttributes = szAttributes & "Database=" & szDatabase & Chr(0)


szAttributes = szAttributes & "Trusted_connection=YES" & Chr(0)



'//To show dialog, use Form1.Hwnd instead of vbAPINull


lReturnValue = SQLConfigDataSource(vbAPINull, ODBC_ADD_SYS_DSN, szDriver, szAttributes)



If lReturnValue <> 0 Then


Return True


Else


Return False


End If


Catch ex As Exception


RptError("Unable to create the ODBC", ex)


Return False


End Try


End Function


Tuesday, February 5, 2008

Simple Progress Bar vb.net Code

Here is a simple sample of using a progress bar in vb.net 2005:

I have a form with a progress bar on it called progBar1, a button called btnUpgrade, and a backgroundworker called bwBackgroundWorker.




Private Sub btnUpgrade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpgrade.Click


        Try


            '//Disable the upgrade button


            Me.btnUpgrade.Enabled = False


 


            '//Create a new instance and initialize


            bwBackgroundWorker = New System.ComponentModel.BackgroundWorker


            bwBackgroundWorker.WorkerReportsProgress = True


            bwBackgroundWorker.WorkerSupportsCancellation = True


            bwBackgroundWorker.RunWorkerAsync()


 


        Catch ex As Exception


            RptError("Unable to upgrade!", ex)


        End Try


    End Sub


 


    Private Sub bwBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwBackgroundWorker.DoWork


        '//Just for a test, create a loop


 


        For X As Int32 = 0 To 100


            If bwBackgroundWorker.CancellationPending Then


                Exit For


            End If


 


            bwBackgroundWorker.ReportProgress(X)


            Threading.Thread.Sleep(100)


        Next


    End Sub


 


    '//Update the progress bar


    Private Sub bwBackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bwBackgroundWorker.ProgressChanged


        Me.progBar1.Value = e.ProgressPercentage


    End Sub


Monday, January 28, 2008

ClickOnce publish error: "...make sure the web server name is valid..."

Scenario:
I am publishing a .NET application to a webserver. For test purposes, I have had a test website set up at http://lee.interyx.net and am going to try to publish to it.

I get the following error:
"ERROR: Failed to connect to http://lee.interyx.net/NetSched with the follwoing error: Unable to create the web 'http://lee.interyx.net/NetSched/'. Could not find a web server at 'lee.interyx.net' on port 80. Please check to make sure that the web server name is valid and your proxy settings are set correctly. If you are sure that everything is correct, the web server may be temporarily out of service."

I then go through the following steps:

1. Create a new folder in C:\inetpub\wwwroot. Then open IIS and create a virtual directory with an alias of TestSide and point it to the folder. Still unable to browse to the test site in a web browser.

2. On the web server, open a command window. Browse to C:\Windows\Microsoft.NET\Framework\
Run aspnet_regiis -i
This should make sure that asp is registered AFTER the framework is installed
Still not able to browse to the website.

3. Make sure that IIS is running (I know, a little late in the game, but better late than never, and at this point, I was hoping it was just something dumb like this that was overlooked). It was running. :(

4. Reinstall the .NET framework, just to make sure it was installed correctly.

5. Change the publishing location to use the internal address of 10.0.10.20. I am able to browse to the website if I use http://10.0.10.20/NetSched, just not http://lee.interyx.net/NetSched.
Now, I get a new error:
"Failed to connect to 'http://10.0.10.20/NetSched' with the following error: Unable to create the web 'http://10.0.10.20/NetSched'. The web server does not appear to have the FrontPage server extensions installed".

6. Double check to make sure the FrontPage server extensions are installed. They are.

About this time, the entire network went down. Apparently Qwest had a line cut between here and Denver and VCN went down too. When everything came back up, I was able to connect. Hmmm...I don't know what the exact resolution was, as I had gotten other people involved and can not say for sure if they also changed a setting that I do not know about.

ClickOnce Publish Error - "...It may be a page or folder in a subweb.."

Scenario:
Publishing a .NET application called NetSched to a webserver at 10.0.10.20. I had created a virtual directory in the default website called NetSched without executable or write permissions. **Note: I had created a virtual directory previously, with the same name with ALL permissions. This is important.
When I tried to publish I got the following error: "ERROR: Failed to connect to 'http://10.0.10.20/NetSched/' with the following error: unable to create the web 'http://10.0.10.20/NetSched/'. Server error: 'NetSched' does not refer to a page or folder in this web. It may be a page or folder in a subweb, or it could be a badly formed URL."

Resolution:
When I had deleted the previosly created virtual directory, I did not delete ALL the files. Therefore, when I created the new virtual directory with the same name, there were files in the directory that needed to be deleted. The Virtual directory NetSched pointed to the folder "C:\Inetpub\wwwroot\NetSched" After I deleted the files that started with _vti (like _vti_cnf, _vti_pvt, ect) and tried to publish, these files were created and the publish succeeded!

ClickOnce Applications and Mozilla Firefox

Scenario:
I published a .NET project to a website for installation and deployment. If the end user was using Internet Explorer, all was well. However, if the user was using Mozilla Firefox, the application was not able to be run, even after the setup file was downloaded.

Resolution:
An addon needs to be downloaded and installed on the end user's machine. The addon can be found at https://addons.mozilla.org/en-US/firefox/addon/1608
Now, when the setup file is run, another button is shown that says "Run ClickOnce Application". Clicking this button runs the setup and installs as intended.

Thursday, January 3, 2008

SQL Error: RestoreContainer::ValidateTargetForCreation

Scenario: I created a backup of a database on the server (Server2003) running SQL 2000. When I tried to restore the database on an XP machine running SQL 2005, I got the following error: The operating system return the error '5(Access is denied)' while attempting 'RestoreContainer::ValidateTargetForCreation' on ''

Resolution:
1. On the XP machine (or whatever machine you are trying to restore to), open SQL configuration manager.
2. Right Click SQL Server Express (or SQL Server (SQLEXPRESS) if running full SQL)
3. Choose Properties
4. On the Logon Tab, make sure the "Built in Account" radio button is checked and then choose "Local System" from the dropdown

This allowed me to restore the backup. This is not as secure, obviously, so I will probably want to change it back after my restore has been completed.

Post Prime

Ok, I'm so tired of running across errors, finding a fix for them and then 2 weeks later forgetting exactly how I fixed it that I finally broke down and created a blog to hold these nuggets of intellectual treasures. Ahhh...my precious! Now I will be able to take over the world in a night! NARF!