Guys,

How come when I try to populate a datatable in Application_start, do I get a SQL error ...

Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.

If I do this on a page_load event, it works fine ?



VB Code:
  1. Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
  2.         ' Fires when the application is started
  3.  
  4.         'Create and hold the Staff, Region and Office tables
  5.  
  6.         Dim dt As New DataTable
  7.         db.Staff.Fill(dt)
  8.         Application("Staff") = dt
  9.  
  10.  
  11.     End Sub
  12.  
  13.  
  14. Class db
  15.        Public Shared ReadOnly Property ConnectionString() As String
  16.         Get
  17.             Return "data source=BLS5test2;persist security info=False;initial catalog=HPT;integrated security = sspi"
  18.            End Get
  19.     End Property
  20.  
  21.  
  22.     Public Shared cnn As SqlConnection
  23.     Private m_connectionstring As String
  24.     Private m_commandtimeout As Integer = 60
  25.     Shared Sub New()
  26.         'If Not gapprunning Then Return
  27.         cnn = New SqlConnection(ConnectionString)
  28.            End Sub
  29.  
  30.     Class Staff
  31.         Private Shared mda As SqlDataAdapter
  32.         Friend Shared Function Table(ByVal RegionID As Integer, ByVal OfficeID As Integer) As DataTable
  33.             Dim dt As New DataTable("Staff")
  34.             Fill(dt)
  35.             Return dt
  36.         End Function
  37.         Friend Shared Function Fill(ByVal dt As DataTable) As Integer
  38.             Return DataAdapter.Fill(dt)
  39.         End Function
  40.         Friend Shared Function Update(ByVal dt As DataTable) As Integer
  41.             Return DataAdapter.Update(dt)
  42.         End Function
  43.         Private Shared Function DataAdapter() As SqlDataAdapter
  44.             If Not mda Is Nothing Then Return mda
  45.             mda = New SqlDataAdapter
  46.             With mda
  47.                      .SelectCommand = New SqlCommand
  48.                 With .SelectCommand
  49.                     .Connection = cnn
  50.                     .CommandText = "select * from vw_HPTStaff"
  51.                     '.CommandType = CommandType.StoredProcedure
  52.                 End With
  53.                               AddHandler .FillError, AddressOf Fill_Error
  54.                 AddHandler .RowUpdated, New SqlRowUpdatedEventHandler(AddressOf DataAdapter_OnRowUpdated)
  55.             End With
  56.             Return mda
  57.         End Function
  58.         Private Shared Sub Fill_Error(ByVal sender As Object, ByVal e As FillErrorEventArgs)
  59.             If TypeOf e.Errors Is Data.ConstraintException Then e.Continue = True
  60.         End Sub
  61.         Private Shared Sub DataAdapter_OnRowUpdated(ByVal sender As Object, ByVal args As SqlRowUpdatedEventArgs)
  62.             Dim newID As Integer = 0
  63.             Dim idCMD As SqlCommand = New SqlCommand("SELECT @@IDENTITY", DataAdapter.UpdateCommand.Connection)
  64.             If args.StatementType = StatementType.Insert Then
  65.                 Try
  66.                     newID = CInt(idCMD.ExecuteScalar())
  67.                 Catch ex As Exception
  68.                     EH.Log(ex, EH.eErrorLogging.DisplayAndFile)
  69.                 End Try
  70.  
  71.                 args.Row("ID") = newID
  72.             End If
  73.         End Sub
  74.     End Class