Using Class Module in VBA give error - vba

I get an "object variable or With block variable not set " error in the following
code. Th line that gets the error is
all = GetPayAllocation(rsPrj, 10, 1)
If I check the properties of the all variable they have values.
Any ideas?
Public Function tmptest1()
Dim rsPrj As Recordset
If Not Connection Then Exit Function
gSQL = "SELECT * FROM Projects WHERE ProjectID=7893"
If Not GetODBCRecordset(gSQL, rsPrj) Then Exit Function
Dim all As PayAllocation
all = GetPayAllocation(rsPrj, 10, 1)
Debug.Print all.ManagementFee
CloseALL
End Function
Public Function GetPayAllocation(rsPrj As Recordset, invHours As Double, invweeksofpay As Integer) As PayAllocation
On Error GoTo ErrHandler
Dim all As PayAllocation
Set all = New PayAllocation
If Not all.Calculate(rsPrj, invHours, invweeksofpay) Then GoTo ErrExit
Set GetPayAllocation = all
ErrExit:
Exit Function
ErrHandler:
GeneralErrorHandler ("GetPayAllocation")
Resume ErrExit
End Function
This the PayAllocation class module
Public PayRate As Double
Public Margin As Double
Public ManagementFee As Double
Public PayrollTax As Double
Public AgencyCommission As Double
Public Total As Double
Public Function Calculate(rsPrj As Recordset, invHours As Double, invweeksofpay As Integer) As Boolean
On Error GoTo ErrHandler
Dim multiplier As Double
multiplier = GetMultiplierValue(rsPrj, invHours, invweeksofpay)
PayRate = GetValue(multiplier, rsPrj!PayRateInclSuper)
Total = PayRate
If rsPrj!MarginRateInclInPayRate = False Then
If rsPrj!MarginRatePercent Then
Margin = GetValue(rsPrj!MarginRate, PayRate)
Else
Margin = GetValue(multiplier, rsPrj!MarginRate)
End If
Total = Total + Margin
End If
If rsPrj!LMFInclInPayRate = False Then
If rsPrj!LMFPercent Then
ManagementFee = GetValue(rsPrj!LMF, PayRate)
Else
ManagementFee = GetValue(multiplier, rsPrj!LMF)
End If
Total = Total + ManagementFee
End If
If rsPrj!PayrollTaxInclInPayRate = False Then
If rsPrj!PayrollTaxPercent Then
PayrollTax = GetValue(rsPrj!PayrolltaxAmount, PayRate)
Else
PayrollTax = GetValue(multiplier, rsPrj!PayrolltaxAmount)
End If
Total = Total + PayrollTax
End If
If rsPrj!AgencyCommInclInPayRate = False Then
If rsPrj!AgencyCommPercent Then
AgencyCommission = GetValue(rsPrj!AgencyComm, PayRate)
Else
AgencyCommission = GetValue(multiplier, rsPrj!AgencyComm)
End If
Total = Total + AgencyCommission
End If
If rsPrj!MarginRateOnTop Then
If rsPrj!MarginRatePercent Then
Margin = GetValue(rsPrj!MarginRate, Total)
Else
Margin = GetValue(multiplier, rsPrj!MarginRate)
End If
Total = Total + Margin
End If
If rsPrj!LMFOnTop Then
If rsPrj!LMFPercent Then
ManagementFee = GetValue(rsPrj!LMF, Total)
Else
ManagementFee = GetValue(multiplier, rsPrj!LMF)
End If
Total = Total + ManagementFee
End If
If rsPrj!PayrollTaxOnTop Then
If rsPrj!PayrollTaxPercent Then
PayrollTax = GetValue(rsPrj!PayrolltaxAmount, Total)
Else
PayrollTax = GetValue(multiplier, rsPrj!PayrolltaxAmount)
End If
Total = Total + PayrollTax
End If
If rsPrj!AgencyCommOnTop Then
If rsPrj!AgencyCommPercent Then
AgencyCommission = GetValue(rsPrj!AgencyComm, Total)
Else
AgencyCommission = GetValue(multiplier, rsPrj!AgencyComm)
End If
Total = Total + AgencyCommission
End If
Calculate = True
ErrExit:
Exit Function
ErrHandler:
Calculate = False
Resume ErrExit
End Function
Private Function GetMultiplierValue(rsPrj As Recordset, invHours As Double, invweeksofpay As Integer) As Double
Dim value As Double
Select Case rsPrj!HourlyDailyMthly
Case "Hourly"
value = invHours
Case "Daily"
value = invHours
Case "Weekly"
value = CDbl(invweeksofpay)
Case "Monthly"
End Select
GetMultiplierValue = value
End Function
Private Function GetValue(multiplier As Double, amount As Double)
GetValue = format(multiplier * amount, "0.00")
End Function

It should have been.
Set all = GetPayAllocation(rsPrj, 10, 1)

Related

Values allocation in 4 textboxes

I have 4 textboxes and one label on my form, 4 textboxes represent 4 payment types (cash, card, bank transfer and other payment method).
In my label i have summary value which i increment when i'm adding new item to receipt.
Textbox names are: cashTbox cardTbox bankTbox otherTbox
Label name is: summaryLbl
By default cashTextbox.text is equal to summ.content and every time I add an item to database i increment summaryLbl + itemPrice (item price from database).
I have tried making a function which will allow me to subtract from cashTboxto other payment types without allowing cashTbox to go to negative value.
My code is as follows:
Private Sub cashTbox_LostKeyboardFocus(sender As Object, e As KeyboardFocusChangedEventArgs) Handles cashTbox.LostKeyboardFocus
If sender.IsFocused = True And sender.text.length > 0 Then
If calculateP(sender) = True Then
End If
End If
End Sub
Dim sumP as decimal = summaryLbl.content
Dim cashP as decimal = cashTbox.text
Dim cardP as decimal = cardTbox.text
Dim bankP as decimal = bankTbox.text
Dim otherP as decimal = otherTbox.text
Public Function calculateP(ByVal s As Object)
Try
If String.IsNullOrEmpty(s.text) Then
s.text = "0,00"
Else
s.text = Decimal.Parse(s.text)
End If
If s Is cashTbox Then
ElseIf s Is cardTbox Then
cardP = s.text
cashTbox.Text = sumP - (cardP + bankP + otherP )
ElseIf s Is bankTbox Then
bankP = s.text
cashTbox.Text = sumP - (cardP + bankP + otherP )
ElseIf s Is otherTbox Then
otherP = s.text
cashTbox.Text = sumP - (cardP + bankP + otherP )
End If
cashTbox.Text = sumP - (cardP + bankP + otherP )
Catch ex As Exception
End Try
setValues()
Return True
End Function
Public Function setValues()
summaryLbl.content = sumP
cashTbox.text = cashP
cardTbox.text = cardP
bankTbox.text = bankP
otherTbox.text = otherP
End Function

File upload issue from one server to another server using classic ASP upload utility

I have a classic ASP application which is currently deployed on server A. I have "upload attachment" functionality through which I am trying to upload files on Server A(where my ASP application is deployed in IIS) itself using below classic ASP code. When I am trying to upload files on Server A, my files are getting uploaded successfully and quickly. But, when I am trying to upload the file to another Server B then I am not able to upload files using same utility ASP code. My question is my application deployed on Server A and I need to upload a file through application to Server B. Is it possible? Please help.
My current upload file utility code:
<% Class Loader
Private dict
Private Sub Class_Initialize
Set dict = Server.CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate
If IsObject(intDict) Then
intDict.RemoveAll
Set intDict = Nothing
End If
If IsObject(dict) Then
dict.RemoveAll
Set dict = Nothing
End If
End Sub
Public Property Get Count
Count = dict.Count
End Property
Public Sub Initialize
If Request.TotalBytes > 0 Then
response.write(Request.TotalBytes)
Dim binData
binData = Request.BinaryRead(Request.TotalBytes)
getData binData
End If
End Sub
Public Function getFileData(name)
If dict.Exists(name) Then
getFileData = dict(name).Item("Value")
Else
getFileData = ""
End If
End Function
Public Function getValue(name)
Dim gv
If dict.Exists(name) Then
gv = CStr(dict(name).Item("Value"))
gv = Left(gv,Len(gv)-2)
getValue = gv
Else
getValue = ""
End If
End Function
Public Function saveToFile(name, path)
If dict.Exists(name) Then
Dim temp
temp = dict(name).Item("Value")
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Dim file
'File write Exception handling - David
On Error Resume Next
Set file = fso.CreateTextFile(path)
For tPoint = 1 to LenB(temp)
file.Write Chr(AscB(MidB(temp,tPoint,1)))
Next
file.Close
If Err.Number<>0 then
saveToFile = False
else
saveToFile = True
End if
On Error Goto 0
Else
saveToFile = False
End If
End Function
Public Function getFileName(name)
If dict.Exists(name) Then
Dim temp, tempPos
temp = dict(name).Item("FileName")
tempPos = 1 + InStrRev(temp, "\")
getFileName = Mid(temp, tempPos)
Else
getFileName = ""
End If
End Function
Public Function getFilePath(name)
If dict.Exists(name) Then
Dim temp, tempPos
temp = dict(name).Item("FileName")
tempPos = InStrRev(temp, "\")
getFilePath = Mid(temp, 1, tempPos)
Else
getFilePath = ""
End If
End Function
Public Function getFilePathComplete(name)
If dict.Exists(name) Then
getFilePathComplete = dict(name).Item("FileName")
Else
getFilePathComplete = ""
End If
End Function
Public Function getFileSize(name)
If dict.Exists(name) Then
getFileSize = LenB(dict(name).Item("Value"))
Else
getFileSize = 0
End If
End Function
Public Function getFileSizeTranslated(name)
If dict.Exists(name) Then
temp = LenB(dict(name).Item("Value"))
If temp <= 1024 Then
getFileSizeTranslated = temp & " bytes"
Else
temp = FormatNumber((temp / 1024), 2)
getFileSizeTranslated = temp & " kilobytes"
End If
Else
getFileSizeTranslated = ""
End If
End Function
Public Function getContentType(name)
If dict.Exists(name) Then
getContentType = dict(name).Item("ContentType")
Else
getContentType = ""
End If
End Function
Private Sub getData(rawData)
Dim separator
separator = MidB(rawData, 1, InstrB(1, rawData, ChrB(13)) - 1)
Dim lenSeparator
lenSeparator = LenB(separator)
Dim currentPos
currentPos = 1
Dim inStrByte
inStrByte = 1
Dim value, mValue
Dim tempValue
tempValue = ""
While inStrByte > 0
inStrByte = InStrB(currentPos, rawData, separator)
mValue = inStrByte - currentPos
If mValue > 1 Then
value = MidB(rawData, currentPos, mValue)
Dim begPos, endPos, midValue, nValue
Dim intDict
Set intDict = Server.CreateObject("Scripting.Dictionary")
begPos = 1 + InStrB(1, value, ChrB(34))
endPos = InStrB(begPos + 1, value, ChrB(34))
nValue = endPos
Dim nameN
nameN = MidB(value, begPos, endPos - begPos)
Dim nameValue, isValid
isValid = True
If InStrB(1, value, stringToByte("Content-Type")) > 1 Then
begPos = 1 + InStrB(endPos + 1, value, ChrB(34))
endPos = InStrB(begPos + 1, value, ChrB(34))
If endPos = 0 Then
endPos = begPos + 1
isValid = False
End If
midValue = MidB(value, begPos, endPos - begPos)
intDict.Add "FileName", trim(byteToString(midValue))
begPos = 14 + InStrB(endPos + 1, value, stringToByte("Content-Type:"))
endPos = InStrB(begPos, value, ChrB(13))
midValue = MidB(value, begPos, endPos - begPos)
intDict.Add "ContentType", trim(byteToString(midValue))
begPos = endPos + 4
endPos = LenB(value)
nameValue = MidB(value, begPos, ((endPos - begPos) - 1))
Else
nameValue = trim(byteToString(MidB(value, nValue + 5)))
End If
If isValid = True Then
intDict.Add "Value", nameValue
intDict.Add "Name", nameN
dict.Add byteToString(nameN), intDict
End If
End If
currentPos = lenSeparator + inStrByte
Wend
End Sub
End Class
Private Function stringToByte(toConv)
Dim tempChar
For i = 1 to Len(toConv)
tempChar = Mid(toConv, i, 1)
stringToByte = stringToByte & chrB(AscB(tempChar))
Next
End Function
Private Function byteToString(toConv)
For i = 1 to LenB(toConv)
byteToString = byteToString & Chr(AscB(MidB(toConv,i,1)))
Next
End Function
%>
Probably the default upload size from windows in the IIS : 196Kb ...

Change subitem backcolor of a listview next item

I have a listview filled with SRT subtitle. I'am trying to change listview color based on subtitle errors. Everything is working fine, but color won't change when subtitles is overlapping. I take end-time of one subtitle and start-time of next subtitle. Based on difference, it decide is there overlapping or not. Calculations are OK but backcolor and forecolor won't change. It does change backcolor for current item but I need to change backcolor for next listview item.
'EXAMPLE #######################################
For i as integer = 0 to listview1.items.count -1
ListView1.Items(i).UseItemStyleForSubItems = False
'this is working #######
ListView1.Items.Item(i).SubItems(1).BackColor = ColorTranslator.FromHtml("#F0A6A7")
'but this is NOT working ( THIS IS WHAT I NEED) ####################
ListView1.Items.Item(i).SubItems(i + 1).BackColor = ColorTranslator.FromHtml("#F0A6A7")
Next i
'########################################################
Public Function Color_Errors(ByVal SubtitleListView As ListView)
For i = 0 To SubtitleListView.Items.Count - 2
SubtitleListView.Items(i).UseItemStyleForSubItems = False
SubtitleListView.Items(i + 1).UseItemStyleForSubItems = False
SubtitleListView.Items.Item(i).SubItems(1).ResetStyle()
SubtitleListView.Items.Item(i).SubItems(2).ResetStyle()
SubtitleListView.Items.Item(i).SubItems(3).ResetStyle()
SubtitleListView.Items.Item(i).SubItems(4).ResetStyle()
SubtitleListView.Items.Item(i).SubItems(5).ResetStyle()
Dim Text As String = SubtitleListView.Items.Item(i + 1).SubItems(5).Text
Dim TextLength As Integer = Get_Longest_Line_Length(Text)
Dim NumberOfLines As Integer = Split(Text, "|").Length
Dim Duration As Double = Convert_Time_to_Milliseconds(SubtitleListView.Items.Item(i).SubItems(4).Text)
Dim Pause As Double = Convert_Time_to_Milliseconds(SubtitleListView.Items.Item(i).SubItems(1).Text)
Dim _Start As Double = Convert_Time_to_Milliseconds(SubtitleListView.Items.Item(i).SubItems(2).Text)
Dim _End As Double = Convert_Time_to_Milliseconds(SubtitleListView.Items.Item(i).SubItems(3).Text)
Dim _NextStart As Double
If i < (SubtitleListView.Items.Count - 1) Then
_NextStart = Convert_TimeSpan_to_Milliseconds(SubtitleListView.Items.Item(i + 1).SubItems(2).Text)
End If
'TOO LONG LINES
If TextLength > MaxLength Then
SubtitleListView.Items.Item(i).SubItems(5).BackColor = ColorTranslator.FromHtml("#F0A6A7")
SubtitleListView.Items.Item(i).SubItems(5).ForeColor = Color.Black
End If
'TOO LONG DURATION
If Duration > 6000 Then
SubtitleListView.Items.Item(i).SubItems(4).BackColor = ColorTranslator.FromHtml("#F5CBD9")
SubtitleListView.Items.Item(i).SubItems(4).ForeColor = ColorTranslator.FromHtml("#6F0021")
SubtitleListView.Items.Item(i).SubItems(3).BackColor = ColorTranslator.FromHtml("#F5CBD9")
SubtitleListView.Items.Item(i).SubItems(3).ForeColor = ColorTranslator.FromHtml("#6F0021")
'SHORTER THAN 2 SECONDS
ElseIf Duration < 2000 AndAlso Duration >= 700 Then
SubtitleListView.Items.Item(i).SubItems(4).BackColor = Color.Red
SubtitleListView.Items.Item(i).SubItems(4).ForeColor = Color.White
'TOO SHORT DURATION
ElseIf Duration < 700 Then
SubtitleListView.Items.Item(i).SubItems(3).BackColor = ColorTranslator.FromHtml("#FFF0E1")
SubtitleListView.Items.Item(i).SubItems(3).ForeColor = ColorTranslator.FromHtml("#A45200")
SubtitleListView.Items.Item(i).SubItems(4).BackColor = ColorTranslator.FromHtml("#FFF0E1")
SubtitleListView.Items.Item(i).SubItems(4).ForeColor = ColorTranslator.FromHtml("#A45200")
End If
''TOO SHORT PAUSE
If Pause < 200 Then
SubtitleListView.Items.Item(i).SubItems(1).BackColor = ColorTranslator.FromHtml("#ACC9E6")
SubtitleListView.Items.Item(i).SubItems(1).ForeColor = Color.Black
SubtitleListView.Items.Item(i).SubItems(2).BackColor = ColorTranslator.FromHtml("#ACC9E6")
SubtitleListView.Items.Item(i).SubItems(2).ForeColor = Color.Black
End If
If NumberOfLines > 2 Then
SubtitleListView.Items.Item(i).SubItems(5).ForeColor = ColorTranslator.FromHtml("#0000FF")
End If
'ERROR PART #################################################
If i < (SubtitleListView.Items.Count - 1) Then
If _End > _NextStart Then
SubtitleListView.Items.Item(i + 1).SubItems(1).BackColor = ColorTranslator.FromHtml("#BB0025")
SubtitleListView.Items.Item(i + 1).SubItems(1).ForeColor = Color.White
SubtitleListView.Items.Item(i).SubItems(2).BackColor = ColorTranslator.FromHtml("#BB0025")
SubtitleListView.Items.Item(i).SubItems(2).ForeColor = Color.White
End If
End If
'############################################################
Next i
Return Nothing
End Function
Public Function Convert_Time_to_Milliseconds(ByVal Time As String)
If Time.Contains(":") = True Then
Dim VremePrviDeo() As String = Split(Time, ":")
If VremePrviDeo.Length = 3 Then
Dim Sati As Integer = VremePrviDeo(0) * 60 * 60 * 1000
Dim Minuti As Integer = VremePrviDeo(1) * 60 * 1000
Dim VremeDrugiDeo() As String = Split(VremePrviDeo(2), ",")
Dim Sekunde As Integer = VremeDrugiDeo(0) * 1000
Dim Milisekunde As Integer = VremeDrugiDeo(1)
Dim Miliseconds As Double = Sati + Minuti + Sekunde + Milisekunde
Return Miliseconds.ToString
ElseIf VremePrviDeo.Length = 2 Then
Dim Minuti As Integer = VremePrviDeo(0) * 60 * 1000
Dim VremeDrugiDeo() As String = Split(VremePrviDeo(2), ",")
Dim Sekunde As Integer = VremeDrugiDeo(0) * 1000
Dim Milisekunde As Integer = VremeDrugiDeo(1)
Dim Miliseconds As Double = Minuti + Sekunde + Milisekunde
Return Miliseconds.ToString
End If
Else
Dim VremePrviDeo() As String = Split(Time, ",")
Dim Sekunde As Integer = VremePrviDeo(0) * 1000
Dim Milisekunde As Integer = VremePrviDeo(1)
Dim Miliseconds As Double = Sekunde + Milisekunde
Return Miliseconds.ToString
End If
Return Nothing
End Function
Public Function Get_Longest_Line_Length(ByVal Text As String)
Dim Duzina As Integer = 0
For Each line As String In Split(Text, "|")
If line.Length > Duzina Then
Duzina = line.Length
End If
Next
Return Duzina
End Function
Public Function Convert_TimeSpan_to_Milliseconds(ByVal Time As String)
'Try
Dim Parsed() As String = Parse_String_to_TimeSpan(Time)
Dim Sat As Double = TimeSpan.FromHours(Parsed(0)).TotalMilliseconds
Dim Minut As Double = TimeSpan.FromMinutes(Parsed(1)).TotalMilliseconds
Dim Sekunda As Double = TimeSpan.FromSeconds(Parsed(2)).TotalMilliseconds
Dim Milisekunda As Double = TimeSpan.FromMilliseconds(Parsed(3)).TotalMilliseconds
Dim TotalTime As Double = Sat + Minut + Sekunda + Milisekunda
Return TotalTime
'Catch ex As Exception
'End Try
'Return Nothing
End Function

Strange issue with Datagridview-Rows not displayed

I have a Datagridview connected to a dataset.The problem is that occasionally,when the data is refreshed,it is not displayed in the DGV.The code is:
Private Sub DisplayInDGV()
Dim SQLSet As String
Dim DASet As New OleDb.OleDbDataAdapter
Dim DSSet As New DataSet
SQLSet = "Select * From SetDisplayTable"
DASet = New OleDb.OleDbDataAdapter(SQLSet, Con)
DSSet.Clear()
DASet.Fill(DSSet, "DSSetHere")
With DGVSetView
.Refresh()
.AutoGenerateColumns = False
.DataSource = Nothing
.DataSource = DSSet.Tables(0)
.Update()
DGVSetView.Columns(i).DataPropertyName = DSSet.Tables(0).Columns(i).ToString
.Columns(0).DataPropertyName = DSSet.Tables(0).Columns(0).ToString
.Columns(2).DataPropertyName = DSSet.Tables(0).Columns(1).ToString
.Columns(3).DataPropertyName = DSSet.Tables(0).Columns(2).ToString
.Columns(4).DataPropertyName = DSSet.Tables(0).Columns(3).ToString
.Columns(5).DataPropertyName = DSSet.Tables(0).Columns(4).ToString
.Columns(6).DataPropertyName = DSSet.Tables(0).Columns(5).ToString
.Columns(7).DataPropertyName = DSSet.Tables(0).Columns(6).ToString
.Columns(8).DataPropertyName = DSSet.Tables(0).Columns(7).ToString
.Columns(9).DataPropertyName = DSSet.Tables(0).Columns(8).ToString
.Columns(10).DataPropertyName = DSSet.Tables(0).Columns(9).ToString
.Columns(11).DataPropertyName = DSSet.Tables(0).Columns(10).ToString 'Item Unique Code for Hot Edit
.Columns(14).DataPropertyName = DSSet.Tables(0).Columns(12).ToString
End With
'Updating Totals/::
For ItemRow As Integer = 0 To DGVSetView.Rows.Count - 1
If DGVSetView.Rows(ItemRow).Cells(14).Value = True Then
DGVSetView.Rows(ItemRow).Cells(12).Value = DGVSetView.Rows(ItemRow).Cells(10).Value
ElseIf DGVSetView.Rows(ItemRow).Cells(14).Value = False Then
DGVSetView.Rows(ItemRow).Cells(13).Value = DGVSetView.Rows(ItemRow).Cells(10).Value
End If
Next
'Updating School and general totals in DGV//:
Dim SchoolTotal, GeneralTotal As Decimal
For ColumnTotal As Integer = 0 To DGVSetView.Rows.Count - 1
SchoolTotal += DGVSetView.Rows(ColumnTotal).Cells(12).Value
GeneralTotal += DGVSetView.Rows(ColumnTotal).Cells(13).Value
Next
txtSchoolAmtFinal.Text = SchoolTotal
txtGeneralAmtFinal.Text = GeneralTotal
DGVSetView.Update()
'Get gross total of the DGV amount column//:
If DGVSetView.RowCount <> 0 Then
Dim GrossAmt As Decimal = 0
For Index As Integer = 0 To DGVSetView.RowCount - 1
' GrossAmt += Convert.ToDecimal(DataGridView1.Rows(Index).Cells(11).Value)
If Str(DGVSetView.Rows(Index).Cells(10).Value) = "Null" Or (DGVSetView.Rows(Index).Cells(10).Value) <= 0 Then
MsgBox("Item Number " & (DGVSetView.Rows(Index).Cells(10).Value) & " is either blank or 0", MsgBoxStyle.Exclamation, "Item Error")
Else
GrossAmt += Convert.ToDecimal(DGVSetView.Rows(Index).Cells(10).Value)
End If
Next
txtInsertGrossAmt.Text = GrossAmt ' - Val(DGVSetView.Text)
Call SetNetAmount()
End If
'Generate Serial//:
Dim X As Integer
Do While X < DGVSetView.Rows.Count
DGVSetView.Item(0, X).Value = X + 1
X = X + 1
Loop
'Disbaling editing in all columns except pending//:
With DGVSetView
.Columns(0).ReadOnly = True
.Columns(2).ReadOnly = True
.Columns(3).ReadOnly = True
.Columns(4).ReadOnly = True
.Columns(5).ReadOnly = True
.Columns(6).ReadOnly = True
.Columns(7).ReadOnly = True
.Columns(8).ReadOnly = True
.Columns(9).ReadOnly = True
.Columns(10).ReadOnly = True
End With
txtTotalItems.Text = DGVSetView.Rows.Count
For Each r As DataGridViewRow In DGVSetView.Rows
r.Cells(1).Value = True
Next r
End Sub
The problem is that occasionally,the DGV will not show any rows and displays a blank frame.At such instances.if I check in DGV.Rows.count
the result is 0 despite there being underlying data in the table.
Note that this happens randomly.At other times the DGV refreshed properly and also displays data correctly.
Also note that this DGV resides within a TabControl.
Further,when the DGV fails to display the data,the totals which I have calculated in the above sub procedure return zero value.As such the problem appears to lie somewhere in the rows not being inserted in the DGV.
Thank you.
Khalid.
//Edit;Code Updated:
#jmcilhinney I have updated my code as follows.However,the earlier problem of the DGV going blank occasionally persists.Also the update process has slowed down slightly.It seems I may be making a mistake in the location of placing the Suspend and ResumeBinding statements:
Private Sub SetPreview()
Dim SQLSet As String
Dim DASet As New OleDb.OleDbDataAdapter
Dim DSSet As New DataSet
SQLSet = "Select * From SetDisplayTable"
Dim DTDGV As New DataTable
Dim DGVBindingSource As New BindingSource
DASet = New OleDb.OleDbDataAdapter(SQLSet, Con)
DASet.Fill(DTDGV)
DGVBindingSource.DataSource = DTDGV
DGVBindingSource.ResumeBinding()
With DGVSetView
.AutoGenerateColumns = False
.DataSource = DGVBindingSource
.Columns(0).DataPropertyName = DTDGV.Columns(0).ToString
.Columns(2).DataPropertyName = DTDGV.Columns(1).ToString
.Columns(3).DataPropertyName = DTDGV.Columns(2).ToString
.Columns(4).DataPropertyName = DTDGV.Columns(3).ToString
.Columns(5).DataPropertyName = DTDGV.Columns(4).ToString
.Columns(6).DataPropertyName = DTDGV.Columns(5).ToString
.Columns(7).DataPropertyName = DTDGV.Columns(6).ToString
.Columns(8).DataPropertyName = DTDGV.Columns(7).ToString
.Columns(9).DataPropertyName = DTDGV.Columns(8).ToString
.Columns(10).DataPropertyName = DTDGV.Columns(9).ToString
.Columns(11).DataPropertyName = DTDGV.Columns(10).ToString 'Item Unique Code for Hot Edit
.Columns(14).DataPropertyName = DTDGV.Columns(12).ToString
End With
DGVBindingSource.SuspendBinding()
'Updating Totals/::
For ItemRow As Integer = 0 To DGVSetView.Rows.Count - 1
If DGVSetView.Rows(ItemRow).Cells(14).Value = True Then
DGVSetView.Rows(ItemRow).Cells(12).Value = DGVSetView.Rows(ItemRow).Cells(10).Value
ElseIf DGVSetView.Rows(ItemRow).Cells(14).Value = False Then
DGVSetView.Rows(ItemRow).Cells(13).Value = DGVSetView.Rows(ItemRow).Cells(10).Value
End If
Next
'Updating School and general totals in DGV//:
Dim SchoolTotal, GeneralTotal As Decimal
For ColumnTotal As Integer = 0 To DGVSetView.Rows.Count - 1
SchoolTotal += DGVSetView.Rows(ColumnTotal).Cells(12).Value
GeneralTotal += DGVSetView.Rows(ColumnTotal).Cells(13).Value
Next
txtSchoolAmtFinal.Text = SchoolTotal
txtGeneralAmtFinal.Text = GeneralTotal
DGVSetView.Update()
'Get gross total of the DGV amount column//:
If DGVSetView.RowCount <> 0 Then
Dim GrossAmt As Decimal = 0
For Index As Integer = 0 To DGVSetView.RowCount - 1
' GrossAmt += Convert.ToDecimal(DataGridView1.Rows(Index).Cells(11).Value)
If Str(DGVSetView.Rows(Index).Cells(10).Value) = "Null" Or (DGVSetView.Rows(Index).Cells(10).Value) <= 0 Then
MsgBox("Item Number " & (DGVSetView.Rows(Index).Cells(10).Value) & " is either blank or 0", MsgBoxStyle.Exclamation, "Item Error")
Else
GrossAmt += Convert.ToDecimal(DGVSetView.Rows(Index).Cells(10).Value)
End If
Next
txtInsertGrossAmt.Text = GrossAmt ' - Val(DGVSetView.Text)
Call SetNetAmount()
End If
'Disabling editing in all columns except pending//:
With DGVSetView
.Columns(0).ReadOnly = True
.Columns(2).ReadOnly = True
.Columns(3).ReadOnly = True
.Columns(4).ReadOnly = True
.Columns(5).ReadOnly = True
.Columns(6).ReadOnly = True
.Columns(7).ReadOnly = True
.Columns(8).ReadOnly = True
.Columns(9).ReadOnly = True
.Columns(10).ReadOnly = True
End With
txtTotalItems.Text = DGVSetView.Rows.Count
For Each r As DataGridViewRow In DGVSetView.Rows
r.Cells(1).Value = True
Next r
End Sub

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.