Results 1 to 2 of 2

Thread: Object Reference not set to an instance of the object

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    113

    Object Reference not set to an instance of the object

    Hi all,
    I'm trying to fix up some horrible code that keeps giving me the error mentioned in the subject. Here is the troublesome procedure:

    VB Code:
    1. Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    2.         Dim strUpdateSelected, strReadOnlySelected As String
    3.         Dim strErrorMessage, strSql, strUpdateList, strReadOnlyList As String
    4.         Dim intIndex, intModuleType, intSelectedFunctionIndex As Integer
    5.         Dim drData As SqlDataReader
    6.        
    7.         Dim clsPraxisDB As New AdminTools.prx.db_lib()
    8.         clsPraxisDB.DBConnectionString = ConfigurationSettings.AppSettings("ConnectionString")
    9.         clsPraxisDb.makeSQLConnection()
    10.        
    11.         If Trim(txtMessage.Text) = "" Or Trim(txtDescription.Text) = "" Then
    12.             If Trim(txtMessage.Text) = "" And Trim(txtDescription.Text) = "" Then
    13.                yada yada yaa
    14.             End If
    15.         Else
    16.             intSelectedFunctionIndex = cboFunctions.SelectedItem.Value
    17.            ' clsPraxisDB.DBConnectionString = Application.Contents("ConnectionString")
    18.             strErrorMessage = ""
    19.             For intIndex = 0 To chkLstUpdate.Items.Count - 1
    20.                 If chkLstUpdate.Items(intIndex).Selected And chkLstReadOnly.Items(intIndex).Selected Then
    21.                     If Trim(strErrorMessage) = "" Then
    22.                         strErrorMessage = "The following position(s) had been selected as Update and Read only: " & chkLstUpdate.Items(intIndex).Text.ToString
    23.                     Else
    24.                         strErrorMessage = strErrorMessage & ", " & chkLstUpdate.Items(intIndex).Text.ToString
    25.                     End If
    26.                 End If
    27.             Next
    28.  
    29.             If Trim(strErrorMessage) = "" Then
    30.                 strSql = "SELECT Description FROM xaFunction WHERE (Description = '" & Replace(txtDescription.Text, "'", "''") & "')"
    31.                 If intSelectedFunctionIndex <> 0 Then
    32.                     strSql = strSql & " And (ModuleType <> " & intSelectedFunctionIndex & ")"
    33.                 End If
    34.  
    35.                 'clsPraxisDB.makeSQLConnection()
    36.                 clsPraxisDB.SQLStatement = strSql
    37.                 drData = clsPraxisDB.getRecords
    38.                 While drData.Read
    39.                     strErrorMessage = "Function Description '" & txtDescription.Text & "' already exists"
    40.                 End While
    41.             End If
    42.  
    43.             If Trim(strErrorMessage) = "" Then
    44.                 If cboFunctions.SelectedItem.Value.ToString = "0" Then
    45.                     'New function
    46.                     strSql = "INSERT INTO xaFunction (Description, ErrorMessage, DteUpd, UserID) VALUES " & _
    47.                             "('" & Replace(txtDescription.Text, "'", "''") & "', '" & Replace(txtMessage.Text, "'", "''") & "',GetDate()," & Session("UserPK") & ");select @@IDENTITY AS ModuleType"
    48.                     'Response.Write(strSql)
    49.                     'Response.End()
    50.                     clsPraxisDB.SQLStatement = strSql
    51.                     intModuleType = clsPraxisDB.getScalarValue
    52.                     '     Response.End()
    53.                     For intIndex = 0 To chkLstUpdate.Items.Count - 1
    54.  
    55.                         If chkLstUpdate.Items(intIndex).Selected Then
    56.                             strSql = "INSERT INTO xaFuncPos (ModuleType, PositionCd, AccessType, DteUpd, UserId) VALUES " & _
    57.                                     "(" & intModuleType & ",'" & chkLstUpdate.Items(intIndex).Value.ToString & "','U',GetDate(), " & Session("UserPK") & ")"
    58.  
    59.                             clsPraxisDB.execNonQuery(strSql)
    60.                         ElseIf chkLstReadOnly.Items(intIndex).Selected Then
    61.                             strSql = "INSERT INTO xaFuncPos (ModuleType, PositionCd, AccessType, DteUpd, UserId) VALUES " & _
    62.                                     "(" & intModuleType & ",'" & chkLstReadOnly.Items(intIndex).Value.ToString & "','R',GetDate(), " & Session("UserPK") & ")"
    63.                             clsPraxisDB.execNonQuery(strSql)
    64.                         End If
    65.                     Next
    66.                     Call PopulateScreenValues()
    67.                 Else
    68.                     'intSelectedFunctionIndex = cboFunctions.SelectedItem.Value
    69.                     'clsPraxisDB.makeSQLConnection()
    70.  
    71.                     strSql = "UPDATE xaFunction SET Description = '" & Replace(txtDescription.Text, "'", "''") & "', ErrorMessage = '" & _
    72.                               Replace(txtMessage.Text, "'", "''") & "', DteUpd = GetDate() WHERE (ModuleType = " & intSelectedFunctionIndex & ")"
    73.                     clsPraxisDB.execNonQuery(strSql)
    74.  
    75.                     strUpdateList = ""
    76.                     strReadOnlyList = ""
    77.  
    78.                     'Get all the positions which got access to this function
    79.                     strSql = "SELECT PositionCd, AccessType " & _
    80.                               "FROM xaFunction LEFT JOIN xaFuncPos ON (xaFunction.ModuleType = xaFuncPos.ModuleType) " & _
    81.                               "WHERE(xaFunction.ModuleType = " & intSelectedFunctionIndex & ") ORDER BY PositionCd"
    82.  
    83.                    ' clsPraxisDB = New AdminTools.prx.db_lib()
    84.                    ' clsPraxisDB.DBConnectionString = Application.Contents("ConnectionString")
    85.                    ' clsPraxisDB.makeSQLConnection()
    86.  
    87.                     'Response.Write(strSql)
    88.                     'Response.End()
    89.                     clsPraxisDB.SQLStatement = strSql
    90.                     drData = clsPraxisDB.getRecords
    91.                     While drData.Read
    92.                         If Not IsDBNull(drData("PositionCd")) Then
    93.                             If drData("AccessType") = "U" Then
    94.                                 strUpdateList = strUpdateList & ", '" & drData("PositionCd") & "'"
    95.                             Else
    96.                                 strReadOnlyList = strReadOnlyList & ", '" & drData("PositionCd") & "'"
    97.                             End If
    98.                         End If
    99.                     End While
    100.  
    101.                     For intIndex = 0 To chkLstUpdate.Items.Count - 1
    102.                         If chkLstUpdate.Items(intIndex).Selected Then
    103.                             If InStr(strUpdateList, "'" & chkLstUpdate.Items(intIndex).Value & "'") > 0 Then
    104.                                 'do nothing
    105.                             ElseIf InStr(strReadOnlyList, "'" & chkLstReadOnly.Items(intIndex).Value & "'") > 0 Then
    106.                                 'Update the access type
    107.                                 strSql = "UPDATE xaFuncPos SET AccessType = 'U', DteUpd = GetDate() WHERE (ModuleType = " & intSelectedFunctionIndex & _
    108.                                         ") AND (PositionCd = '" & chkLstReadOnly.Items(intIndex).Value & "')"
    109.                                 clsPraxisDB.execNonQuery(strSql)
    110.                             Else
    111.                                 'Insert record which had just been selected
    112.                                 strSql = "INSERT INTO xaFuncPos (ModuleType, PositionCd, AccessType, DteUpd, UserId) VALUES " & _
    113.                                         "(" & intSelectedFunctionIndex & ",'" & chkLstUpdate.Items(intIndex).Value.ToString & "','U',getdate(), " & Session("UserPK") & ")"
    114.  
    115.                                 clsPraxisDB.execNonQuery(strSql)
    116.                                 'Response.Write(strSql)
    117.                                 'Response.End()
    118.                             End If
    119.                         ElseIf chkLstReadOnly.Items(intIndex).Selected Then
    120.                             If InStr(strUpdateList, "'" & chkLstUpdate.Items(intIndex).Value & "'") > 0 Then
    121.                                 'Update the access type
    122.                                 strSql = "UPDATE xaFuncPos SET AccessType = 'R', DteUpd = GetDate() WHERE (ModuleType = " & intSelectedFunctionIndex & _
    123.                                         ") AND (PositionCd = '" & chkLstUpdate.Items(intIndex).Value & "')"
    124.                                 clsPraxisDB.execNonQuery(strSql)
    125.                             ElseIf InStr(strReadOnlyList, "'" & chkLstReadOnly.Items(intIndex).Value & "'") > 0 Then
    126.                                 'Do nothing
    127.                             Else
    128.                                 'Insert record which had just been selected
    129.                                 strSql = "INSERT INTO xaFuncPos (ModuleType, PositionCd, AccessType, DteUpd, UserId) VALUES " & _
    130.                                           "(" & intSelectedFunctionIndex & ",'" & chkLstReadOnly.Items(intIndex).Value.ToString & "','R',GetDate(), " & Session("UserPK") & ")"
    131.                                 clsPraxisDB.execNonQuery(strSql)
    132.                             End If
    133.                         ElseIf (InStr(strUpdateList, "'" & chkLstUpdate.Items(intIndex).Value & "'") > 0) Or _
    134.                               (InStr(strReadOnlyList, "'" & chkLstReadOnly.Items(intIndex).Value & "'") > 0) Then
    135.                             'Delete access for that position to the selected function
    136.                             strSql = "DELETE FROM xaFuncPos WHERE (ModuleType = " & intSelectedFunctionIndex & _
    137.                                     ") AND (PositionCd = '" & chkLstReadOnly.Items(intIndex).Value & "')"
    138.                             clsPraxisDB.execNonQuery(strSql)
    139.                         End If
    140.                     Next
    141.                     'Close connection here before calling a new function
    142.                     clsPraxisDB.closeSQLConnection()
    143.                     Call PopulateScreenValues(intSelectedFunctionIndex)
    144.                 End If
    145.             Else
    146.                 Response.Write("<b>" & strErrorMessage)
    147.             End If
    148.         End If
    149.         clsPraxisDB = Nothing
    150.     End Sub

    The crazy thing is that it only happens after we have made about 8-10 transactions....then when we get the error we cant get back to the page for a good 2 hours.

    I think Satan wrote this code.
    You are living a pacifist dream, and if you dreaming it means you sleeping and you should damn well wake up!

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Put that stuff in a try/catch block to get a better idea of what & where the problem is.

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