Results 1 to 6 of 6

Thread: [RESOLVED] Application Datatable connection error

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Resolved [RESOLVED] Application Datatable connection error

    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Application Datatable connection error

    First, make sure your connection string is like this:

    Code:
    "Data Source=BLS5test2;Initial Catalog=HPT;Integrated Security = SSPI;"
    I don't care what they say about case sensitivity, it should be right in strings like this.

    Second, keep in mind that since you're connecting from your ASP.NET application, you are connecting as the ASP.NET worker process and not as your login. Therefore, I suggest that you create a sql server login in your SQL Server, and then use those credentials to connect to the database, so that your connection string looks something like this:

    Code:
    "Data Source=BLS5TEST2;Initial Catalog=HPT;User Id=someuser;Password=asdflasdfasdf;"
    HTH

  3. #3

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Application Datatable connection error

    Thanks MH,

    I have corrected my connection string.... fair comment.

    I have tried using the SA account explicitly declared in the connection string, but am still getting a login fail. I thought that doing this in the web.config . .

    <authentication mode="Windows" />
    <identity impersonate="true" />

    . . . should sort this out and use the local user account ?

    Cheers
    Bob

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Application Datatable connection error

    When using the sa login, were your credentials correct? Also, did you get the error message saying that the user was null?

  5. #5

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Application Datatable connection error

    Thanks MH, I thought I had everything correct, but I must of had a typo, its worked fine with a new SQL user login.

    Thanks
    Bob

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Application Datatable connection error

    That's good. Add [Resolved] to the thread title, and all that other stuff you must be familiar with by now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width