Parameterized Query in VB.net with Null Date in Oracle - oracle

I have the following VB.Net Code to insert a row in an Oracle Table
SQL = "INSERT INTO Roles_ (ACTIVE,ACTIVATIONDATE,DEACTIVATIONDATE,CANBEDELETED,FRIENDLYNAME) VALUES (:ACTIVE, :ACTIVATIONDATE, :DEACTIVATIONDATE, :CANBEDELETED, :FRIENDLYNAME)"
Command.CommandText = SQL
Command.Parameters.Add("ACTIVE", IIf(Active, "y", "n"))
Command.Parameters.Add("CANBEDELETED", IIf(CanBeDeleted, "y", "n"))
Command.Parameters.Add("FRIENDLYNAME", RoleFiendlyName)
If IsNothing(ActivationDate) Then
Using Param As OracleParameter = New OracleParameter()
Param.ParameterName = "ACTIVATIONDATE"
Param.DbType = DbType.DateTime
Param.Value = DBNull.Value
Command.Parameters.Add(Param)
End Using
Else
Command.Parameters.Add("ACTIVATIONDATE", ActivationDate)
End If
If IsNothing(DeactivationDate) Then
Using Param As OracleParameter = New OracleParameter()
Param.ParameterName = "DEACTIVATIONDATE"
Param.DbType = DbType.DateTime
Param.Value = DBNull.Value
Command.Parameters.Add(Param)
End Using
Else
Command.Parameters.Add("DEACTIVATIONDATE", DeactivationDate)
End If
Try
Command.ExecuteNonQuery()
Catch Ex As OracleException
msgbox(Ex.Message)
Catch Ex As Exception
msgbox(Ex.Message)
End Try
The values of the parameters are :
Active = true 'Boolean
CanBeDeleted = True 'Boolean
FriendlyName = "Test" 'String
ActivationDate = nothing 'Date
DeactivationDate = nothing 'Date
but when I run the code I receive the error : "ORA-01858: a non-numeric character was found where a numeric was expected"
Does anyone know what is wrong with this code please ?
Thanks.

Since the parameters are not added in the same order as in the SQL query, the command should requires binding by name.
Command.Bindbyname = true

Related

Updating DataTable inside as DataSet

In an accounting WPF app we have a number of public DataTables that reload data once a tab is opened. This works great locally as the reports just refer to those tables..
For some more complex reports we have just set up a Web Service to run them and return a PDF... To make it easier I thought of loading these DataTables into a DataSet and then extracting them at the server end. This works perfectly the first time it's run, but then (as best as I can work out so far) it's sending the incorrect (last loaded) data for subsequent reports. Locally it still works as it should but the DataTables we are sending in the DataSet appear to not be updating. Have tried to remove the tables and re-add them again, but that just throws a 'object reference not set' the first time a DataRow is selected from a DataTable at the server.
Must be something very basic I have been missing :-(
Any ideas?
Thanks
Private Async Sub RunProfitAndLossOnServer(sender As Object, e As RoutedEventArgs)
Try
Dim StartDateTB As DateTBx = ReportBalanceSheet_Grid.FindName("BalanceSheet_StartDateTB")
Dim EndDateTB As DateTBx = ReportBalanceSheet_Grid.FindName("BalanceSheet_EndDateTB")
ReportStartDate = StartDateTB.Value
ReportEndDate = EndDateTB.Value
ReportDate = Today
ReportName = "Profit and Loss Report"
PaperLandscape = False
ReportFontSize = 8
PopupModals_ReportGenerator()
If ReportGeneratorRun = True Then
Dim vPDF() As Byte = Nothing
'Determine if the Export Dataset has already been created
If ExportDS Is Nothing Then
ExportDS = New DataSet
VariablesDT = New DataTable
With VariablesDT.Columns
.Add("Current_HOA_Name", GetType(String))
.Add("Current_HOA_ID", GetType(Integer))
.Add("ReportName", GetType(String))
.Add("ReportFontSize", GetType(Integer))
.Add("ReportStartDate", GetType(Date))
.Add("ReportEndDate", GetType(Date))
.Add("User_InternationalDate", GetType(Integer))
.Add("PaperSize", GetType(String))
.Add("PaperLandscape", GetType(Boolean))
.Add("Reports_IsSplitGL", GetType(Boolean))
.Add("Form_ID", GetType(Integer))
End With
With VariablesDT.Rows
.Add(Current_HOA_Name, Current_HOA_ID, ReportName, ReportFontSize, ReportStartDate, ReportEndDate, User_InternationalDate, PaperSize, PaperLandscape, False, Form_ID)
End With
VariablesDT.TableName = "VariablesDT"
ExportDS.Tables.Add(VariablesDT)
ResIncomeDT.TableName = "ResIncomeDT"
ExportDS.Tables.Add(ResIncomeDT)
ResIncNomDT.TableName = "ResIncNomDT"
ExportDS.Tables.Add(ResIncNomDT)
ResExpensesDT.TableName = "ResExpensesDT"
ExportDS.Tables.Add(ResExpensesDT)
ResExpNomDT.TableName = "ResExpNomDT"
ExportDS.Tables.Add(ResExpNomDT)
RevenueDT.TableName = "RevenueDT"
ExportDS.Tables.Add(RevenueDT)
ExpensesDT.TableName = "ExpensesDT"
ExportDS.Tables.Add(ExpensesDT)
RevenueNomDT.TableName = "RevenueNomDT"
ExportDS.Tables.Add(RevenueNomDT)
ExpensesNomDT.TableName = "ExpensesNomDT"
ExportDS.Tables.Add(ExpensesNomDT)
Else
ExportDS.Tables.Remove("ResIncomeDT")
ExportDS.Tables.Remove("ResIncNomDT")
ExportDS.Tables.Remove("ResExpensesDT")
ExportDS.Tables.Remove("ResExpNomDT")
ExportDS.Tables.Remove("RevenueDT")
ExportDS.Tables.Remove("ExpensesDT")
ExportDS.Tables.Remove("RevenueNomDT")
ExportDS.Tables.Remove("ExpensesNomDT")
ExportDS.Tables.Add(ResIncomeDT)
ExportDS.Tables.Add(ResIncNomDT)
ExportDS.Tables.Add(ResExpensesDT)
ExportDS.Tables.Add(ResExpNomDT)
ExportDS.Tables.Add(RevenueDT)
ExportDS.Tables.Add(ExpensesDT)
ExportDS.Tables.Add(RevenueNomDT)
ExportDS.Tables.Add(ExpensesNomDT)
ExportDS.Tables.Remove("VariablesDT")
For Each row As DataRow In VariablesDT.Rows
row("Current_HOA_Name") = Current_HOA_Name
row("current_HOA_ID") = Current_HOA_ID
row("ReportFontSize") = ReportFontSize
row("User_InternationalDate") = User_InternationalDate
row("PaperSize") = PaperSize
row("PaperLandscape") = PaperLandscape
row("Reports_IsSplitGL") = False
row("ReportStartDate") = ReportStartDate
row("ReportEndDate") = ReportEndDate
row("ReportName") = ReportName
row("Form_ID") = Form_ID
Next
ExportDS.Tables.Add(VariablesDT)
End If
Dim vImage As New LoadingImage
LoadingStarted("Uploading to xSoftware... Please wait...", vImage)
Await Task.Run(Sub()
Using vService As New Service5Client
vPDF = vService.ReturnProfitAndLossSheet(ExportDS)
End Using
End Sub)
LoadingCompleted("File uploaded to xSoftware...", "File was uploaded and PDF returned...", vImage)
Dim vFile As String = ByteToFilePath(vPDF)
If System.IO.File.Exists(vFile) Then
Dim P As New Process
With P
.StartInfo.FileName = vFile
.StartInfo.Verb = "Open"
.Start()
End With
Else
AppBoxError("The file path for the PDF is not valid!")
End If
End If
Catch ex As Exception
EmailError(ex)
End Try
End Sub
Throws an error at the server end if we remove and add the datatables back to the DataSet
Dim RevenueData() As DataRow = RevenueDT.Select("FormID = " & Form_ID, "Position")
Dim ExpenseData() As DataRow = ExpensesDT.Select("FormID = " & Form_ID, "Position")
Dim RevenueNomData() As DataRow = RevenueNomDT.Select("FormID = " & Form_ID, "Position")
Dim ExpenseNomData() As DataRow = ExpensesNomDT.Select("FormID = " & Form_ID, "Position")
It looks like the answer is
Remove the DataTables from the DataSet and add them again (which got us part of the way there)
Name the DataTables again!
ExportDS.Tables.Remove("ResIncomeDT")
ExportDS.Tables.Remove("ResIncNomDT")
ExportDS.Tables.Remove("ResExpensesDT")
ExportDS.Tables.Remove("ResExpNomDT")
ExportDS.Tables.Remove("RevenueDT")
ExportDS.Tables.Remove("ExpensesDT")
ExportDS.Tables.Remove("RevenueNomDT")
ExportDS.Tables.Remove("ExpensesNomDT")
ExportDS.Tables.Remove("VariablesDT")
VariablesDT.TableName = "VariablesDT"
ExportDS.Tables.Add(VariablesDT)
ResIncomeDT.TableName = "ResIncomeDT"
ExportDS.Tables.Add(ResIncomeDT)
ResIncNomDT.TableName = "ResIncNomDT"
ExportDS.Tables.Add(ResIncNomDT)
ResExpensesDT.TableName = "ResExpensesDT"
ExportDS.Tables.Add(ResExpensesDT)
ResExpNomDT.TableName = "ResExpNomDT"
ExportDS.Tables.Add(ResExpNomDT)
RevenueDT.TableName = "RevenueDT"
ExportDS.Tables.Add(RevenueDT)
ExpensesDT.TableName = "ExpensesDT"
ExportDS.Tables.Add(ExpensesDT)
RevenueNomDT.TableName = "RevenueNomDT"
ExportDS.Tables.Add(RevenueNomDT)
ExpensesNomDT.TableName = "ExpensesNomDT"
ExportDS.Tables.Add(ExpensesNomDT)
Now it appears to be working correctly :-)

Dreamweaver ASP update record not passing query string

I am trying to update a record in a SQL database using Dreamweaver which work fine on its own, but I want the goto url to show the updated product record.
I have tried to figure this out using some online tutorials but keep coming up with this error
Microsoft VBScript runtime error '800a01a8'
Object required: ''
/coding_requests/dashboard.asp, line 33
This is my code;
<%
If (CStr(Request("MM_update")) = "coding_technical_code") Then
If (Not MM_abortEdit) Then
' execute the update
Dim MM_editCmd
Set MM_editCmd = Server.CreateObject ("ADODB.Command")
MM_editCmd.ActiveConnection = MM_conn_PSCRM_Demo_STRING
MM_editCmd.CommandText = "UPDATE DBA.coding_requests SET prodref = ?, lastupdatedby = ?, coding_status = ? WHERE prodref = ?"
MM_editCmd.Prepared = true
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 201, 1, 25, Request.Form("prodref")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 201, 1, 3, Request.Form("lastupdatedby")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param3", 201, 1, 6, Request.Form("coding_status")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param4", 200, 1, 25, Request.Form("MM_recordId")) ' adVarChar
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
' append the query string to the redirect URL
Dim MM_editRedirectUrl
MM_editRedirectUrl = "coding.asp?prodref=" & (RS_Dashboard_Coding_TechnicalView.Fields.Item("prodref").Value)
If (Request.QueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0) Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
End If
End If
Response.Redirect(MM_editRedirectUrl)
End If
End If
%>
Any suggestions?
The reason for this error is simply that line 33 contains an Object Reference (which is a variable that is created with Set variablename =) but the runtime cannot identify it as an Object Reference that has been created (instantiated).
The line 33 as explained in the comments points to
MM_editRedirectUrl = "coding.asp?prodref=" & (RS_Dashboard_Coding_TechnicalView.Fields.Item("prodref").Value)
The Object Reference on this line is RS_Dashboard_Coding_TechnicalView but no where in the provided code is the Object Reference RS_Dashboard_Coding_TechnicalView instantiated using
Set RS_Dashboard_Coding_TechnicalView = Server.CreateObject("ADODB.Recordset")
The above line is an educated guess based on the name of the Object Reference and the methods you are attempting to use (.Fields.Item()).
If you are just trying to pass the value of prodref from your Form POST to the MM_editRedirectUrl variable as #SearchAndResQ suggests here just pass the value of Request.Form("prodref") which you already have to hand so that line would be;
MM_editRedirectUrl = "coding.asp?prodref=" & Request.Form("profref")

"Not all variables are bound" Error when passing parameters

I'm using PL/SQL and I get the "Not all variables are bound" when I execute the sql expression. I'm selecting data by passing values into 3 parameters. Therefore "host variables" do not help me in here.
SELECT SomeApi1_Api.Get_Part_No(t.or_no, t.l_no, t.rel_no, t.item_no),
t.description,
c.serial_no,
c.lot_batch_no,
t.order_no,
t.line_no,
t.release_no,
SomeApi1_Api.Get_State(t.or_no, t.l_no, t.rel_no, t.item_no)
FROM TableView1 c,
TableView2 t
WHERE c.or_no = t.or_no
AND c.l_no = t.l_no
AND c.rel_no = t.rel_no
AND c.item_no = t.item_no
AND deliv_no IN (SELECT deliv_no
FROM TableView3 a
WHERE a.del_no IN (SELECT DISTINCT dele_no
FROM TableView3 cod,
TableView4 cdi
WHERE cod.deliv_no = cdi.deliv_no
AND cdi.i_id = t.i_id
AND cdi.item_id = t.item_id
AND cdi.co = t.co
AND a.or_no = t.or_no
AND a.l_no = l_no
AND a.rel_no = rel_no
AND a.item_no = item_no))
AND t.i_id = '&A'
AND t.item_id = '&B'
AND t.co = '&C'

LINQ Subquery issue

Following is my linq query:
var varResourceStatusReportDataBase =
(
from content in listContent
join workflowInstance in listWorkflkowInstance
on content.Field<string>("ows_ID").Trim()
equals workflowInstance.Field<string>("ows_Content ID").Split(';')[0].Trim()
join WorkflowInstanceStep in listWorkflowInstanceStep
on workflowInstance.Field<string>("ows_ID")
equals WorkflowInstanceStep.Field<string>("ows_Workflow Instance ID").Split(';')[0]
select new
{
ContentName = content.Field<string>("ows_Name"),
WorkflowInstanceId = workflowInstance.Field<string>("ows_ID"),
WIName = workflowInstance.Field<string>("ows_Title"),
WIPlannedStartDate = workflowInstance.Field<string>("ows_Planned Start Date") ?? "",
WIPlannedEndDate = workflowInstance.Field<string>("ows_Planned End Date") ?? "",
WIActualStartDate = workflowInstance.Field<string>("ows_Actual Start Date") ?? "",
WIActualEndDate = workflowInstance.Field<string>("ows_Actual End Date") ?? "",
WIApprovalDate = workflowInstance.Field<string>("ows_Approval Date") ?? "",
WITaskStatus = workflowInstance.Field<string>("ows_Status").ToUpper() ?? "",
WIMetadataStatus = workflowInstance.Field<string>("ows_Metadata Status") ?? "",
WIApprover = workflowInstance.Field<string>("ows_Approver").Replace("#", "").Split(';')[1].ToUpper() ?? "",
WISResponsible = WorkflowInstanceStep.Field<string>("ows_Responsible").Replace("#", "").Split(';')[1].ToUpper() ?? "",
WISDesiredEndDate=
(
from WorkflowInstanceStep1 in listWorkflowInstanceStep
where (WorkflowInstanceStep1.Field<string>("ows_Status") =="IN PROGRESS" ||
WorkflowInstanceStep1.Field<string>("ows_Status") =="ASSIGNED") &&
workflowInstance.Field<string>("ows_ID") == WorkflowInstanceStep1.Field<string>("ows_Workflow Instance ID").Split(';')[0]
select new {abc = WorkflowInstanceStep1.Field<string>("ows_Desired End Date")}).Take(1)
}).Distinct();
I have sub-query in above query to calculate WISDesiredEndDate, but when I execute this query, I get System.Linq.Enumerable+TakeIterator>d__3a'1[<>f__AnonymousType1'1[System.String]] value for WISDesiredEndDate column not actual value which I want.
Please give suggestion on the same.
Thanks.
You want FirstOrDefault() rather than Take(1)
As from comments, you don't want a anonymous object so change the select to be
select WorkflowInstanceStep1.Field<string>("ows_Desired End Date")}).FirstOrDefault()
You can us let clause more readable code. Nevertheless you have to force linq expression execution by calling FirstOrDefault method
let desiredEndDate = (from WorkflowInstanceStep1 in listWorkflowInstanceStep .... Take(1))
select new
{
...
WISDesiredEndDate= desiredEndDate.FirstOrDefault()
}

Can't force LINQ evaluation

I'm trying to get a record and store it for later use if an error occurs on the page.
This code is used on Create/Edit. So I'm getting values from a View Model and mapping to the db entity:
Dim objExistingSupplier As SUPPLIER = db.SUPPLIER.Single(Function(e) e.SUPPLIER_ID = Supplier.SUPPLIER_ID)
objPermStaffAction = objExistingSupplier
objSupplier = Mapper.Map(Of SupplierViewModel, SUPPLIER)(Supplier, objExistingSupplier)
From what I understand, the .Single should force evaluation. However, after the mapping occurs properties are lost/changed in the objPermStaffAction object. Can someone explain what I'm doing wrong?
Here is the whole function:
Private Function SaveSupplier(Supplier As SupplierViewModel) As ActionResult
Dim objSupplier, objPermStaffAction As SUPPLIER
If Supplier.SUPPLIER_ID = 0 Then
objSupplier = Mapper.Map(Of SupplierViewModel, SUPPLIER)(Supplier)
Else
Dim objExistingSupplier As SUPPLIER = db.SUPPLIER.Single(Function(e) e.SUPPLIER_ID = Supplier.SUPPLIER_ID)
objPermStaffAction = objExistingSupplier
objSupplier = Mapper.Map(Of SupplierViewModel, SUPPLIER)(Supplier, objExistingSupplier)
End If
If ModelState.IsValid Then
If Supplier.SUPPLIER_ID = 0 Then
objSupplier.CREATED_BY = Session("AppUserID")
db.SUPPLIER.Add(objSupplier)
Else
objSupplier.UPDATED_BY = Session("AppUserID")
UpdateModel(objSupplier)
End If
Try
db.SaveChanges()
Return RedirectToAction("Index")
Catch ex As Exception
If InStr(ex.InnerException.InnerException.Message, "PRSNL.SUPPLIER_UK") > 0 Then
ModelState.AddModelError("SUPPLIER_CODE", "Supplier Code already exists. Please choose another.")
End If
End Try
End If
'This will run if an error occured
If Supplier.SUPPLIER_ID > 0 Then
Supplier = Mapper.Map(Of SupplierViewModel)(objPermStaffAction)
End If
ViewBag.YNList = Common.GetYNList
Return View(Supplier)
End Function
SupplierViewModel is reference type. So objPermStaffAction refer to the same object which is getting updated after mapping.
Why do you need original (not mapped) object?

Resources