Use structure or 2 dimensional array as source for RDLC - vb.net

I've done some Googling but can't find anything concrete enough to go forward with, so here I am.
I have date fields that I am running a WHERE clause against, and returning fields based on that. The date fields are encrypted, so I was forced to take an alternative approach.
Dim aEmpList(dt.Rows.Count, 5) As String
Try
lCount = 0
For i As Integer = 0 To dt.Rows.Count - 1
If clsEncrypt.DecryptData(dt.Rows(i)(3)) >= 19500101 Then
aEmpList(lCount, 0) = clsEncrypt.DecryptData(dt.Rows(i)(0))
aEmpList(lCount, 1) = clsEncrypt.DecryptData(dt.Rows(i)(1))
aEmpList(lCount, 2) = clsEncrypt.DecryptData(dt.Rows(i)(2))
aEmpList(lCount, 3) = clsEncrypt.DecryptData(dt.Rows(i)(3))
aEmpList(lCount, 4) = clsEncrypt.DecryptData(dt.Rows(i)(4))
lCount = lCount + 1
End If
Next
Catch ex As Exception
MessageBox.Show(e.ToString())
End Try
clsEncrypt is an encryption/decryption class.
The fields are Employee First Name, Last Name, and Date of Birth - and they are all encrypted.
I am trying to find all Employees born>= 19500101, and return that data as a report.
I created a Structure and put the data as an array into the structure myStructure
'Define a structure to be used as source for data grid view.
Structure mystructure
Private mDim1 As String
Private mDim2 As String
Private mDim3 As String
Private mDim4 As String
Private mDim5 As String
Public Property Dim1() As String
Get
Return mDim1
End Get
Set(ByVal value As String)
mDim1 = value
End Set
End Property
Public Property Dim2() As String
Get
Return mDim2
End Get
Set(ByVal value As String)
mDim2 = value
End Set
End Property
Public Property Dim3() As String
Get
Return mDim3
End Get
Set(ByVal value As String)
mDim3 = value
End Set
End Property
Public Property Dim4() As String
Get
Return mDim4
End Get
Set(ByVal value As String)
mDim4 = value
End Set
End Property
Public Property Dim5() As String
Get
Return mDim5
End Get
Set(ByVal value As String)
mDim5 = value
End Set
End Property
End Structure
I populate the structure to load into a DataGridView for visualization purposes:
'populate structure with aEmpList array
Dim myarr(dt.Rows.Count) As mystructure
Try
For i As Integer = 0 To lCount - 1
myarr(i) = New mystructure With {.Dim1 = aEmpList(i, 0).ToString, .Dim2 = aEmpList(i, 1).ToString, .Dim3 = aEmpList(i, 2).ToString, .Dim4 = aEmpList(i, 3).ToString, .Dim5 = aEmpList(i, 4).ToString}
Next
Catch ex As Exception
MessageBox.Show(e.ToString())
End Try
DataGridView1.DataSource = myarr
Now that I've provided sufficient background, does anyone know what my next move should be?
I'm not sure how, or if, I should load the structure into a DataSet() [if that's even possible] or something of that nature.

As I see it you're trying to create a business class, and thus should use a reference type (Class) instead of a value type (Structure). So, the "next move" would be to change your structure to a class, implement INotifyPropertyChanged, IEditableObject, IChangeTracking, IRevertibleChangeTracking, and create a BindingList(Of T) to hold your items.

So, this kind of sucked to figure out, but I got it. As the question title says - I actually ditched the array portion because all it did was confuse me, and I went with the following solution:
Dim dt As New DataTable
Dim dts As DataSet = New DataSet()
With comm
.CommandText = "SELECT EMPL_ID, EMPL_FIRST_NM, EMPL_LAST_NM, BEG_DT, END_DT FROM EMPL"
End With
Dim adapter As New SqlDataAdapter(comm)
adapter.Fill(dts) 'Fill DT with Query results
dt.Load(dts.CreateDataReader())
Me.reportViewer1.RefreshReport()
Dim dts2 As New DataSet()
dts2 = dts.Clone
Try
Dim m As Integer
m = 0
For i As Integer = 0 To dts.Tables(0).Rows.Count - 1
If clsEncrypt.DecryptData(dts.Tables(0).Rows(i)(3)) >= "20130101" Then
dts2.Tables(0).Rows.Add()
dts2.Tables(0).Rows(m)(0) = clsEncrypt.DecryptData(dts.Tables(0).Rows(i)(0))
dts2.Tables(0).Rows(m)(1) = clsEncrypt.DecryptData(dts.Tables(0).Rows(i)(1))
dts2.Tables(0).Rows(m)(2) = clsEncrypt.DecryptData(dts.Tables(0).Rows(i)(2))
dts2.Tables(0).Rows(m)(3) = clsEncrypt.DecryptData(dts.Tables(0).Rows(i)(3))
dts2.Tables(0).Rows(m)(4) = clsEncrypt.DecryptData(dts.Tables(0).Rows(i)(4))
'dts2.Tables(0).Rows.Add(dts.Tables(0).Rows(i).cl)
m = m + 1
End If
Next
Catch ex As Exception
MessageBox.Show(e.ToString())
End Try
Dim rds As ReportDataSource = New ReportDataSource("DataSetDateOfBirthTest", dts2.Tables(0))
With reportViewer1
.LocalReport.DataSources.Clear()
.LocalReport.DataSources.Add(rds)
.RefreshReport()
End With

Related

Round cell values of a datatable (VB.NET)

I have trouble rounding some cells from my datatable.
I want this round to 2 decimal places, as you can imagine. I will explain quickly how I charge data to the DataTable :
I have this function stored in a class:
Protected Friend Function cargarPref(ByVal id_Pref As String) As DataTable
Dim cmd As String = "Select Material,Cubicaje,SubTotal,ITBM,Total from Preferencia WHERE Id_Preferencia=#id_Pref"
Dim t As New DataTable
Try
con.Open()
comando = New OleDbCommand(cmd, con)
comando.Parameters.AddWithValue("#id_Pref", id_Pref)
adapter = New OleDbDataAdapter(comando)
adapter.Fill(t)
comando.Dispose()
adapter.Dispose()
con.Close()
Catch ex As Exception
MsgBox("Error en la consulta: " + ex.Message, MsgBoxStyle.Critical)
End Try
Return t
End Function
Ok, now I call it in my windows form:
Private Sub DataCliente_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataCliente.CellContentClick
If (Not IsNothing(DataMate)) Then
DataMate.DataSource = Nothing
mt.Clear()
End If
Dim index As Integer = 0
If (DataCliente.Columns(DataCliente.CurrentCell.ColumnIndex).Name.Equals("Empresa")) Then
index = Me.DataCliente.CurrentRow.Index
lblid.Text = DataCliente.Rows(index).Cells("Id_Cliente").Value
lblempresa.Text = DataCliente.Rows(index).Cells("Empresa").Value
lbldirecc.Text = DataCliente.Rows(index).Cells("Direccion").Value
lblcorreo.Text = DataCliente.Rows(index).Cells("Correo").Value
lbltel.Text = DataCliente.Rows(index).Cells("Telefono").Value
lblpreyd.Text = Math.Round(CDbl(DataCliente.Rows(index).Cells("PrecioYD").Value), 2).ToString("N2")
End If
mt = data.cargarPref(DataCliente.Rows(index).Cells("Id_Preferencia").Value) <--HERE!!!
DataMate.DataSource = mt
PanelMaterial.Enabled = True
End Sub
Now, watch as my values ​​are in my database access, Along With the datagrid of the program!
For some strange reason ... the data I have taken from the database , the program treats them as if they were integers.
How I can then round off those values ​​that are within the datatable, before printing in datagridview?
I finally discovered a solution , and I am going to show you how to solve this problem, if you need it (Before you do this , you must define all the columns you want to see on your datagridview !):
First, I create a class with public properties:
Public Class Preferencia
Public Property Material() As String
Public Property Cubicaje() As String
Public Property SubTotal() As String
Public Property ITBM() As String
Public Property Total() As String
End Class
Second , I create 2 functions and one procedural method :
Private Function setPreferencia(ByVal material As String, ByVal cubicaje As String, ByVal subtotal As String, ByVal itbm As String, ByVal total As String) As Preferencia
Dim item As New Preferencia
item.Material = material
item.Cubicaje = FormatNumber(cubicaje, 2)
item.SubTotal = FormatNumber(subtotal, 2)
item.ITBM = FormatNumber(itbm, 2)
item.Total = FormatNumber(total, 2)
Return item
End Function
Private Function registrarPreferencia() As List(Of Preferencia)
Dim lista As New List(Of Preferencia)
For Each itm In mt.Rows
lista.Add(setPreferencia(itm(0), itm(1), itm(2), itm(3), itm(4)))
Next
Return lista
Protected Friend Sub FillGrid()
DataMate.AutoGenerateColumns = False
DataMate.DataSource = registrarPreferencia()
DataMate.Columns("Material").DataPropertyName = "Material"
DataMate.Columns("Cubicaje").DataPropertyName = "Cubicaje"
DataMate.Columns("SubTotal").DataPropertyName = "SubTotal"
DataMate.Columns("ITBM").DataPropertyName = "ITBM"
DataMate.Columns("Total").DataPropertyName = "Total"
End Sub
Now the only thing is to call the method " FillGrid ()" when you want to print the data. And finaly, I resolved it!

Trying to populate combobox from SQLite VB.NET Windows Universal App

I am new to SQLite and I am trying to populate a ComboBox in a Windows Universal App from the database. I did the following so far:
With cmbPaciente.Items
Try
Dim sConexao As String = Path.Combine(ApplicationData.Current.LocalFolder.Path, "\Banco\Pronto Facil.db")
Dim aConexao As New SQLite.SQLiteConnection(sConexao)
Dim Comando = aConexao.Execute("select Nome from Dados_Pessoais")
For Each item In Comando
.Add(item)
Next
Catch ex As Exception
.Add("Text 1")
.Add("Text 2")
End Try
End With
I am getting this error:
BC32023 Expression is of type 'Integer', which is not a collection type.
I understood thats because aConexao.Execute is returning an integer, but how else should I do it then?
I was using the wrong approach, this way it Works:
With cmbPaciente.Items
Dim sConexao As String = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Deal.db")
Dim aConexao As New SQLiteConnection(New SQLitePlatformWinRT(), sConexao)
For Each item As Stockvb In aConexao.Table(Of Stockvb)()
Dim newlist As New Stockvb()
newlist.Id = item.Id
newlist.Symbol = item.Symbol
combolist.Add(newlist)
Next
cmbPaciente.ItemsSource = combolist
End With
And here is the Stockvb class:
Public Class Stockvb
<PrimaryKey, AutoIncrement>
Public Property Id() As Integer
Get
Return m_Id
End Get
Set
m_Id = Value
End Set
End Property
Private m_Id As Integer
<MaxLength(8)>
Public Property Symbol() As String
Get
Return m_Symbol
End Get
Set
m_Symbol = Value
End Set
End Property
Private m_Symbol As String
End Class
Special thanks to Grace Feng - MSFT

How to access class properties outside the constructor

I'm trying to retrieve my other football attributes (crossing, dribbling, finishing, ball control) that haven't been requested as arguments in the constructor- reportID, playerID and comments are all that are required.
There are over 20 football attributes, so I've just included the first few for readability.
Report Class:
Public Class Report
Public Sub New(ByVal reportID As Integer, ByVal playerID As Integer, ByVal comments As String)
_ReportID = reportID
_PlayerID = playerID
_Comments = comments
End Sub
Private _ReportID As Integer = 0
Private _PlayerID As Integer = 0
Private _Comments As String = ""
Private _Crossing As Integer = 0
Private _Dribbling As Integer = 0
Private _Finishing As Integer = 0
Private _BallControl As Integer = 0
Public Property ReportID() As Integer
Get
Return _ReportID
End Get
Set(ByVal value As Integer)
_ReportID = value
End Set
End Property
Public Property PlayerID() As Integer
Get
Return _PlayerID
End Get
Set(ByVal value As Integer)
_PlayerID = value
End Set
End Property
Public Property Comments() As String
Get
Return _Comments
End Get
Set(ByVal value As String)
_Comments = value
End Set
End Property
Public Property Crossing() As Integer
Get
Return _Crossing
End Get
Set(ByVal value As Integer)
_Crossing = value
End Set
End Property
Public Property Dribbling() As Integer
Get
Return _Dribbling
End Get
Set(ByVal value As Integer)
_Dribbling = value
End Set
End Property
Public Property Finishing() As Integer
Get
Return _Finishing
End Get
Set(ByVal value As Integer)
_Finishing = value
End Set
End Property
Public Property BallControl() As Integer
Get
Return _BallControl
End Get
Set(ByVal value As Integer)
_BallControl = value
End Set
End Property
End Class
Below I realise I'm only adding reportID, playerID and comments to my typeList, which is why I'm getting all 0's for my other attributes. How do access the attributes as well?
Retrieving the data:
Private Function retrieveReport() As List(Of Report)
Dim typeList As New List(Of Report)
Dim Str As String = "SELECT * FROM Report ORDER BY PlayerID"
Try
Using conn As New SqlClient.SqlConnection(DBConnection)
conn.Open()
Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
Using drResult As SqlClient.SqlDataReader = cmdQuery.ExecuteReader()
While drResult.Read
typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")))
End While
End Using 'Automatically closes connection
End Using
End Using
Catch ex As Exception
MsgBox("Report Exception: " & ex.Message & vbNewLine & Str)
End Try
Return typeList
End Function
Private Sub setReport()
For Each rpt As Report In retrieveReport()
'*****General Information
UC_Menu_Scout1.txtComments.Text = rpt.Comments
'*****Technical
UC_Menu_Scout1.UcAttributes1.lblXCrossing.Text = rpt.Crossing
UC_Menu_Scout1.UcAttributes1.lblXDribbling.Text = rpt.Dribbling
UC_Menu_Scout1.UcAttributes1.lblXFinishing.Text = rpt.Finishing
UC_Menu_Scout1.UcAttributes1.lblXBallControl.Text = rpt.BallControl
UC_Menu_Scout1.UcAttributes1.lblXPassing.Text = rpt.Passing
UC_Menu_Scout1.UcAttributes1.lblXHeadingAccuracy.Text = rpt.HeadingAccuracy
UC_Menu_Scout1.UcAttributes1.lblXMarking.Text = rpt.Marking
UC_Menu_Scout1.UcAttributes1.lblXTackling.Text = rpt.Tackling
'*****Mental
UC_Menu_Scout1.UcAttributes1.lblXAggression.Text = rpt.Aggression
UC_Menu_Scout1.UcAttributes1.lblXPositioning.Text = rpt.Positioning
UC_Menu_Scout1.UcAttributes1.lblXAnticipation.Text = rpt.Anticipation
UC_Menu_Scout1.UcAttributes1.lblXComposure.Text = rpt.Composure
UC_Menu_Scout1.UcAttributes1.lblXVision.Text = rpt.Vision
UC_Menu_Scout1.UcAttributes1.lblXTeamwork.Text = rpt.Teamwork
UC_Menu_Scout1.UcAttributes1.lblXWorkRate.Text = rpt.WorkRate
'*****Physical
UC_Menu_Scout1.UcAttributes1.lblXPace.Text = rpt.Pace
UC_Menu_Scout1.UcAttributes1.lblXBalance.Text = rpt.Balance
UC_Menu_Scout1.UcAttributes1.lblXJumping.Text = rpt.Jumping
UC_Menu_Scout1.UcAttributes1.lblXStrength.Text = rpt.Strength
UC_Menu_Scout1.UcAttributes1.lblXStamina.Text = rpt.Stamina
Next
End Sub
I imagine it's not that hard, so any help would be appreciated please!
To add values to the properties, use With:
typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")) With {.BallControl = drResult("BallControl"), .Dribbling = drResult("Dribbling")})
You need to assign the property values that you aren't providing to the constructor as arguments, or you can add arguments to the constructor. Try this -- instead of calling the constructor like this:
typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")))
instead, do this:
dim rep = New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments"))
.Crossing = drResult("Crossing")
'additional property assignments
With rep
End With
typeList.Add(rep)

initialize a class from datarow

I'm trying to initialize a class using a DataRow, But when the DataRow item "LOCATION" is a Null value I receive an error. How can I load these variables with their appropriate datatype values when the row item is null?
Public Sub New(ByVal row As DataRow)
intID = row.Item("ID")
strDepartmentCode = row.Item("DEPARTMENT_CODE")
strDepartmentName = row.Item("DEPARTMENT_NAME")
intLocation = row.Item("LOCATION")
End Sub
Something like this:
Dim location As Object = row.Item("LOCATION")
If location Is Convert.DBNull
intLocation = -1
Else
intLocation = Convert.ToInt32(location)
End If

How to add item when using List (Of)?

I created a class where i declared some properties.
Public Class BlogPost
Dim _postTitleUrl As String = String.Empty
Dim _pageGUID As String = String.Empty
Property postTitleUrl() As String
Get
Return _postTitleUrl
End Get
Set(ByVal value As String)
_postTitleUrl = value
End Set
End Property
Property pageGUID() As String
Get
Return _pageGUID
End Get
Set(ByVal value As String)
_pageGUID = value
End Set
End Property
End Class
Now, I have another class where I want to set the values.
Public Class SetBlogData
Public blogPostList As New List(Of BlogPost)
Public dataCounter as integer = 0
blogPostList(dataCounter).pageGUID = mainBlogSPWeb.ID.ToString
....
This gives me an error about Index was out of range. Hpw can I properly access the properties in BlogPost class?
Because your list has nothing .
You should use add method to add your new item. Like ...
Dim blogPostList = New List(Of BlogPost)
Dim blogPost = New BlogPost
blogPost.pageGUID = mainBlogSPWeb.ID.ToString
blogPostList.Add(blogPost)
You need to put a BlogPost in your list by writing blogPost.List.Add(New BlogPost())
I find this a good way of inserting into a list:
1) Check if the list is nothing
2) If so instantiate a new list
3) Add the value to list.
Below is an example.
Private Sub ExampleAddValueToList(ByVal value as BlogPost)
Try
If _blogPostList Is Nothing Then
_blogPostList = New List(Of BlogPost)
End If
_blogPostList.Add(value)
Catch ex As Exception
debug.write(ex.message)
End Try
End Sub