Database Collection - Software Design and Programming for Business and Construction

Home Software Experience Clients Contact Me Tech Tips

 

 

Programming Tips for VB.Net and SQLServer

  1. VB.Net

    1. Bring a running application to the front

    2. Responding to page selection in a tab control

    3. Copy rows from one data table to another

    4. Deleting rows from a data table according to content

    5. Using Directory Services

  2. SQLServer

    1. Restore a backup to a different server
       

A1. Bring a running application to the front
  Put this above the class declaration:
Imports System.Runtime.InteropServices

Put this after the class declaration and any constant/variable declarations:
'The SetForegroundWindow function puts the thread that created the
'specified window in the foreground and then activates the window.

<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(ByVal hwnd As IntPtr) As Boolean
End Function

The following is a menu click event for bringing an application called RespEditor to the front if it is running or else starting it.
Private Sub mnToolsRespEditor_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles mnToolsRespEditor.Click
     Dim myProcesses As Process() = Process.GetProcessesByName("RespEditor")
    
'The path to RespEditor.exe is stored in Settings.
     Dim respEdPath As String = My.Settings.RespEditorPath
     Dim result As Boolean
     If (myProcesses.GetUpperBound(0) > -1) Then
          result=SetForegroundWindow(myProcesses(0).MainWindowHandle)
     Else
        If File.Exists(respEdPath) Then
             Process.Start(respEdPath)
        End If
     End If
     myProcesses = Nothing
End Sub

Back to Top

A2. Responding to page selection in a tab control
  This method allows checking of the tab-page name rather than the page index which may get changed if you change the order of the pages:
Private Sub TabInspDets_SelectedIndexChanged( _
                          ByVal sender As System.Object,  _
                          ByVal e As System.EventArgs)  _
                          Handles TabInspDets.SelectedIndexChanged
        Me.lblActionNote.Visible = (Me.TabInspDets.SelectedTab.Name = "tpgActions")
End Sub

Back to Top

A3. Copy rows from one data table to another
  To copy from a datatable called dtSource to one called dtTarget:
For each row As DataRow In dtSource.Rows
        dtTarget.Rows.Add(row.ItemArray)
Next

Back to Top

A4. Deleting rows from a data table according to content
  Loop through the data table backwards because the index of the rows above the deleted row will keep changing:
For rowNr As Integer = (MyDataTable.Rows.Count -1) To 0 Step -1
         If MyDataTable.Rows(rowNr)("TestColumnName")=TestValue
                  MyDataTable.Rows.RemoveAt(rowNr)
         Next
Next

Back to Top

A5. Using Directory Services
  When you use Imports System.DirectoryServices you get an error saying that Directory Services cannot be found.

This is because Visual Studio does not automatically add a reference to this library. You need to add the reference manually through the Project-Add Reference menu item. You should find System.Directory Services listed on the .Net tab.

Back to Top

B1. Restore a backup to a different SQLServer
  This is for SQLServer 2000.
You restore the backup first followed by the subsequent transaction backup file(s) in chronological order, using WITH NORECOVERY for each step until the last.
If you are restoring over an existing version then you use WITH NORECOVERY,REPLACE in the restore database step.
There are lots of variations on this so do look at the RESTORE DATABASE and RESTORE LOG topics in Transact-SQL Help from SQL Query Analyser:
RESTORE DATABASE yourDBName
FROM DISK = 'pathToFile.BAK'
WITH NORECOVERY
GO

RESTORE LOG yourDBName
FROM DISK = 'pathToFile.TRN'
WITH RECOVERY
GO

Back to Top

   
Based in Devon in southwest UK, Database Collection can provide software and program development for English speaking businesses worldwide.
Last modified: 14 March 2008 Software Development companies