EF query hits database more than one time - vb.net
maybe is a simple issue but I'm pretty new to EF so:
I have this query:
Dim InvoicesQuery = (From i As Invoice In dbContext.Invoices.AsNoTracking() Where i.InvoiceNumber = -1
Select New InvoicesWithDetails With {
.InvoiceDetails = i.InvoicesDetails,
.InvoiceDetailsUnmerged = i.InvoicesDetailsUnmergeds,
.Examination = Nothing,
.Applicant = Nothing,
.InvoiceNumber = i.InvoiceNumber,
.InvoiceYear = i.InvoiceYear,
.DocCode = i.DocCode,
.CompanyId = i.CompanyId,
.InvoiceDate = i.InvoiceDate,
.NetAmount = i.NetAmount,
.AmountPaid = i.AmountPaid,
.AmountToPay = i.AmountToPay,
.VAT = i.VAT,
.AdditionalTax = i.AdditionalTax,
.Status = i.Status,
.Amount = i.Amount,
.ExaminationId = i.ExaminationId,
.ReceiverName = If(i.OtherReceiverName Is Nothing, i.ReceiverName, i.OtherReceiverName),
.ReceiverAddress = If(i.OtherReceiverAddress Is Nothing, i.ReceiverAddress, i.OtherReceiverAddress),
.ReceiverCity = If(i.OtherReceiverCity Is Nothing, i.ReceiverCity, i.OtherReceiverCity),
.ReceiverTaxCode = If(i.OtherReceiverTaxCode Is Nothing, i.ReceiverTaxCode, i.OtherReceiverTaxCode),
.ReceiverCompanyTaxCode = i.ReceiverCompanyTaxCode,
.ReceiverZipCode = If(i.OtherReceiverZipCode Is Nothing, i.ReceiverZipCode, i.OtherReceiverZipCode),
.IsCreditNote = i.IsCreditNote,
.HasCreditNote = i.HasCreditNote,
.FixedAdditionalAmountOnInvoice = i.FixedAdditionalAmountOnInvoice,
.CashFlowPaymentModeId = If(i.CashFlowPaymentModeId IsNot Nothing, i.CashFlowPaymentModeId.Trim, Nothing),
.AgreementDeductible = i.AgreementDeductible,
.IsPatientInvoiceRecipient = i.IsPatientInvoiceRecipient,
.IsApplicantInvoiceRecipient = i.IsApplicantInvoiceRecipient,
.IsDoctorInvoiceRecipient = i.IsDoctorInvoiceRecipient,
.DoctorId = If(i.DoctorId IsNot Nothing, i.DoctorId, Nothing),
.Doctor = Nothing,
.CreditNoteParentInvoice = i.CreditNoteParentInvoice,
.CreditNoteParentInvoiceYear = i.CreditNoteParentInvoiceYear,
.IsDeferredInvoice = i.IsDeferredInvoice,
.IsExtracted = If(i.IsExtracted IsNot Nothing, i.IsExtracted, False),
.IsExportedToHOST = If(i.IsExportedToHOST IsNot Nothing, i.IsExportedToHOST, False),
.ApplicantId = i.ApplicantId,
.InvoiceHasVAT = i.InvoiceHasVAT,
.VAT_Percentage = i.VAT_Percentage,
.IssuedBy = i.IssuedBy,
.MaskedCounter = i.MaskedCounter,
.DocTypeId = i.DocTypeId,
.OtherReceiverName = i.OtherReceiverName,
.OtherReceiverAddress = i.OtherReceiverAddress,
.OtherReceiverCity = i.OtherReceiverCity,
.OtherReceiverTaxCode = i.OtherReceiverTaxCode,
.OtherReceiverZipCode = i.OtherReceiverZipCode,
.CashFlows = i.CashFlows}).ToList
InvoicesWithDetails is a class that hinerits Invoice; Invoice is a db entity
After this call I loop through the results like this:
For Each i As InvoicesWithDetails In InvoicesQuery.Where(Function(iwd) iwd.CompanyDescription Is Nothing And iwd.CompanyId IsNot Nothing)
i.CompanyDescription = ReturnCompanyDescription(i.CompanyId)
Next
For each cycle in the loop the database is called.
I thought that after the .ToList in the first call I could iterate the results without invoke the database.
What is wrong?
Thanks in advance
Ok, I worked more on that and I tried a different approach:
This is the new query, targeting the Invoice entity:
Dim InvoicesQuery As List(Of Invoice)
InvoicesQuery = (From i As Invoice In dbContext.Invoices.AsNoTracking Where
i.InvoiceDate >= FromDate And i.InvoiceDate <= ToDate).ToList
After that I create a new istance of InvoiceWithDetails and set all the properties like this:
For Each i As Invoice In InvoicesQuery
Dim newInvoiceWithDetails As New InvoicesWithDetails
With newInvoiceWithDetails
.InvoiceDetails = i.InvoicesDetails
.InvoiceDetailsUnmerged = i.InvoicesDetailsUnmergeds
.Examination = Nothing
.Applicant = Nothing
.InvoiceNumber = i.InvoiceNumber
.InvoiceYear = i.InvoiceYear
.DocCode = i.DocCode
.CompanyId = i.CompanyId
.InvoiceDate = i.InvoiceDate
.NetAmount = i.NetAmount
.AmountPaid = i.AmountPaid
.AmountToPay = i.AmountToPay
.VAT = i.VAT
.AdditionalTax = i.AdditionalTax
.Status = i.Status
.Amount = i.Amount
.ExaminationId = i.ExaminationId
.ReceiverName = If(i.OtherReceiverName Is Nothing, i.ReceiverName, i.OtherReceiverName)
.ReceiverAddress = If(i.OtherReceiverAddress Is Nothing, i.ReceiverAddress, i.OtherReceiverAddress)
.ReceiverCity = If(i.OtherReceiverCity Is Nothing, i.ReceiverCity, i.OtherReceiverCity)
.ReceiverTaxCode = If(i.OtherReceiverTaxCode Is Nothing, i.ReceiverTaxCode, i.OtherReceiverTaxCode)
.ReceiverCompanyTaxCode = i.ReceiverCompanyTaxCode
.ReceiverZipCode = If(i.OtherReceiverZipCode Is Nothing, i.ReceiverZipCode, i.OtherReceiverZipCode)
.IsCreditNote = i.IsCreditNote
.HasCreditNote = i.HasCreditNote
.FixedAdditionalAmountOnInvoice = i.FixedAdditionalAmountOnInvoice
.CashFlowPaymentModeId = If(i.CashFlowPaymentModeId IsNot Nothing, i.CashFlowPaymentModeId.Trim, Nothing)
.AgreementDeductible = i.AgreementDeductible
.IsPatientInvoiceRecipient = i.IsPatientInvoiceRecipient
.IsApplicantInvoiceRecipient = i.IsApplicantInvoiceRecipient
.IsDoctorInvoiceRecipient = i.IsDoctorInvoiceRecipient
.DoctorId = If(i.DoctorId IsNot Nothing, i.DoctorId, Nothing)
.Doctor = Nothing
.CreditNoteParentInvoice = i.CreditNoteParentInvoice
.CreditNoteParentInvoiceYear = i.CreditNoteParentInvoiceYear
.IsDeferredInvoice = i.IsDeferredInvoice
.IsExtracted = If(i.IsExtracted IsNot Nothing, i.IsExtracted, False)
.IsExportedToHOST = If(i.IsExportedToHOST IsNot Nothing, i.IsExportedToHOST, False)
.ApplicantId = i.ApplicantId
.InvoiceHasVAT = i.InvoiceHasVAT
.VAT_Percentage = i.VAT_Percentage
.IssuedBy = i.IssuedBy
.MaskedCounter = i.MaskedCounter
.DocTypeId = i.DocTypeId
.OtherReceiverName = i.OtherReceiverName
.OtherReceiverAddress = i.OtherReceiverAddress
.OtherReceiverCity = i.OtherReceiverCity
.OtherReceiverTaxCode = i.OtherReceiverTaxCode
.OtherReceiverZipCode = i.OtherReceiverZipCode
.CashFlows = i.CashFlows
End With
FinalList.Add(newInvoiceWithDetails)
For each cycle and each property value, the database is invoked
Again, what is wrong?
Related
VB replacing an object in a list
I have 2 lists approvedSuppliers and originalSupplierData When approved Suppliers gets populated we clone the entry into the originalSupplierData . If they have modified a record but don't save we ask the user if they want to revert the changes . If they want to revert I am trying to replace the entry in approved suppliers with a clone of the original data. My current code for the revert is Public Sub RevertChanges(SupplierID As Integer) Dim orignalSupplier As Approved_Supplier = originalSupplierlist.Where(Function(x) x.ID = SupplierID).Single() Dim modifiedSupplier As Approved_Supplier = ApprovedSuppliers.Where(Function(x) x.ID = SupplierID).Single() modifiedSupplier = orignalSupplier.Clone End Sub The modifiedSupplier gets updated with the original values however the actual item in the list is not updated with the values. If I modify one of the properties the list gets update. I am not sure what i am doing wrong can anyone point me in the right direction please? Edit The code for populating the list from the database is supplierTableAdapter.Fill(supplierTable) _approvedSuppliers = New List(Of Approved_Supplier) originalSupplierlist = New List(Of Approved_Supplier)() For Each row As ApprovedSuppliersDataset.ApprovedSupplierRow In supplierTable supplier = New Approved_Supplier() supplier.supplierID = row.PLSupplierAccountID supplier.AccountNumber = row.SupplierAccountNumber supplier.SupplierName = row.SupplierAccountName supplier.SupplierAddress = CompileAddress(row) supplier.Phone = CompilePhoneNumber(row) If row.IsIDNull = False Then supplier.ID = row.ID If row.IsAdded_ByNull = False Then supplier.AddedBy = row.Added_By End If If row.IsApprovedNull = False Then supplier.Approved = row.Approved End If If row.IsAuditorNull = False Then supplier.Auditor = row.Auditor End If If row.IsAudit_CommentsNull = False Then supplier.AuditComments = row.Audit_Comments End If If row.IsAudit_DateNull = False Then supplier.AuditDate = row.Audit_Date End If If row.IsDate_AddedNull = False Then supplier.DateAdded = row.Date_Added End If If row.IsNotesNull = False Then supplier.Notes = row.Notes End If If row.IsQuestionnaire_Return_DateNull = False Then supplier.QuestionnaireReturnDate = row.Questionnaire_Return_Date End If If row.IsQuestionnaire_Sent_DateNull = False Then supplier.QuestionnaireSentDate = row.Questionnaire_Sent_Date End If If row.IsQuestionnaire_StatusNull = False Then supplier.QuestionnaireStatus = row.Questionnaire_Status End If If row.IsReplinNull = False Then supplier.Replin = row.Replin End If If row.IsReview_CommentsNull = False Then supplier.ReviewComment = row.Review_Comments End If If row.IsReview_DateNull = False Then supplier.ReviewDate = row.Review_Date End If If row.IsReviewerNull = False Then supplier.Reviewers = row.Reviewer End If If row.IsStakeholder_ContactNull = False Then supplier.StakeholderContact = row.Stakeholder_Contact End If If row.IsStandardsNull = False Then supplier.Standards = row.Standards End If If row.IsStandard_ExpiryNull = False Then supplier.StandardExpiry = row.Standard_Expiry End If If row.IsStatusNull = False Then supplier.Status = row.Status End If If row.IsSupplier_Expiry_DateNull = False Then supplier.SupplierExpiryDate = row.Supplier_Expiry_Date End If If row.IsSupplier_ScopeNull = False Then supplier.SupplierScope = row.Supplier_Scope End If If row.Is_T_CsNull = False Then supplier.TC = row._T_Cs End If End If supplier.ClearISDirty() _approvedSuppliers.Add(supplier) originalSupplierlist.Add(supplier.Clone) Next and for the clone we have Public Function Clone() As Object Implements ICloneable.Clone Dim cloned As New Approved_Supplier() cloned.ID = Me.ID cloned.DateAdded = Me.DateAdded cloned.Status = Me.Status cloned.AddedBy = Me.AddedBy cloned.Approved = Me.Approved cloned.AuditDate = Me.AuditDate cloned.Auditor = Me.Auditor cloned.AuditComments = Me.AuditComments cloned.QuestionnaireStatus = Me.QuestionnaireStatus cloned.QuestionnaireSentDate = Me.QuestionnaireSentDate cloned.QuestionnaireReturnDate = Me.QuestionnaireReturnDate cloned.ReviewDate = Me.ReviewDate cloned.Reviewers = Me.Reviewers cloned.ReviewComment = Me.ReviewComment cloned.Standards = Me.Standards cloned.StandardExpiry = Me.StandardExpiry cloned.SupplierScope = Me.SupplierScope cloned.Replin = Me.Replin cloned.TC = Me.TC cloned.Notes = Me.Notes cloned.StakeholderContact = Me.StakeholderContact cloned.SupplierExpiryDate = Me.SupplierExpiryDate cloned.supplierID = Me.supplierID cloned.AccountNumber = Me.AccountNumber cloned.SupplierName = Me.SupplierName cloned.SupplierAddress = Me.SupplierAddress cloned.Phone = Me.Phone cloned.Email = Me.Email cloned.ClearISDirty() Return cloned End Function
You are not replacing in the list by affecting modifiedSupplier. Try by getting the index of the modifiedSupplier and then replacing the item at the found index by your clone. Public Sub RevertChanges(SupplierID As Integer) Dim orignalSupplier As Approved_Supplier = originalSupplierlist.Where(Function(x) x.ID = SupplierID).Single() Dim modifiedIndex As Integer = ApprovedSuppliers.FindIndex(Function(x) x.ID = SupplierID) ApprovedSuppliers(modifiedIndex) = orignalSupplier.Clone() End Sub
Automation Error VBA from Userform
Keep getting an automation error when my userform uses the below code when it initializes. I dont get an error when i take it out. The userform is being called from a shape using a module. the text from the userform is stored under a sheet called "Compliance". My userform is also called compliance. Here is my code below, any help would be much appreciated: Private Sub UserForm_Initialize() Dim ws As Worksheet Set ws = Sheets("compliance") On Error Resume Next With page1 employee1.Value = ws.Range("B2") employee2.Value = ws.Range("B3") employee3.Value = ws.Range("B4") employee4.Value = ws.Range("B5") employee5.Value = ws.Range("B5") employee6.Value = ws.Range("B6") '//// setting ops grade grade1.Value = ws.Range("D2") grade2.Value = ws.Range("D3") grade3.Value = ws.Range("D4") grade4.Value = ws.Range("D5") grade5.Value = ws.Range("D6") grade6.Value = ws.Range("D7") '//// setting employee compliance recap recap1.Value = ws.Range("F2") recap2.Value = ws.Range("F3") recap3.Value = ws.Range("F4") recap4.Value = ws.Range("F5") recap5.Value = ws.Range("F6") recap6.Value = ws.Range("F7") '////////////////////////////////////////// febraury '//// setting employees interviewed With page2 febemp1.Value = ws.Range("B14") febemp2.Value = ws.Range("B15") febemp3.Value = ws.Range("B16") febemp4.Value = ws.Range("B17") febemp5.Value = ws.Range("B18") febemp6.Value = ws.Range("B19") febgrd1.Value = ws.Range("D14") febgrd2.Value = ws.Range("D15") febgrd3.Value = ws.Range("D16") febgrd4.Value = ws.Range("D17") febgrd5.Value = ws.Range("D18") febgrd6.Value = ws.Range("D19") febrecap1.Value = ws.Range("F14") febrecap2.Value = ws.Range("F15") febrecap3.Value = ws.Range("F16") febrecap4.Value = ws.Range("F17") febrecap5.Value = ws.Range("F18") febrecap6.Value = ws.Range("F19") With page3 marchemp1.Value = ws.Range("B26") marchemp2.Value = ws.Range("B27") marchemp3.Value = ws.Range("B28") marchemp4.Value = ws.Range("B29") marchemp5.Value = ws.Range("B30") marchemp6.Value = ws.Range("B31") marchgrd1.Value = ws.Range("D26") marchgrd2.Value = ws.Range("D27") marchgrd3.Value = ws.Range("D28") marchgrd4.Value = ws.Range("D29") marchgrd5.Value = ws.Range("D30") marchgrd6.Value = ws.Range("D31") marchrecap1.Value = ws.Range("F26") marchrecap2.Value = ws.Range("F27") marchrecap3.Value = ws.Range("F28") marchrecap4.Value = ws.Range("F29") marchrecap5.Value = ws.Range("F30") marchrecap6.Value = ws.Range("F31") With page4 apremp1.Value = ws.Range("B38") apremp2.Value = ws.Range("B39") apremp3.Value = ws.Range("B40") apremp4.Value = ws.Range("B41") apremp5.Value = ws.Range("B42") apremp6.Value = ws.Range("B43") aprgrd1.Value = ws.Range("D38") aprgrd2.Value = ws.Range("D39") aprgrd3.Value = ws.Range("D40") aprgrd4.Value = ws.Range("D41") aprgrd5.Value = ws.Range("D42") aprgrd6.Value = ws.Range("D43") aprrecap1.Value = ws.Range("F38") aprrecap2.Value = ws.Range("F39") aprrecap3.Value = ws.Range("F40") aprrecap4.Value = ws.Range("F41") aprrecap5.Value = ws.Range("F42") aprrecap6.Value = ws.Range("F43") With page5 mayemp1.Value = ws.Range("B50") mayemp2.Value = ws.Range("B51") mayemp3.Value = ws.Range("B52") mayemp4.Value = ws.Range("B53") mayemp5.Value = ws.Range("B54") mayemp6.Value = ws.Range("B55") maygrd1.Value = ws.Range("D50") maygrd2.Value = ws.Range("D51") maygrd3.Value = ws.Range("D52") maygrd4.Value = ws.Range("D53") maygrd5.Value = ws.Range("D54") maygrd6.Value = ws.Range("D55") mayrecap1.Value = ws.Range("F50") mayrecap2.Value = ws.Range("F51") mayrecap3.Value = ws.Range("F52") mayrecap4.Value = ws.Range("F53") mayrecap5.Value = ws.Range("F54") mayrecap6.Value = ws.Range("F55") With page6 junemp1.Value = ws.Range("B62") junemp2.Value = ws.Range("B63") junemp3.Value = ws.Range("B64") junemp4.Value = ws.Range("B65") junemp5.Value = ws.Range("B66") junemp6.Value = ws.Range("B67") jungrd1.Value = ws.Range("D62") jungrd2.Value = ws.Range("D63") jungrd3.Value = ws.Range("D64") jungrd4.Value = ws.Range("D65") jungrd5.Value = ws.Range("D66") jungrd6.Value = ws.Range("D67") junrecap1.Value = ws.Range("F62") junrecap2.Value = ws.Range("F63") junrecap3.Value = ws.Range("F64") junrecap4.Value = ws.Range("F65") junrecap5.Value = ws.Range("F66") junrecap6.Value = ws.Range("F67") With page7 julemp1.Value = ws.Range("B74") julemp2.Value = ws.Range("B75") julemp3.Value = ws.Range("B76") julemp4.Value = ws.Range("B77") julemp5.Value = ws.Range("B78") julemp6.Value = ws.Range("B79") julgrd1.Value = ws.Range("D74") julgrd2.Value = ws.Range("D75") julgrd3.Value = ws.Range("D76") julgrd4.Value = ws.Range("D77") julgrd5.Value = ws.Range("D78") julgrd6.Value = ws.Range("D79") julrecap1.Value = ws.Range("F74") julrecap2.Value = ws.Range("F75") julrecap3.Value = ws.Range("F76") julrecap4.Value = ws.Range("F77") julrecap5.Value = ws.Range("F78") julrecap6.Value = ws.Range("F79") With page8 augemp1.Value = ws.Range("B86") augemp2.Value = ws.Range("B87") augemp3.Value = ws.Range("B88") augemp4.Value = ws.Range("B89") augemp5.Value = ws.Range("B90") augemp6.Value = ws.Range("B91") auggrd1.Value = ws.Range("D86") auggrd2.Value = ws.Range("D87") auggrd3.Value = ws.Range("D88") auggrd4.Value = ws.Range("D89") auggrd5.Value = ws.Range("D90") auggrd6.Value = ws.Range("D91") augrecap1.Value = ws.Range("F86") augrecap2.Value = ws.Range("F87") augrecap3.Value = ws.Range("F88") augrecap4.Value = ws.Range("F89") augrecap5.Value = ws.Range("F90") augrecap6.Value = ws.Range("F91") With page9 sepemp1.Value = ws.Range("B98") sepemp2.Value = ws.Range("B99") sepemp3.Value = ws.Range("B100") sepemp4.Value = ws.Range("B101") sepemp5.Value = ws.Range("B102") sepemp6.Value = ws.Range("B103") sepgrd1.Value = ws.Range("D98") sepgrd2.Value = ws.Range("D99") sepgrd3.Value = ws.Range("D100") sepgrd4.Value = ws.Range("D101") sepgrd5.Value = ws.Range("D102") sepgrd6.Value = ws.Range("D103") seprecap1.Value = ws.Range("F98") seprecap2.Value = ws.Range("F99") seprecap3.Value = ws.Range("F100") seprecap4.Value = ws.Range("F101") seprecap5.Value = ws.Range("F102") seprecap6.Value = ws.Range("F103") With page10 octemp1.Value = ws.Range("B110") octemp2.Value = ws.Range("B111") octemp3.Value = ws.Range("B112") octemp4.Value = ws.Range("B113") octemp5.Value = ws.Range("B114") octemp6.Value = ws.Range("B115") octgrd1.Value = ws.Range("D110") octgrd2.Value = ws.Range("D111") octgrd3.Value = ws.Range("D112") octgrd4.Value = ws.Range("D113") octgrd5.Value = ws.Range("D114") octgrd6.Value = ws.Range("D115") octrecap1.Value = ws.Range("F110") octrecap2.Value = ws.Range("F111") octrecap3.Value = ws.Range("F112") octrecap4.Value = ws.Range("F113") octrecap5.Value = ws.Range("F114") octrecap6.Value = ws.Range("F115") With page11 novemp1.Value = ws.Range("B122") novemp2.Value = ws.Range("B123") novemp3.Value = ws.Range("B124") novemp4.Value = ws.Range("B125") novemp5.Value = ws.Range("B126") novemp6.Value = ws.Range("B127") novgrd1.Value = ws.Range("D122") novgrd2.Value = ws.Range("D123") novgrd3.Value = ws.Range("D124") novgrd4.Value = ws.Range("D125") novgrd5.Value = ws.Range("D126") novgrd6.Value = ws.Range("D127") novrecap1.Value = ws.Range("F122") novrecap2.Value = ws.Range("F123") novrecap3.Value = ws.Range("F124") novrecap4.Value = ws.Range("F125") novrecap5.Value = ws.Range("F126") novrecap6.Value = ws.Range("F127") With page12 decemp1.Value = ws.Range("B134") decemp2.Value = ws.Range("B135") decemp3.Value = ws.Range("B136") decemp4.Value = ws.Range("B137") decemp5.Value = ws.Range("B138") decemp6.Value = ws.Range("B139") decgrd1.Value = ws.Range("D134") decgrd2.Value = ws.Range("D135") decgrd3.Value = ws.Range("D136") decgrd4.Value = ws.Range("D137") decgrd5.Value = ws.Range("D138") decgrd6.Value = ws.Range("D139") decrecap1.Value = ws.Range("F134") decrecap2.Value = ws.Range("F135") decrecap3.Value = ws.Range("F136") decrecap4.Value = ws.Range("F137") decrecap5.Value = ws.Range("F138") decrecap6.Value = ws.Range("F139") End With End With End With End With End With End With End With End With End With End With End With End With End Sub
Untested - too many controls to make... You'd need to harmonize your control naming a bit, but something like this should work: Private Sub UserForm_Initialize() Dim ws As Worksheet, mNum As Long, months, i As Long, m Dim c As Range Set ws = Sheets("compliance") Set c = ws.Range("B1") months = Array("jan", "feb", "mar", "apr", "may", "jun", _ "jul", "aug", "sep", "oct", "nov", "dec") For mNum = 1 To 12 m = months(m - 1) With Me.MultiPage1.Pages("page" & mNum) For i = 1 To 6 .Controls(m & "emp" & i).Value = c.Offset(i, 0).Value .Controls(m & "grd" & i).Value = c.Offset(i, 2).Value .Controls(m & "recap" & i).Value = c.Offset(i, 4).Value Next i End With Set c = c.Offset(12, 0) Next mNum End Sub
how to get picture from database to picture box by using textbox lost focus
Private Sub tunjukrekod() Dim cn As MySqlConnection = New MySqlConnection() cn.ConnectionString = ("server=localhost; userid=root; password=; database=payrollsystemdb;") cn.Open() cmd.Connection = cn Try If TextBoxEmployeeID.Text <> "" Then dt.Clear() cmd = New MySqlCommand("select * from employeedetail where EmployeeID='" & TextBoxEmployeeID.Text & "'", cn) da = New MySqlDataAdapter(cmd) da.Fill(dt) TextBoxEmployeeID.Text = dt.Rows(0).Item(0) TextBoxEmployeeName.Text = dt.Rows(0).Item(1) TextBoxChineseName.Text = dt.Rows(0).Item(2) If dt.Rows(0).Item(3) = "Active" Then RadioButtonActiveStatus.Checked = True ElseIf dt.Rows(0).Item(3) = "Inactive" Then RadioButtonInactiveStatus.Checked = True End If TextBoxbranchID.Text = dt.Rows(0).Item(4) TextBoxAccessLevel.Text = dt.Rows(0).Item(5) RichTextBoxAddress.Text = dt.Rows(0).Item(6) TextBoxTel1.Text = dt.Rows(0).Item(7) TextBoxTel2.Text = dt.Rows(0).Item(8) TextBoxMobile.Text = dt.Rows(0).Item(9) TextBoxEmail.Text = dt.Rows(0).Item(10) TextBoxICNew.Text = dt.Rows(0).Item(11) TextBoxICOld.Text = dt.Rows(0).Item(12) TextBoxPassport.Text = dt.Rows(0).Item(13) DateTimePickerpassportexpire.Value = dt.Rows(0).Item(14) TextBoxPermit.Text = dt.Rows(0).Item(15) DateTimePickerpermitexpire.Value = dt.Rows(0).Item(16) DateTimePickerBirthDate.Value = dt.Rows(0).Item(17) TextBoxreligionPD.Text = dt.Rows(0).Item(18) TextBoxAge.Text = dt.Rows(0).Item(19) TextBoxracePD.Text = dt.Rows(0).Item(20) TextBoxcitizenshipPD.Text = dt.Rows(0).Item(21) TextBoxsexPD.Text = dt.Rows(0).Item(22) TextBoxmaritalPD.Text = dt.Rows(0).Item(23) If dt.Rows(0).Item(24) = "yes" Then RadioButtonyesC.Checked = True ElseIf dt.Rows(0).Item(24) = "No" Then RadioButtonnoC.Checked = True End If If dt.Rows(0).Item(25) = "yes" Then RadioButtonYesEOT.Checked = True ElseIf dt.Rows(0).Item(25) = "No" Then RadioButtonnoEOT.Checked = True End If TextBoxNameSpouse.Text = dt.Rows(0).Item(26) TextBoxICNewSpouse.Text = dt.Rows(0).Item(27) TextBoxICOldSpouse.Text = dt.Rows(0).Item(28) RichTextBoxAddressSpouse.Text = dt.Rows(0).Item(29) TextBoxIncomeSpouse.Text = dt.Rows(0).Item(30) TextBoxBranchSpouse.Text = dt.Rows(0).Item(31) If dt.Rows(0).Item(32) = "yes" Then RadioButtonyesworking.Checked = True ElseIf dt.Rows(0).Item(32) = "No" Then RadioButtonNoworking.Checked = True End If TextBoxChildrenSpouse.Text = dt.Rows(0).Item(33) If dt.Rows(0).Item(34) = "Auto" Then RadioButtonAutoAuMaSpouse.Checked = True ElseIf dt.Rows(0).Item(34) = "Manual" Then RadioButtonManualAuMaSpouse.Checked = True End If TextBoxpositionED.Text = dt.Rows(0).Item(35) TextBoxdepartmentED.Text = dt.Rows(0).Item(36) TextBoxdivisionED.Text = dt.Rows(0).Item(37) TextBoxsectionED.Text = dt.Rows(0).Item(38) TextBoxlocationED.Text = dt.Rows(0).Item(39) TextBoxgradeED.Text = dt.Rows(0).Item(40) TextBoxcategoryED.Text = dt.Rows(0).Item(41) TextBoxworkgroupED.Text = dt.Rows(0).Item(42) TextBoxformularateED.Text = dt.Rows(0).Item(43) DateTimePickerjoindateED.Value = dt.Rows(0).Item(44) DateTimePickerconfirmdateED.Value = dt.Rows(0).Item(45) DateTimePickerresigneddateED.Value = dt.Rows(0).Item(46) TextBoxacbankED.Text = dt.Rows(0).Item(47) If dt.Rows(0).Item(48) = "IC New" Then RadioButtonICNewACBank.Checked = True ElseIf dt.Rows(0).Item(48) = "IC Old" Then RadioButtonICOldACBank.Checked = True ElseIf dt.Rows(0).Item(48) = "Passport No" Then RadioButtonPassportNoACBank.Checked = True ElseIf dt.Rows(0).Item(48) = "Permit No" Then RadioButtonPermitNoACBank.Checked = True End If TextBoxACNoED.Text = dt.Rows(0).Item(49) TextBoxHRDFundED.Text = dt.Rows(0).Item(50) If dt.Rows(0).Item(51) = "IC New" Then RadioButtonICNewHRDFund.Checked = True ElseIf dt.Rows(0).Item(51) = "IC Old" Then RadioButtonICOldHRDFund.Checked = True ElseIf dt.Rows(0).Item(51) = "Passport No" Then RadioButtonPassportNoHRDFund.Checked = True ElseIf dt.Rows(0).Item(51) = "Permit No" Then RadioButtonPermitNoHRDFund.Checked = True End If TextBoxProbationED.Text = dt.Rows(0).Item(52) TextBoxSalaryED.Text = dt.Rows(0).Item(53) TextBoxNoticeResignED.Text = dt.Rows(0).Item(54) TextBoxEmployerNamePE.Text = dt.Rows(0).Item(55) RichTextBoxAddressPE.Text = dt.Rows(0).Item(56) TextBoxTelNo1PE.Text = dt.Rows(0).Item(57) TextBoxTelNo2PE.Text = dt.Rows(0).Item(58) TextBoxFaxPE.Text = dt.Rows(0).Item(59) TextBoxPICPE.Text = dt.Rows(0).Item(60) TextBoxPositionPE.Text = dt.Rows(0).Item(61) DateTimePickerdatejoinPE.Value = dt.Rows(0).Item(62) TextBoxSalaryPE.Text = dt.Rows(0).Item(63) DateTimePickerdateresignPE.Value = dt.Rows(0).Item(64) TextBoxReasonPE.Text = dt.Rows(0).Item(65) TextBoxEPFNoGD.Text = dt.Rows(0).Item(66) If dt.Rows(0).Item(67) = "IC New" Then RadioButtonICNewICTypeEPFGD.Checked = True ElseIf dt.Rows(0).Item(67) = "IC Old" Then RadioButtonICOldICTypeEPFGD.Checked = True ElseIf dt.Rows(0).Item(67) = "Passport No" Then RadioButtonPassportNoICTypeEPFGD.Checked = True ElseIf dt.Rows(0).Item(67) = "Permit No" Then RadioButtonPermitNoICTypeEPFGD.Checked = True End If TextBoxInitialGD.Text = dt.Rows(0).Item(68) TextBoxNKGD.Text = dt.Rows(0).Item(69) TextBoxepftableGD.Text = dt.Rows(0).Item(70) TextBoxkwspGD.Text = dt.Rows(0).Item(71) TextBoxIncomeTaxGD.Text = dt.Rows(0).Item(72) If dt.Rows(0).Item(73) = "IC New" Then RadioButtonICNewICTypeIncomeGD.Checked = True ElseIf dt.Rows(0).Item(73) = "IC Old" Then RadioButtonICOldICTypeIncomeGD.Checked = True ElseIf dt.Rows(0).Item(73) = "Passport No" Then RadioButtonPassportNoICTypeIncomeGD.Checked = True ElseIf dt.Rows(0).Item(73) = "Permit No" Then RadioButtonPermitNoICTypeIncomeGD.Checked = True End If TextBoxbranchincomeGD.Text = dt.Rows(0).Item(74) TextBoxpcbcodeGD.Text = dt.Rows(0).Item(75) TextBoxincometaxdeptGD.Text = dt.Rows(0).Item(76) TextBoxSocsoNoGD.Text = dt.Rows(0).Item(77) If dt.Rows(0).Item(78) = "IC New" Then RadioButtonICNewICTypeSocsoGD.Checked = True ElseIf dt.Rows(0).Item(78) = "IC Old" Then RadioButtonICOldICTypeSocsoGD.Checked = True ElseIf dt.Rows(0).Item(78) = "Passport No" Then RadioButtonPassportNoICTypeSocsoGD.Checked = True ElseIf dt.Rows(0).Item(78) = "Permit No" Then RadioButtonPermitNoICTypeSocsoGD.Checked = True End If TextBoxBranchSocsoGD.Text = dt.Rows(0).Item(79) TextBoxSocsoOfficeGD.Text = dt.Rows(0).Item(80) TextBoxsocsotypeGD.Text = dt.Rows(0).Item(81) TextBoxboardofsocsoGD.Text = dt.Rows(0).Item(82) TextBoxTHAccGD.Text = dt.Rows(0).Item(83) If dt.Rows(0).Item(84) = "IC New" Then RadioButtonICNewICTypeTHGD.Checked = True ElseIf dt.Rows(0).Item(84) = "IC Old" Then RadioButtonICOldICTypeTHGD.Checked = True ElseIf dt.Rows(0).Item(84) = "Passport No" Then RadioButtonPassportNoICTypeTHGD.Checked = True ElseIf dt.Rows(0).Item(84) = "Permit No" Then RadioButtonPermitNoICTypeTHGD.Checked = True End If TextBoxthcGD.Text = dt.Rows(0).Item(85) 'Dim ms As New MemoryStream(changephoto(CInt(khaiEDForm2.DataGridViewfind.SelectedCells(0).Value))) 'PictureBox1.Image = Image.FromStream(ms) cmd = New MySqlCommand("select * from salarydetail", cn) da.SelectCommand = cmd da.Fill(dtsalary) End If Catch ex As Exception 'TextBoxEmployeeName.Focus() End Try End Sub 'Function changephoto(ByVal photo As Integer) As Byte() ' Dim cn As MySqlConnection = New MySqlConnection() ' cn.ConnectionString = ("server=localhost; userid=root; password=; database=payrollsystemdb;") ' cn.Open() ' '.CommandText = System.Data.CommandType.Text ' With cmd ' .Connection = cn ' .CommandText = "SELECT Imageblob FROM employeedetail WHERE EmployeeID=" & khaiEDForm2.DataGridViewfind.SelectedRows(0).Cells(0).Value ' End With ' Dim myphoto() As Byte = CType(cmd.ExecuteScalar(), Byte()) ' cn.Close() ' Return myphoto 'End Function Private Sub TextBoxEmployeeID_LostFocus(sender As Object, e As EventArgs) Handles TextBoxEmployeeID.LostFocus If TextBoxEmployeeID.Text = "" Then TextBoxEmployeeID.Select() Else tunjukrekod() tunjukdgv() End If addrow() End Sub How I Can Rewrite the code? because the code cam retrive picture only when from form2 to form1. I used visual studio express 2014, MySQL Database using PHPMYADMIN, MySQLConnection.
ok . i got the solution ! finally ! dr = cmd.ExecuteReader() If (dr.HasRows) Then While (dr.Read()) With Me 'fetch image from database Dim imgBytes() As Byte = dr("Imageblob") 'image field Dim image As Bitmap = New Bitmap(New System.IO.MemoryStream(imgBytes)) 'convert binary to image .PictureBox1.Image = image 'show picture to picture box End With End While Else MsgBox("No records Found!") End If
Run-time error 91: Object variable or With block variable not set
I'm using Access 2010 VBA that is returning a recordset from an IBM iSeries. I have the following loop to append the recordset to a local table: 'Loop through recordset and place values Do While rsti401.EOF = False Set rst401 = CurrentDb.OpenRecordset("tblLocal_SL401WK", dbOpenDynaset, dbSeeChanges) With rst401 .AddNew .Fields("PC") = rsti401.Fields("PC") .Fields("TIME") = rsti401.Fields("TIME") .Fields("CONO") = rsti401.Fields("CONO") .Fields("STYCOL") = rsti401.Fields("STYCOL") .Fields("WHSE") = rsti401.Fields("WHSE") .Fields("CUNO") = rsti401.Fields("CUNO") .Fields("SIZE01") = rsti401.Fields("SIZE01") .Fields("SIZE02") = rsti401.Fields("SIZE02") .Fields("SIZE03") = rsti401.Fields("SIZE03") .Fields("SIZE04") = rsti401.Fields("SIZE04") .Fields("SIZE05") = rsti401.Fields("SIZE05") .Fields("SIZE06") = rsti401.Fields("SIZE06") .Fields("SIZE07") = rsti401.Fields("SIZE07") .Fields("SIZE08") = rsti401.Fields("SIZE08") .Fields("SIZE09") = rsti401.Fields("SIZE09") .Fields("SIZE10") = rsti401.Fields("SIZE10") .Fields("SIZE11") = rsti401.Fields("SIZE11") .Fields("SIZE12") = rsti401.Fields("SIZE12") .Fields("SIZE13") = rsti401.Fields("SIZE13") .Fields("SIZE14") = rsti401.Fields("SIZE14") .Fields("SIZE15") = rsti401.Fields("SIZE15") .Fields("BQTY01") = rsti401.Fields("BQTY01") .Fields("BQTY02") = rsti401.Fields("BQTY02") .Fields("BQTY03") = rsti401.Fields("BQTY03") .Fields("BQTY04") = rsti401.Fields("BQTY04") .Fields("BQTY05") = rsti401.Fields("BQTY05") .Fields("BQTY06") = rsti401.Fields("BQTY06") .Fields("BQTY07") = rsti401.Fields("BQTY07") .Fields("BQTY08") = rsti401.Fields("BQTY08") .Fields("BQTY09") = rsti401.Fields("BQTY09") .Fields("BQTY10") = rsti401.Fields("BQTY10") .Fields("BQTY11") = rsti401.Fields("BQTY11") .Fields("BQTY12") = rsti401.Fields("BQTY12") .Fields("BQTY13") = rsti401.Fields("BQTY13") .Fields("BQTY14") = rsti401.Fields("BQTY14") .Fields("BQTY15") = rsti401.Fields("BQTY15") .Update End With rsti401.MoveNext Loop 'close connections rsti401.Close rst401.Close IBM.Close Set IBM = Nothing Set rst401 = Nothing Set rsti401 = Nothing Set CMD = Nothing However, each time I run it I is stopping at the following line: rst401.Close With error 'Run-time error 91'. I can't work it out. I've set rst401 at the beginning, so why am I still getting the error. Any pointers would be a great help. Thanks, Michael
The problem you have is because the rst401 is set inside the Do While Loop, and you are trying to close an object that has lost its scope outside the Loop. Suggest you make the following changes. 'Loop through recordset and place values Set rst401 = CurrentDb.OpenRecordset("tblLocal_SL401WK", dbOpenDynaset, dbSeeChanges) Do While rsti401.EOF = False With rst401 .AddNew .Fields("PC") = rsti401.Fields("PC") .Fields("TIME") = rsti401.Fields("TIME") .Fields("CONO") = rsti401.Fields("CONO") .Fields("STYCOL") = rsti401.Fields("STYCOL") .Fields("WHSE") = rsti401.Fields("WHSE") .Fields("CUNO") = rsti401.Fields("CUNO") .Fields("SIZE01") = rsti401.Fields("SIZE01") .Fields("SIZE02") = rsti401.Fields("SIZE02") .Fields("SIZE03") = rsti401.Fields("SIZE03") .Fields("SIZE04") = rsti401.Fields("SIZE04") .Fields("SIZE05") = rsti401.Fields("SIZE05") .Fields("SIZE06") = rsti401.Fields("SIZE06") .Fields("SIZE07") = rsti401.Fields("SIZE07") .Fields("SIZE08") = rsti401.Fields("SIZE08") .Fields("SIZE09") = rsti401.Fields("SIZE09") .Fields("SIZE10") = rsti401.Fields("SIZE10") .Fields("SIZE11") = rsti401.Fields("SIZE11") .Fields("SIZE12") = rsti401.Fields("SIZE12") .Fields("SIZE13") = rsti401.Fields("SIZE13") .Fields("SIZE14") = rsti401.Fields("SIZE14") .Fields("SIZE15") = rsti401.Fields("SIZE15") .Fields("BQTY01") = rsti401.Fields("BQTY01") .Fields("BQTY02") = rsti401.Fields("BQTY02") .Fields("BQTY03") = rsti401.Fields("BQTY03") .Fields("BQTY04") = rsti401.Fields("BQTY04") .Fields("BQTY05") = rsti401.Fields("BQTY05") .Fields("BQTY06") = rsti401.Fields("BQTY06") .Fields("BQTY07") = rsti401.Fields("BQTY07") .Fields("BQTY08") = rsti401.Fields("BQTY08") .Fields("BQTY09") = rsti401.Fields("BQTY09") .Fields("BQTY10") = rsti401.Fields("BQTY10") .Fields("BQTY11") = rsti401.Fields("BQTY11") .Fields("BQTY12") = rsti401.Fields("BQTY12") .Fields("BQTY13") = rsti401.Fields("BQTY13") .Fields("BQTY14") = rsti401.Fields("BQTY14") .Fields("BQTY15") = rsti401.Fields("BQTY15") .Update End With rsti401.MoveNext Loop 'close connections rsti401.Close rst401.Close IBM.Close Set IBM = Nothing Set rst401 = Nothing Set rsti401 = Nothing Set CMD = Nothing
VB IF statement to ask if NULL then apply default image instead
Inherited a VB website and am new to vb programming, so steep learning curve. I have a site that searches and list all currently available cars in the UK for a leasing company. the vehicle data is provided by an external comapany and links all the tech specs etc and images to a keyID. However... If the vehicle has not been assigned an image it is not counted or displayed. I want to add an IF statement so that if the ImageId is Null then it will display a default 'awaiting image' jpg and would therefore still be listed to the public. the page is http://www.carmyke.co.uk/search_prices.aspx with the 'Vans' dropping the most from the list. I have included the code I think I need to update. I think I need an IF statement for the .ImageId that if the SQL returns NULL then it uses a default image located in the same folder as defined by the appsettings Hope this makes sense!? <--- THE CODE ---> #Region "Methods" Private Function GetVehicle(ByVal SearchBy As SearchBy, _ ByVal SearchText As String) As Data.LeasingPrices.Vehicle Dim _Vehicle As New Data.LeasingPrices.Vehicle Try Dim _SQL As New Net.SQL _SQL.AppendSQL("SELECT TOP 1 * ") _SQL.AppendSQL("FROM vw_carmyke_Rates_Business ") _SQL.AppendSQL("LEFT OUTER JOIN carmyke_SpecialOffers ON vw_carmyke_Rates_Business.CVehicleId = carmyke_SpecialOffers.CVehicleId ") Select Case SearchBy Case Hydrate.SearchBy.Make _SQL.AppendSQL("WHERE Make = #SearchText ") Case Hydrate.SearchBy.Model _SQL.AppendSQL("WHERE MakeModel = #SearchText ") Case Hydrate.SearchBy.Derivative _SQL.AppendSQL("WHERE MakeModelDerivative = #SearchText ") End Select _SQL.AppendSQL("ORDER BY Rental_48_40;") _SQL.AddParameter("#SearchText", SearchText, SqlDbType.VarChar) _SQL.ConnectReader() If _SQL.Validation.NoErrors Then If _SQL.Reader.Read() Then With _Vehicle .CVehicleId = _SQL.Reader.SQLString("CVehicleId").ToInteger() .Van = _SQL.Reader.SQLString("BodyStyle").Contains("Van") .Make = _SQL.Reader.SQLString("Make") .Model = _SQL.Reader.SQLString("Model") .Derivative = _SQL.Reader.SQLString("Derivative") .ImageId = _SQL.Reader.SQLString("ImageId") & ".jpg" .Co2 = _SQL.Reader.SQLString("Co2").ToInteger() .P11d = _SQL.Reader.SQLString("P11d").ToDouble() .Business = _SQL.Reader.SQLString("Business").ToBoolean() .Personal = _SQL.Reader.SQLString("Personal").ToBoolean() .Details = _SQL.Reader.SQLString("Details") .OfferPrice = _SQL.Reader.SQLString("OfferPrice").ToDouble() If .OfferPrice = 0 Then _ .OfferPrice = _SQL.Reader.SQLString("Offer_48_40").ToDouble() If .OfferPrice = 0 Then _ .OfferPrice = _SQL.Reader.SQLString("Rental_48_40").ToDouble() .Commercial = _SQL.Reader.SQLString("Commercial").ToBoolean() .Offer_24_20 = _SQL.Reader.SQLString("Offer_24_20").ToDouble() .Offer_24_40 = _SQL.Reader.SQLString("Offer_24_40").ToDouble() .Offer_24_60 = _SQL.Reader.SQLString("Offer_24_60").ToDouble() .Offer_36_30 = _SQL.Reader.SQLString("Offer_36_30").ToDouble() .Offer_36_60 = _SQL.Reader.SQLString("Offer_36_60").ToDouble() .Offer_36_90 = _SQL.Reader.SQLString("Offer_36_90").ToDouble() .Offer_48_40 = _SQL.Reader.SQLString("Offer_48_40").ToDouble() .Offer_48_80 = _SQL.Reader.SQLString("Offer_48_80").ToDouble() .Offer_48_120 = _SQL.Reader.SQLString("Offer_48_120").ToDouble() If .Offer_24_20 = -1 Then .Rental_24_20 = 0 ElseIf .Offer_24_20 > 0 Then .Rental_24_20 = .Offer_24_20 Else .Rental_24_20 = _SQL.Reader.SQLString("Rental_24_20").ToDouble() End If If .Offer_24_40 = -1 Then .Rental_24_40 = 0 ElseIf .Offer_24_40 > 0 Then .Rental_24_40 = .Offer_24_40 Else .Rental_24_40 = _SQL.Reader.SQLString("Rental_24_40").ToDouble() End If If .Offer_24_60 = -1 Then .Rental_24_60 = 0 ElseIf .Offer_24_60 > 0 Then .Rental_24_60 = .Offer_24_60 Else .Rental_24_60 = _SQL.Reader.SQLString("Rental_24_60").ToDouble() End If If .Offer_36_30 = -1 Then .Rental_36_30 = 0 ElseIf .Offer_36_30 > 0 Then .Rental_36_30 = .Offer_36_30 Else .Rental_36_30 = _SQL.Reader.SQLString("Rental_36_30").ToDouble() End If If .Offer_36_60 = -1 Then .Rental_36_60 = 0 ElseIf .Offer_36_60 > 0 Then .Rental_36_60 = .Offer_36_60 Else .Rental_36_60 = _SQL.Reader.SQLString("Rental_36_60").ToDouble() End If If .Offer_36_90 = -1 Then .Rental_36_90 = 0 ElseIf .Offer_36_90 > 0 Then .Rental_36_90 = .Offer_36_90 Else .Rental_36_90 = _SQL.Reader.SQLString("Rental_36_90").ToDouble() End If If .Offer_48_40 = -1 Then .Rental_48_40 = 0 ElseIf .Offer_48_40 > 0 Then .Rental_48_40 = .Offer_48_40 Else .Rental_48_40 = _SQL.Reader.SQLString("Rental_48_40").ToDouble() End If If .Offer_48_80 = -1 Then .Rental_48_80 = 0 ElseIf .Offer_48_80 > 0 Then .Rental_48_80 = .Offer_48_80 Else .Rental_48_80 = _SQL.Reader.SQLString("Rental_48_80").ToDouble() End If If .Offer_48_120 = -1 Then .Rental_48_120 = 0 ElseIf .Offer_48_120 > 0 Then .Rental_48_120 = .Offer_48_120 Else .Rental_48_120 = _SQL.Reader.SQLString("Rental_48_120").ToDouble() End If End With Else _Vehicle = Nothing End If Else _Vehicle = Nothing End If _SQL.DisconnectReader() Catch _Vehicle = Nothing End Try Return _Vehicle End Function Public Function Vehicle(ByVal SearchText As String) As Data.LeasingPrices.Vehicle Dim _Vehicle As New Data.LeasingPrices.Vehicle _Vehicle = GetVehicle(Hydrate.SearchBy.Derivative, SearchText) If _Vehicle Is Nothing Then _Vehicle = GetVehicle(Hydrate.SearchBy.Model, SearchText) End If If _Vehicle Is Nothing Then _Vehicle = GetVehicle(Hydrate.SearchBy.Make, SearchText) End If Return _Vehicle End Function Private Function GetSearchOption(ByVal SearchOption As String) As String Dim _GetSearchOption As String = "" Try If Not HttpContext.Current.Session(SearchOption) Is Nothing Then _ _GetSearchOption = HttpContext.Current.Session(SearchOption) Catch _GetSearchOption = "" End Try Return _GetSearchOption End Function Public Function SearchOptions() As Data.LeasingPrices.SearchOptions Dim _SearchOptions As New Data.LeasingPrices.SearchOptions Try With _SearchOptions .FourByFour = GetSearchOption("FourByFour").ToBoolean() .CityCar = GetSearchOption("CityCar").ToBoolean() .Coupe = GetSearchOption("Coupe").ToBoolean() .Estate = GetSearchOption("Estate").ToBoolean() .Hatchback = GetSearchOption("Hatchback").ToBoolean() .MPV = GetSearchOption("MPV").ToBoolean() .Saloon = GetSearchOption("Saloon").ToBoolean() .Sports = GetSearchOption("Sports").ToBoolean() .Van = GetSearchOption("Van").ToBoolean() .RentalFrom = GetSearchOption("RentalFrom").ToInteger() .RentalTo = GetSearchOption("RentalTo").ToInteger() If .RentalFrom = 0 And .RentalTo = 0 Then .RentalFrom = Data.LeasingPrices.SearchOptions.DefaultRentalFrom .RentalTo = Data.LeasingPrices.SearchOptions.DefaultRentalTo End If End With Catch _SearchOptions = Nothing End Try Return _SearchOptions End Function #End Region #Region "Constructors" Public Sub New() End Sub #End Region End Class End Namespace
I'm not familiar with the Net.SQL entity you're using, but it is usual to use something like the Convert.IsDBNull Method to check for a NULL database value. An alternative is to use COALESCE in the query, like SELECT TOP 1 [CVehicleId], ..more columns.., COALESCE([ImageId], 'AwaitingImage'), ..remaining columns.. You should really explicitly specify the columns, and put the column names in square brackets so that if you accidentally have a column name which happens to be an SQL keyword then it doesn't mistake it for a keyword.