You are hereProgramming & Web Design / .Net / VB.Net


VB.Net

warning: Creating default object from empty value in /home/hagrinad/public_html/modules/taxonomy/taxonomy.pages.inc on line 33.

VB.Net

Create a Word 2003 Document Using VB.Net

By hagrin - Posted on 04 August 2008

The following code will help you create a very basic Word 2003 document using VB.Net. In addition to the code below, you will need to make sure that Office is installed on the host running the application for the document to be created.

Dim word As New Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document
doc = word.Documents.Add()
Dim range As Microsoft.Office.Interop.Word.Range = doc.Range(Start:=0, End:=0)
range.Text = "Put your document's text here."
doc.SaveAs("Path and File Name")
doc.Close(True)
word.Quit()

Create an Excel 2003 Workbook Using VB.Net

By hagrin - Posted on 04 August 2008

The following contains code on "How to Create an Excel 2003 Workbook Using VB.Net". Remember, you will need to add the "Import Microsoft.Office.Interop" reference at the top of your code. In addition, you will need sufficient rights to create files in your desired save location as well as Office installed on the machine hosting the application.

Dim MyEx As New Excel.Application
Dim MyBook As Excel.Workbook
Dim MySheet As Excel.Worksheet
MyEx.DisplayAlerts = False
MyBook = MyEx.Workbooks.Add
MySheet = MyBook.Sheets.Add
MySheet.Name = "Worksheet Name"

'Entering static values into cells as headers
MySheet.Cells(1, 1) = "Header 1"
MySheet.Cells(1, 2) = "Header 2"
MySheet.Cells(1, 3) = "Header 3"
MySheet.Cells(1, 4) = "Header 4"
MySheet.Cells(1, 5) = "Header 5"
MySheet.Cells(1, 6) = "Header 6"

'Reading through a data set to fill in the rest of the worksheet
For i = 0 To DataSet.Tables(0).Rows.Count - 1
   MySheet.Cells(i + 2, 1) = DataSet.Tables(0).Rows(i).Item(0)
   MySheet.Cells(i + 2, 2) = DataSet.Tables(0).Rows(i).Item(1)
   MySheet.Cells(i + 2, 3) = DataSet.Tables(0).Rows(i).Item(2)
   MySheet.Cells(i + 2, 4) = DataSet.Tables(0).Rows(i).Item(3)
   MySheet.Cells(i + 2, 5) = DataSet.Tables(0).Rows(i).Item(4)
   MySheet.Cells(i + 2, 6) = DataSet.Tables(0).Rows(i).Item(5)
Next

MyBook.SaveAs("FileName.csv", FileFormat:=Excel.XlFileFormat.xlCSV)
MyBook.Close()
MyEx.Quit()
MySheet = Nothing
MyBook = Nothing
MyEx = Nothing
System.GC.Collect()

VB.Net 2.0, SQL Server 2000 & Windows Application SQL Error

By hagrin - Posted on 11 May 2007

Today, I started working on an application to perform bulk changes to our Deltek Vision database to change tasks, sub-tasks, budgets and billing terms. So, as I go to write the SQL connection string, something I have done 100s of times, I kept getting the following error from my app:

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

While the error is pretty descriptive, there is one slight problem - I'm not trying to access a 2005 SQL Server, but a SQL Server 2000 box. So, I do what any good programmer does - he searches the major search engines for a solution, but most of the examples are of users trying to 1) build a Web application and have a web.config error or 2) are actually trying to access a SQL Server 2005 database. Yikes, now what?

I can't stress this enough - check your connection string. I was actually passing a blank Data Source (I was using SelectedValue as opposed to SelectedItem) and since I have SQLExpress running on my development machine, I assume that's why I was generating a 2005 error when trying, in theory, to connect to a SQL 2000 machine. The lesson is - verify your connection string when getting this error and trying to access a SQL 2000 machine. If you're trying to access a SQL 2005 machine, follow the enabling remote connections info that you'll find on the hundreds of pages that come up in he search engines for this error.

Convert .Net Code to HTML

By hagrin - Posted on 03 May 2007

When you're writing a development related blog, you're eventually (hopefully) going to write some code snippets that you want to share with your readers. However, in order to get the most out of that code snippet, you should make it easy to read and color coded so that potential users can quickly read through your code and evaluate its worth.

That's where CopySourceAsHTML will solve all your problems. CSAH is a plug-in that integrates directly into your Visual Studio IDE environment and allows you to highlight code, right click and choose "Copy As HTML". The tool is freeware and performs exceptionally - if you maintain a .Net developer blog, I would suggest installing this add-on.

SharePoint v3: Quick Launch Update

By hagrin - Posted on 02 May 2007

Just a FYI to the 3 of you reading this site (Hi Mom and Dad!), I made an update to the Recreating the SharePoint Quick Launch story I posted back in February. I made some code fixes that were mainly introduced with the release of the 40 Free WSS Application Templates made by Microsoft.

Hope the bug fixes help!

VB.Net - How to Unlock a Domain Account in Active Directory

By hagrin - Posted on 09 February 2007

How to Unlock a Domain Account in Active Directory utilizing Visual Basic .Net - (note: you must have Domain Admin privledges to execute this code successfully and you must import the System.DirectoryServices namespace):

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim strError As String
Try
Dim child As New System.DirectoryServices.DirectoryEntry("LDAP://DC=YourDomainsName,DC=com")
Dim searcher As New DirectorySearcher(child)
Dim result As SearchResult
Dim userEntry As DirectoryEntry
searcher.Filter = "(SAMAccountName=TheUsernameYouWantDisabled)"
searcher.CacheResults = False
result = searcher.FindOne
userEntry = result.GetDirectoryEntry
With userEntry
userEntry.Properties("LockOutTime").Value = 0
End With
userEntry.CommitChanges()
Catch ex As Exception
strError = ex.ToString
End Try

End Sub

VB.Net - How to Disable a Domain Account in Active Directory

By hagrin - Posted on 09 February 2007

How to Disable a Domain Account in Active Directory utilizing Visual Basic .Net - (note: you must have Domain Admin privledges to execute this code successfully and you must import the System.DirectoryServices namespace):

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim strError As String
Try
Dim child As New System.DirectoryServices.DirectoryEntry("LDAP://DC=YourDomainsName,DC=com")
Dim searcher As New DirectorySearcher(child)
Dim result As SearchResult
Dim userEntry As DirectoryEntry
searcher.Filter = "(SAMAccountName=TheUsernameYouWantDisabled)"
searcher.CacheResults = False
result = searcher.FindOne
userEntry = result.GetDirectoryEntry
With userEntry
userEntry.NativeObject.accountdisabled = True
End With
userEntry.CommitChanges()
Catch ex As Exception
strError = ex.ToString
End Try

End Sub

Visual Basic .Net Coding Samples

By hagrin - Posted on 09 February 2007

Below are code samples utilizing Visual Basic .Net. If you have any questions, comments or corrections on any code sample, please email me at hagrin at gmail dot com and I'll try and get back to all user inquiries as soon as possible. Thank you and hopefully this code will proove to be helpful.

Disable a Domain Account in Active Directory
Unlock a Domain Account in Active Directory

SharePoint v3: Recreating the Quick Launch Menu

By hagrin - Posted on 07 February 2007

UPDATE: 2009 June 17th - Please see the comment by David in the comments section. His fix is the best fix I have seen for this problem.

If you see any improvements that I can make, let me know by leaving me a comment. Hope this helps someone.

Versioning

  1. Version 1.2 - (2009 June 17) - Much better solution posted in comments by user David.
  2. Version 1.1 - (2007 May 2) - Bug fixes which include:
    • Applying Quick Launch logic that is used in the SharePoint WSS 40 Free Application templates.
    • Wrapping data retreived from the database and other sources in System.Web.HttpUtility.UrlPathEncode() to prepare for URL formation and usage.
    • Colorized the comments (I'm looking for a Drupal module to automatically do this for me)
  3. Version 1.0 - (2007 Feb 7) - Original Article on recreating the SharePoint v3 Quick Launch through a Web Part

Project: NCAA Basketball Statistics Scraper & Handicapper

By hagrin - Posted on 29 November 2006

Since my SEO Tool has been successfully collecting data and I haven't decided how to display the information, I decided to start collecting data for another project. I remember reading that there was a discussion over the public domain of sports statistics and I personally believe that the data should be free and available to download. However, since it is not and data distribution sites want big money for such information, I did what any good programmer would do - develop a web scraper to parse available data, restucture it and hopefully release it freely to the public (this last statement is probably a no go, but I'll do the necessary research to figure out the potential copyright issues).

So, one would probably ask, once you have the data what do you plan on doing with it? To be honest, the only real use I see outside of eay-to-use public distribution would be to provide the public a "system" for handicapping future contests. Many sites like covers.com that post trends post statistics on an average basis which really misleads someone looking at the matchups. I believe that stronger trends exist when evaluating other factors not necessarily measured or taken into account as well as looking at standard deviations of data and potentially the median of such data.

I actually set myself a deadline for this one - January 1, 2007 so I would check back around then for the SQL scripts needed and hopefully the web interface that allows people to evaluate certain matchups (although as I write that, I'm thinking that it might just be better to automatically evaluate all the games for a day and then rank them - no need for user intervention). Obviously, I could track the accuracy of my predictions and tweak the formula as I see certain trends to improve the accuracy.