Strange issue with Datagridview-Rows not displayed - vb.net

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

Related

Dev Express chart Not Updating Second Time in Vb.net

For the first time when I run my window application it will successfully run, but when I change the database column and binding then it gives me an error that specific data column is not there in database; also I am make null chart datasource but it gives an error. Please help.
Dim ctrArr As Integer
Dim serCnt As Integer
Dim series1 As New Series
Dim seriesFound As Boolean = False
For serCnt = 0 To ctrChart.Series.Count - 1
ctrChart.Series(0).ArgumentDataMember = ""
ctrChart.Series(0).ValueDataMembers(0) = ""
ctrChart.Series(0).Visible = False
Next
For ctrArr = 0 To gStrYAxisParamArray.Length - 1 'deptname'
For serCnt = 0 To ctrChart.Series.Count - 1
If UCase(Trim(gStrYAxisParamArray(ctrArr))) = UCase(ctrChart.Series(serCnt).Name.ToString) Then
ctrChart.Series(serCnt).ArgumentDataMember = ""
ctrChart.Series(serCnt).ValueDataMembers(0) = ""
ctrChart.Series(serCnt).Visible = True
ctrChart.Series(serCnt).ArgumentDataMember = gxAxis
ctrChart.Series(serCnt).ValueDataMembers.Item(0) = Trim(gStrYAxisParamArray(ctrArr))
seriesFound = True
Exit For
End If
Next
If seriesFound = False Then
series1 = New Series(Trim(gStrYAxisParamArray(ctrArr)).ToString, ViewType.Bar)
'ctrChart.Series.AddRange(New Series() {series1, series2})
ctrChart.Series.Add(series1)
series1.ArgumentDataMember = ""
series1.ValueDataMembers(0) = ""
series1.Visible = True
series1.ArgumentDataMember = gxAxis
series1.Label.Border.Visible = False
series1.ValueDataMembers(0) = Trim(gStrYAxisParamArray(ctrArr))
series1.LegendText = Trim(gStrYAxisParamArray(ctrArr).ToString)
End If
seriesFound = False
Next
cmbSeries.Items.Clear()
For ctrArr = 0 To ChrtStockDept.Series.Count - 1
With cmbSeries.Items
cmbSeries.Items.Add(ChrtStockDept.Series(ctrArr).Name.ToString)
End With
Next

why i am getting this error "There is no row at position 0 vb.net"?

I am trying to add values from LANG_OBJ.TEXT to DataTableRow.
While adding i am getting a error:
There is no row at position 0
dtsaveTranslate = checkTranslateValues()
lang_id_text CType(Controls.Find("txt_id"True).FirstOrDefault(),TextB`enter code here`ox)
lang_de_text = CType(Controls.Find("txt_de", True).FirstOrDefault(), TextBox)
lang_row = dtsaveTranslate.NewRow()
lang_row("de") = lang_de_text
For Each row As DataRow In dtlang.Rows
lang_iso = Convert.ToString(row("ISO"))
lang_obj = CType(Controls.Find("txt_" + lang_iso, True).FirstOrDefault(), TextBox)
Dim len As Integer = lang_obj.Text.Length
Dim count_de As Integer = lang_de_text.Text.Length
progress.ProgressValue = len + 1
If Convert.ToString(row("isUbersetzen")) = "True" AndAlso lang_iso <> "de" Then
lang_obj.Text = lanClass.GoogleApiTranslate("de", Convert.ToString(row("ISO")), lang_de_text.Text.Trim())
lang_row(lang_iso) = lang_obj.Text
Else
count_txt = 0
End If
Next
dtsaveTranslate.Rows.Add(lang_row)
First you need to make sure that dtLang has rows and then try to loop them!
You need to make sure that dtLang is loaded with data. You should research why it doesn't contain any rows first. From the code above I can't see how the rows are loaded in there. Therefore, the proper check should start with:
If dtlang.Rows.count > 0 '<--- Added a check before looping
For Each row As DataRow In dtlang.Rows
lang_iso = Convert.ToString(row("ISO"))
lang_obj = CType(Controls.Find("txt_" + lang_iso, True).FirstOrDefault(), TextBox)
Dim len As Integer = lang_obj.Text.Length
Dim count_de As Integer = lang_de_text.Text.Length
progress.ProgressValue = len + 1
If Convert.ToString(row("isUbersetzen")) = "True" AndAlso lang_iso <> "de" Then
lang_obj.Text = lanClass.GoogleApiTranslate("de", Convert.ToString(row("ISO")), lang_de_text.Text.Trim())
lang_row(lang_iso) = lang_obj.Text
Else
count_txt = 0
End If
Next
End If

Read, Get, Compare and Count Rows from GridView VB.net?

i have a little problem and actually i can't get out from his.. I have a column called 'Periodicidade' that gives me how many times must a thing get done.
What i want to do is a validation that it will count how many times exists one of them, and if one of them have the radiobutton fill, it will force the user to fill the others of the same type of periocidade, but in the moment he is counting the total gridview rows, and not the type of same periodicidade. My current code is:
Dim todos_items_periocidade_vazios As Boolean = True
Dim todos_items_periocidade_preenchidos As Boolean = False
Dim periocidade_validada As Boolean = False
'quantas linhas tem a grid
Dim n_linha_grid As Integer = GridView_Manutencao.Rows.Count
Dim periocidade_linha As String
Dim contador_preenchido As Integer = 0
Dim contador_linhas As Integer = 0
'para cada linha verificar
For Each row2 As GridViewRow In GridView_Manutencao.Rows
'percorrer tabela e validar a periocidade da linha encontrada
periocidade_linha = (CType(row2.FindControl("Label_Periodicidade"), Label).Text)
For Each row As GridViewRow In GridView_Manutencao.Rows
If ((CType(row.FindControl("Label_Periodicidade"), Label).Text) = periocidade_linha) Then
contador_linhas = contador_linhas + 1
periocidade_validada = True
If periocidade_validada = True Then
'testar se está vazio ou preenchido
If (CType(row.FindControl("RBList"), RadioButtonList).SelectedValue = "") Then
'percorrer a tabela e verificar se todos os itens estão vazios ou não
For Each row1 As GridViewRow In GridView_Manutencao.Rows
If ((CType(row1.FindControl("Label_Periodicidade"), Label).Text) = periocidade_linha) Then
If (CType(row1.FindControl("RBList"), RadioButtonList).SelectedValue = "") Then
todos_items_periocidade_vazios = True
Else
todos_items_periocidade_vazios = False
End If
End If
Next
Else
For Each row1 As GridViewRow In GridView_Manutencao.Rows
If ((CType(row1.FindControl("Label_Periodicidade"), Label).Text) = periocidade_linha) Then
If (CType(row1.FindControl("RBList"), RadioButtonList).SelectedValue <> "") Then
contador_preenchido = contador_preenchido + 1
todos_items_periocidade_preenchidos = True
Else
todos_items_periocidade_preenchidos = False
End If
End If
Next
End If
End If
End If
Next
valida_comentario()
If contador_preenchido = 0 Then
periocidade_validada = False
tudo_validado = False
ElseIf contador_preenchido < contador_linhas Then
periocidade_validada = False
tudo_validado = False
Else
If valida_comentario() = True Then
If ((todos_items_periocidade_vazios = True) And (todos_items_periocidade_preenchidos = True)) Then
periocidade_validada = True
tudo_validado = True
Lbl_Mensagem.Text = "Registo inserido com sucesso!"
ElseIf ((todos_items_periocidade_vazios = False) And (todos_items_periocidade_preenchidos = True)) Then
periocidade_validada = True
tudo_validado = True
Lbl_Mensagem.Text = "Registo inserido com sucesso!"
Else
periocidade_validada = False
tudo_validado = False
Lbl_Mensagem.Text = "Erro"
End If
Else
tudo_validado = False
periocidade_validada = False
Lbl_Mensagem.Text = "Erro"
End If
End If
Next
Return periocidade_validada
I got the answer, hope to be useful to all of you, thanks, here it goes:
Dim texto_periodicidade As String
Dim valor As String
Dim flag_validacao As Boolean = False
Dim contador_falso As Integer
contador_falso = 0
For Each linha As GridViewRow In GridView_Manutencao.Rows
texto_periodicidade = CType(linha.FindControl("Label_Periodicidade"), Label).Text
valor = CType(linha.FindControl("RBList"), RadioButtonList).SelectedValue
For Each row As GridViewRow In GridView_Manutencao.Rows
If (texto_periodicidade = CType(row.FindControl("Label_Periodicidade"), Label).Text) Then
If (valor = CType(row.FindControl("RBList"), RadioButtonList).SelectedValue) Then
flag_validacao = True
Else
contador_falso = contador_falso + 1
flag_validacao = False
End If
End If
Next row
Next linha
If contador_falso > 0 Then
Lbl_Mensagem.Text = "Complete os restantes valores da periocidade!"
contador_falso = 0
Return False
Else
If valida_comentario() = True Then
contador_falso = 0
Return True
End If
End If

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.

Adding rows to a dataGridView dynamically

VB.NET 4.0 framework Windows Forms Application. So I have a DataGridView that I have dropped on my form in designer, set all the columns to readOnly, AllowUserToAddRows = False, AllowUserToDeleteRows = False. Now for the part where it the code is going bad at.
My function Looks Like this:
Private Sub fill_items()
Dim prop As List(Of property_info) = db.property_info.ToList
Dim units As List(Of unit) = db.units.ToList
Dim _year As Integer = System.DateTime.Now.Year
Dim fin As List(Of financial) = db.financials.Where(Function(f) f.Year_start.Value.Year = _year).OrderBy(Function(f) f.transaction_date).ToList
Dim x As Integer = 0
For Each _i In prop
x += 1
Dim _typeName As String = String.Empty
Dim i As property_info = _i
Select Case i.property_type
Case Is = 0
_typeName = "Storage"
Case Is = 1
_typeName = "House/Apartment"
Case Is = 2
_typeName = "Office Space"
End Select
reports1GridView.Rows.Add(_typeName, i.property_Name, " ", " ", " ", " ")
For Each _t In units.Where(Function(f) f.propertyId = i.idProperties)
Dim t As unit = _t
x += 1
For Each trans In fin.Where(Function(F) F.Unit_finId = t.UnitId)
x += 1
Dim _ttype As String = String.Empty
Dim _typeCheck As Integer = 0
Select Case trans.transaction_type
Case Is = 0
_ttype = "Payment Recieved"
_typeCheck = 0
Case Is = 2
_ttype = "Rent Charged"
_typeCheck = 1
Case Is = 3
_ttype = "Deposit"
_typeCheck = 1
Case Is = 20
_ttype = "Late Fee"
_typeCheck = 0
Case Is = 4
_ttype = "Auction Collection"
_typeCheck = 0
Case Is = 5
_ttype = "Auction Fee"
_typeCheck = 2
Case Is = 6
_ttype = "City Tax"
_typeCheck = 0
Case Is = 7
_ttype = "County Tax"
_typeCheck = 0
Case Is = 8
_ttype = "State Tax"
_typeCheck = 0
Case Is = 9
_ttype = "Maintenance"
_typeCheck = 2
End Select
Dim _TypeValue As Decimal = Nothing
Select Case _typeCheck
Case Is = 0
_TypeValue = trans.Amount_Paid
Case Is = 1
_TypeValue = trans.amount_due
Case Is = 2
End Select
Dim _tDate As Date = trans.transaction_date
Dim _tDateString As String = _tDate.ToShortDateString.ToString
reports1GridView.Rows.Add(" ", " ", t.UnitId, _ttype, _tDateString, _TypeValue)
Dim xl As String = String.Empty
Next
Next
Next
End Sub
My problem is that the datagridview is displaying only values in the 0,1,2,3 columns of the gridview.. The Gridview looks correct until it Gets to column 3 which is where the transaction type goes. For some reason the amount that should be in column 5 is being displayed in that column and columns 4 and 5 are being left completely blank.. I looked at the values contained in the variables in the last reports1GridView.Rows.Add of the function and all of the variables are not only correct but in the correct order. So my question is why is this failing...
Supposing that your DataGridView is unbound (meaning that no columns are automatically defined) you need to create the appropriate columns required by your code. Then the Row.Add(item, ....) will work
For example:
Private Sub SetupGrid()
reports1GridView.ColumnCount = 5
reports1GridView.Columns(0).Name = "Type"
.... ' other columns
End Sub
before entering in your main loop call this method to define name and type of your columns