I'm always getting error 3704. Operation is not allowed when object is closed.
Dim myConnection2, RSTitleList2
Set myConnection2 = CreateObject("ADODB.Connection")
Set RSTitleList2 = CreateObject("ADODB.Recordset")
myConnection2.Open "<%=connectionString%>"
sSQL1 = "Update FileInformation SET Status = 4 Where DataDefinitionID = 147 AND CustomerID = 71"
RSTitleList2.open sSQL1, myConnection2
if RSTitleList2.BOF and RSTitleList2.EOF then
msgbox("INSERT SUCCESSFUL")
frmProcess.cmdPublish.disabled = true
Else
msgbox("Not SUCCESSFUL")
msgbox(err.Number & " | " & err.description & " | " & err.Source)
End IF
msgbox(err.Number & " | " & err.description & " | " & err.Source)
It would help if you gave us the line where the line number which comes with the error message. However I can see one rather strange thing in your code
myConnection2.Open "<%=connectionString%>"
Why is "connectionstring" inside <%= %>. That syntax is used when you want display an asp variable within your html, eg
<h1>Welcome, <%= username %></h1>
When you're already in a block of asp code, just try
myConnection2.Open connectionString
Related
I have the following code:
Public Function UltimoIngreso() As Date
Try
UltimoIngreso = Now.Date 'I am not ALWAYS getting the current date and time
' --other lines of code
Catch ex As Exception
' An error has occured so display an error message.
ReportBDLog.ReportErrorBD(ex, Me.GetType.ToString & "." & "UltimoIngreso" & vbCrLf & "Message: " & ex.Message & vbCrLf & "Source: " & ex.Source & vbCrLf, System.Reflection.MethodBase.GetCurrentMethod.ToString)
End Try
End Function
Sometimes, but not always, I get "Object reference not set to an instance of an object."
How do I ensure that Function UltimoIngreso always returns current date and time?
What am I missing?
This Function is used in an asp.net App.
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
My code updates & inserts into a bunch of tables. About 2 months ago I got the same error as the one below, I assumed it was because the exception it self is Nothing.
So I built in a test to check if it isn't nothing, write the exception details.
However, the error in the screenshot occurred again this morning. My question now is how does a code block like the one below result in the Exception being Null, and in which cases will an exception be Null?
Edited - This is different to this question because I want to know how an EXCEPTION can be Null/Nothing
Try
If t.DpaPosted = "N" Then
i += 1
OutputCount += 1
Dim SubAccountID As Integer = 0
If t.SubaccountId = 0 Then
SubAccountID = GetSubAccountID(t.DpaInId)
If SubAccountID = 0 Then
If DupAcc > 1 Then
UpdateLog(t.clientReference & " - Multiple CLOSED(Paid Up/Refunds outstanding) accounts.")
Else
SubAccountID = GetClosedSubAccountID(t.DpaInID)
If SubAccountID = 0 Then
UpdateLog(t.clientReference & " - No accounts found.")
End If
End If
End If
End If
If SubAccountID <> 0 Then
If t.DpaDocNo <> "0" Then
If q.Execquery("SELECT COUNT(1) FROM Transactions WHERE SubAccountID = " & t.SubaccountId & " AND TranTypeID = 6 AND TranAmount = " & t.DpaAmount & " AND DPANo = '" & t.DpaDocNo & "'") > 0 Then
HttpContext.Current.Response.Write("Sequence [" & t.DpaDocNo & "] already posted.<br/>")
dpaadapter.SetDpaPosted(t.DpaInId)
Else
PostDpa(SubAccountID, 6, t.DpaAmount, t.Dpadate, "0", t.DpaDocNo)
dpaadapter.SetDpaPosted(t.DpaInId)
End If
Else
PostDpa(SubAccountID, 6, t.DpaAmount, t.Dpadate, "0", t.DpaDocNo)
dpaadapter.SetDpaPosted(t.DpaInId)
End If
End If
If WriteOutput Then HttpContext.Current.Response.Write("Imported [" + i.ToString + "] of [" + dpa.Rows.Count.ToString + "]<br/>")
If OutputCount = 10 Then
OutputCount = 0
If WriteOutput Then
If HttpContext.Current.Response.IsClientConnected Then
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.Write("<script>window.scrollTo(0,document.body.scrollHeight);</script>")
HttpContext.Current.Response.Flush()
End If
End If
End If
End If
Catch ex As Exception
Dim LogStr As String = ""
LogStr = "Record [" & i.ToString & "]. "
LogStr = "Ref [" & t.clientReference & "] - "
If ex.Message.ToString <> Nothing Then LogStr = LogStr & ex.Message.ToString Else LogStr = LogStr & "No Exception Message"
If ex.InnerException.ToString <> Nothing Then LogStr = LogStr & vbCrLf & ex.InnerException.ToString Else LogStr = LogStr & "No Inner Exception"
UpdateLog(LogStr)
SaveLog()
End Try
I can't actually see what code is triggering the exception, until I can fix the exception handling.
Some more information that might help
I am using asp.net V 4.0
The code is in a class, and the file is located in the appcode directory.
The issue doesn't always trigger
When running the code for a second/third time, the exception doesn't trigger again. So I can't recreate the error all the time.
Windows event viewer didn't log any exceptions for this page.
ANSWER
Found from this question.
An inner exception is the exception that caused the current exception.
It is used in cases when you want to surface a different exception
than the one that your code caught but you don't want to throw away
the original context.
In order for a new exception to have information about a previous one,
like you said, you pass it as constructor parameter to the new one.
Usually, a null inner exception means that the current exception is
root cause of the exceptional situation.
How about safeguarding your ex.InnerException? Once you make sure that an InnerException exists then you can access its methods and properties.
If ex.InnerException Is Not Nothing AndAlso ... Then
See this explanation (and read the accepted answer) about InnerException. It is not necessary to have an InnerException, unless the current exception was caused by another exception.
I am using the following code to update an Access Database with Classic Asp:
<%# Language=VBScript %>
<% Option Explicit %>
<%
Response.Buffer = True
'First, we need to get the total number of items that could be updated
Dim iCount
iCount = Request("Count")
'We need to obtain each cost and ID
Dim strstudent, strcourse, strgrade, strcomments
'We will also need to build a SQL statement
Dim strSQL
Dim conn
set conn=server.CreateObject("ADODB.connection")
conn.ConnectionString="provider=Microsoft.jet.OLEDB.4.0;data source=C:\db\agsystem.mdb"
conn.Open
'Now, we want to loop through each form element
Dim iLoop
For iLoop = 0 to iCount
'student data
strstudent = Request(iLoop & ".Student")
'course data
strcourse = Request(iLoop & ".course")
'grade
if isNull(Request(iLoop & ".grade")) or Request(iLoop & ".grade")="" then
strgrade="null"
else
strgrade= Request(iLoop & ".grade")
end if
if isNull(Request(iLoop & ".comments")) or Request(iLoop & ".comments")="" then
strcomments=null
else
strcomments=Request(iLoop & ".comments")
end if
strSQL = "UPDATE testing SET semester2 = " & strgrade & ", commentss=" & "'" & strcomments & "'" & " WHERE newstudentid = " &"'"& strstudent&"'" & " and Courseid = " & "'"& strcourse & "'"
conn.Execute strSQL
Next
conn.Close
Set conn = Nothing
Response.Redirect "protected.asp"
%>
The problem is that when tested in the server it updates without any issues. But when access from a wireless network it won't update.
The target table to update has about 27,000 records
I need to know what I'm doing wrong or if there is another approach.
I found the error after carefully analyzing the situation.
Records in primary key that have spaces for example '2 OR 13' will not update. But records without spaces in primary key like '2CEN13' updates perfectly. I did not had time to solve it in my asp code, so i edited all records with spaces and that solve the problem.
I have a web app with a form that I am trying to pass to an ASP.NET server (using VB.NET) and then on to a MS SQL Server table. The form uses a jQuery datepicker in several textboxes and formats them as MM/dd/yyyy. The form fields are then passed through a PageMethod to the web server which takes the various field values and combines them into a SQL UPDATE command.
I am constantly getting the following error whenever I try to execute the SQL command:
Conversion failed when converting date and/or time from character string.
Here is the code on the server:
Using myConn As New System.Data.SqlClient.SqlConnection(CString.ConnectionString)
myConn.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("UPDATE table " & _
"SET type = '" & type & "', " & _
"target = '" & "#target" & "', " & _
"patient = '" & patient & "', " & _
"dob = '" & "#dob" & "' " & _
"WHERE serial = '" & serial & "'", myConn)
cmd.Parameters.Add(SqlParameter("#target", Data.SqlDbType.Date))
cmd.Parameters.Add(SqlParameter("#dob", Data.SqlDbType.Date))
If target = "" Then
cmd.Parameters("#target").Value = Data.SqlTypes.SqlDateTime.Null
Else
cmd.Parameters("#target").Value = target
End If
If dob = "" Then
cmd.Parameters("#dob").Value = Data.SqlTypes.SqlDateTime.Null
Else
cmd.Parameters("#dob").Value = dob
End If
cmd.ExecuteNonQuery()
End Using
Note: I've tried about twenty different ways of parsing the dates, converting them to dates, changing around the formats and none of it has worked.
Note 2: The conditional statements at the end are simply to prevent empty date fields from being stored in the SQL DB as "1/1/1900", but rather as an actual SQL NULL value. From debugging though, it seems that this is not the issue - it is when there is an actual value that the error is fired.
If anyone can see what I'm doing wrong and how I might fix it, it would be greatly appreciated. Thanks in advance for your help!
You are mixing up your parameterized and non-parameterized parts (why aren't you parameterizing everything?)
Dim cmd As New System.Data.SqlClient.SqlCommand("UPDATE table " & _
"SET type = '" & type & "', " & _
"target = #target, " & _
"patient = '" & patient & "', " & _
"dob = #dob " & _
"WHERE serial = '" & serial & "'", myConn)
Are you including time? DateTime fields require date and time.
I am trying to maintain a log field (fldUserLog ) of my database table so that when updating each raw, the log field will be amended with given log string.
Log string
strUserLog = "Added by : " & Session("auth_Id") & " at " & Now() & " from IP " &
Request.ServerVariables("REMOTE_ADDR") & vbCrLf
and I am using SQL command parameters to UPDATE query. as follows
strSQLQuery = "UPDATE myTable SET " _
& "fldTitle = #xTitle, " _
& "fldDesc = #xDesc, " _
& "fldUserLog = fldUserLog + #xUserLog " _
& "WHERE fldId = #xId ;"
strMessage = "updated"
ObjAddDB.setCommand(strSQLQuery)
With ObjAddDB
.setParameters("#xTitle", frmTitle.Text)
.setParameters("#xDesc", frmDesc.Text)
.setParameters("#xUserLog", strUserLog)
.setParameters("#xId", MyItemId)
End With
Please note that setCommand and setParameters are my own methods I am using in my database.vb class file.
I get following error when its executed
Exception Details:
System.Data.SqlClient.SqlException:
Incorrect syntax near 'fldUserLog'.
please help me to use my UPDATE query to amend existing data with command parameters.
If the format of the fldUserLog field value contains spaces, you need to embrace the value with [ ] ..
& "fldUserLog = [fldUserLog #xUserLog] " _
I guess what you may want to write is the following:
& "fldUserLog = #xUserLog " _