trying to query a database - vb.net

I'm having a bit of a problem populating a collections class with the values from the database. Everytime I loop through a record in the WHILE DR.READ loop, the last record over writes all the other items in the collection. My returnVal collections has several of the same items despite the loop showing each individual record being added into returnVal. Thanks for any help.
Public Shared Function getStuff(ByVal sb As StringBuilder) As System.Collections.Generic.List(Of Minutes)
Dim returnVal As New System.Collections.Generic.List(Of Minutes)
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
cmd.Connection = conn
cmd.CommandText = sb.ToString
Try
conn.Open()
dr = cmd.ExecuteReader
While dr.Read
Dim _minutes As New Minutes
_minutes.Minutes = dr("minutes")
_minutes.MinutesId = dr("minutesId")
returnVal.Add(_minutes)
End While
Catch ex As Exception
Dim _minutes As New Minutes
_minutes.Minutes = ex.ToString
_minutes.MinutesId = 0
returnVal.Add(_minutes)
End Try
conn.Close()
Return returnVal
End Function
This is my Minutes Class
Imports Microsoft.VisualBasic
Public Class Minutes
Private Shared _minutesId As Integer
Private Shared _minutes As String
Public Property MinutesId() As Integer
Get
Return _minutesId
End Get
Set(ByVal value As Integer)
_minutesId = value
End Set
End Property
Public Property Minutes() As String
Get
Return _minutes
End Get
Set(ByVal value As String)
_minutes = value
End Set
End Property
Public Shared Function getStuff(ByVal sb As StringBuilder) As System.Collections.Generic.List(Of Minutes)
Return MinutesDA.getStuff(sb)
End Function
Public Shared Function modify(ByVal sb As StringBuilder) As String
Return MinutesDA.modify(sb)
End Function
Public Shared Property Id() As Integer
Get
Return MinutesDA.Id
End Get
Set(ByVal value As Integer)
MinutesDA.Id = value
End Set
End Property
Public Shared Property Index() As Integer
Get
Return MinutesDA.Index
End Get
Set(ByVal value As Integer)
MinutesDA.Index = value
End Set
End Property
End Class

The problem is that the fields in the Minutes class are shared. There will be one instance of the fields shared by all instances of the class. Remove the "Shared" from the fields:
Private _minutesId As Integer
Private _minutes As String
This will store the values for each instance separately

To confirm you are getting new values or the same value try this:
Dim i as Integer
i = 0
While dr.Read
Dim _minutes As New Minutes
_minutes.Minutes = dr("minutes")
' _minutes.MinutesId = dr("minutesId")
_minutes.MinutesId = i
i = i + 1
returnVal.Add(_minutes)
End While
After this you can see if the MinuteId is different.
N.B. In VB6 you had to set the _minutes to Nothing at the end of the loop to get a new instance. However I wouldn't have thought that would be true in VB.NET

Related

Populate class from query on VB Net

Help translate C# code from this link Simplest way to populate class from query in C# to VB Net.
Option Infer On
Imports System.Reflection
Private Sub Main()
Dim connectionString = "..."
Dim records = (New Query(connectionString)).SqlQuery(Of TVChannel)("select top 10 * from TVChannel")
End Sub
Private Class TVChannel
Public Property number() As String
Public Property title() As String
Public Property favoriteChannel() As String
Public Property description() As String
Public Property packageid() As String
Public Property format() As String
End Class
Public Class Query
Private ReadOnly _connectionString As String
Public Sub New(ByVal connectionString As String)
_connectionString = connectionString
End Sub
Public Function SqlQuery(Of T)(ByVal query As String) As List(Of T)
Dim result = New List(Of T)()
Using connection = New SqlConnection(_connectionString)
connection.Open()
Using command = connection.CreateCommand()
command.CommandText = query
Using reader = command.ExecuteReader()
Dim columns = Enumerable.Range(0, reader.FieldCount).Select(Function(f) reader.GetName(f)).ToArray()
Dim properties = GetType(T).GetProperties()
Do While reader.Read()
Dim data = New Object(reader.FieldCount - 1){}
reader.GetValues(data)
Dim instance = DirectCast(Activator.CreateInstance(GetType(T)), T)
For i = 0 To data.Length - 1
If data(i) Is DBNull.Value Then
data(i) = Nothing
End If
Dim [property] = properties.SingleOrDefault(Function(x) x.Name.Equals(columns(i), StringComparison.InvariantCultureIgnoreCase))
If [property] IsNot Nothing Then
[property].SetValue(instance, Convert.ChangeType(data(i), [property].PropertyType))
End If
Next i
result.Add(instance)
Loop
End Using
End Using
End Using
Return result
End Function
End Class
but, I got error on this line
Dim instance = DirectCast(Activator.CreateInstance(GetType(T)), T)
System.MissingMethodException: 'No parameterless constructor defined for this object.'
This is a much better pattern to follow. It addresses at least four issues in the original code (sql injection, Nothing vs null, constructor access, unnecessary allocations):
Public Module SQL
Private ReadOnly _connectionString As String = "..."
Public Iterator Function Query(Of T)(ByVal query As String, translate As Func(IDataRecord, T), ParamArray data() As SqlParameter) As IEnumerable(Of T)
Using connection As New SqlConnection(_connectionString), _
command As New SqlCommand(query, connection)
If data IsNot Nothing Then command.Parameters.AddRange(data)
connection.Open()
Using reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
Yield translate(reader)
End While
reader.Close()
End Using
End Using
End Function
End Module
Call it like this:
Private Sub Main()
Dim records = SQL.Query("select top 10 * from TVChannel",
Function(r)
'Yes, you're doing the mapping manually now for each query.
'But this lets you properly account for things NULL, column name mismatches, computed properties, etc.
Return New TVChannel With {
.number = r["number"],
.title = r["title"],
.favoriteChannel = r["favoriteChannel"],
.description = r["description"],
.packageid = r["packageid"],
.format = r["format"]
}
End Function,
Nothing)
For Each channel As TVChannel In records
Console.WriteLine($"Channel {channel.number}, {channel.title}")
Next
End Sub

Why does the combobox throws error when button associated with its event is clicked

Basic rundown of program: connected to a database. Combo box is populated with a terms list, after you select a term a get term button is pressed which populates a listview and then returns total balance due in a text box at the bottom of the form.
The Combobox populates, but when the btn is pressed catch ex throws an error along the lines of string cannot be converted to integer(occurs on the form design code). I'm not quite sure where I've gone wrong. It doesn't seem to catch anything else anywhere.
I'll include the code below
Imports Payables
Public Class Form1
Dim invoiceList As List(Of Invoices)
Dim termList As List(Of Terms)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadComboBoxes()
End Sub
Private Sub LoadComboBoxes()
termList = TermsDB.GetTermsList
cboTerms.DataSource = termList
cboTerms.ValueMember = "TermsID"
cboTerms.DisplayMember = "Description"
End Sub
Private Sub btnGetInvoice_Click(sender As Object, e As EventArgs) Handles btnGetInvoice.Click
Dim invoiceList As List(Of Invoices)
Dim TermsID = CInt(cboTerms.SelectedValue)
Try
invoiceList = InvoicesDB.FindInvoiceByID(TermsID)
txtTotalBalanceDue.Text = FormatCurrency(InvoicesDB.GetBalanceDue())
If invoiceList.Count > 0 Then
Dim invoice As Invoices
For i = 0 To invoiceList.Count - 1
invoice = invoiceList(i)
lvInvoices.Items.Add(invoice.InvoiceID)
lvInvoices.Items(i).SubItems.Add(invoice.VendorID)
lvInvoices.Items(i).SubItems.Add(invoice.InvoiceNumber)
lvInvoices.Items(i).SubItems.Add(invoice.InvoiceDate)
lvInvoices.Items(i).SubItems.Add(invoice.InvoiceTotal)
lvInvoices.Items(i).SubItems.Add(invoice.PaymentTotal)
lvInvoices.Items(i).SubItems.Add(invoice.CreditTotal)
lvInvoices.Items(i).SubItems.Add(invoice.TermsID)
lvInvoices.Items(i).SubItems.Add(invoice.DueDate)
lvInvoices.Items(i).SubItems.Add(invoice.PaymentDate)
Next
Else
MessageBox.Show("There is no info on this account")
Me.Close()
End If
Catch ex As Exception
Throw ex
Me.Close()
End Try
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Public Class Invoices
Dim m_InvoiceID As Integer
Dim m_VendorID As Integer
Dim m_InvoiceNumber As String
Dim m_InvoiceDate As Date
Dim m_InvoiceTotal As Decimal
Dim m_PaymentTotal As Decimal
Dim m_CreditTotal As Decimal
Dim m_TermsID As Integer
Dim m_DueDate As Date
Dim m_PaymentDate As Date
Public Sub New()
End Sub
Public Property InvoiceID() As Integer
Get
Return m_InvoiceID
End Get
Set(value As Integer)
m_InvoiceID = value
End Set
End Property
Public Property VendorID() As Integer
Get
Return m_VendorID
End Get
Set(value As Integer)
m_VendorID = value
End Set
End Property
Public Property InvoiceNumber() As String
Get
Return m_InvoiceNumber
End Get
Set(value As String)
m_InvoiceNumber = value
End Set
End Property
Public Property InvoiceDate() As Date
Get
Return m_InvoiceDate
End Get
Set(value As Date)
m_InvoiceDate = value
End Set
End Property
Public Property InvoiceTotal() As Decimal
Get
Return m_InvoiceTotal
End Get
Set(value As Decimal)
m_InvoiceTotal = value
End Set
End Property
Public Property PaymentTotal() As Integer
Get
Return m_PaymentTotal
End Get
Set(value As Integer)
m_PaymentTotal = value
End Set
End Property
Public Property CreditTotal() As Integer
Get
Return m_CreditTotal
End Get
Set(value As Integer)
m_CreditTotal = value
End Set
End Property
Public Property TermsID() As Integer
Get
Return m_TermsID
End Get
Set(value As Integer)
m_TermsID = value
End Set
End Property
Public Property DueDate() As Date
Get
Return m_DueDate
End Get
Set(value As Date)
m_DueDate = value
End Set
End Property
Public Property PaymentDate() As Date
Get
Return m_PaymentDate
End Get
Set(value As Date)
m_PaymentDate = value
End Set
End Property
'Create a function BalanceDue to return the BalanceDue
Public Function GetBalanceDue() As Decimal
Return m_InvoiceTotal - m_PaymentTotal - m_CreditTotal
End Function
End Class
Imports System.Data.SqlClient
Public Class InvoicesDB
Public Shared Function FindInvoiceByID(ByVal TermsID) As List(Of Invoices)
Dim invoice As New Invoices
Dim connection As SqlConnection = PayablesDB.GetConnection
Dim invoiceList As New List(Of Invoices)
Dim selectStatement As String = "SELECT InvoiceID, VendorID, InvoiceNumber, InvoiceDate, InvoiceTotal,PaymentTotal,CreditTotal,TermsID,DueDate,PaymentDate FROM Invoices WHERE TermsID=#TermsID"
Dim selectCommand As New SqlCommand(selectStatement, connection)
'add the parameter to the parameter collection of the command object
selectCommand.Parameters.AddWithValue("#TermsID", TermsID)
Try
connection.Open()
Dim reader As SqlDataReader = selectCommand.ExecuteReader
If reader.Read Then
invoice.InvoiceID = CInt(reader("InvoiceID"))
invoice.VendorID = CInt(reader("VendorID"))
invoice.InvoiceNumber = CInt(reader("InvoiceNumber"))
invoice.InvoiceDate = CDate(reader("InvoiceDate"))
invoice.InvoiceTotal = CDec(reader("InvoiceTotal"))
invoice.PaymentTotal = CDec(reader("PaymentTotal"))
invoice.CreditTotal = CDec(reader("CreditTotal"))
invoice.TermsID = CInt(reader("TermsID"))
invoice.DueDate = CDate(reader("DueDate"))
invoice.PaymentDate = CDate(reader("PaymentDate"))
Else
'that means the invoice is not found
invoice = Nothing 'this means the vendor object no longer exists
End If
reader.Close()
connection.Close()
Catch ex As Exception
Throw ex
End Try
Return invoiceList
End Function
Public Shared Function GetBalanceDue() As Decimal 'aggregate
Dim connection As SqlConnection = PayablesDB.GetConnection
Dim selectCommand As New SqlCommand()
selectCommand.Connection = connection
selectCommand.CommandText =
"SELECT SUM(InvoiceTotal - PaymentTotal - CreditTotal) " &
"AS BalanceDue FROM Invoices" &
"WHERE TermsID=#TermsID"
connection.Open()
Dim balanceDue As Decimal = CDec(selectCommand.ExecuteScalar)
connection.Close()
Return balanceDue
End Function
End Class
Public Class Terms
Dim m_TermsID As Integer
Dim m_Description As String
Dim m_DueDays As Integer
Public Sub New()
End Sub
Public Property TermsID() As Integer
Get
Return m_TermsID
End Get
Set(ByVal value As Integer)
m_TermsID = value
End Set
End Property
Public Property Description() As String
Get
Return m_Description
End Get
Set(ByVal value As String)
m_Description = value
End Set
End Property
Public Property DueDays() As Integer
Get
Return m_DueDays
End Get
Set(ByVal value As Integer)
m_DueDays = value
End Set
End Property
End Class
Imports System.Data.SqlClient
Public Class TermsDB
Public Shared Function GetTermsList() As List(Of Terms)
Dim termList As New List(Of Terms)
Dim connection As SqlConnection = PayablesDB.GetConnection
Dim selectStatement As String =
"SELECT TermsID,Description,DueDays " &
"FROM Terms " &
"ORDER BY Description"
Dim selectCommand As New SqlCommand(selectStatement, connection)
Try
connection.Open()
Dim reader As SqlDataReader = selectCommand.ExecuteReader()
Dim term As Terms
Do While reader.Read
term = New Terms
term.TermsID = CInt(reader("TermsID"))
term.Description = reader("Description").ToString
term.DueDays = CInt(reader("DueDays"))
termList.Add(term)
Loop
reader.Close()
Catch ex As SqlException
Throw ex
Finally
connection.Close()
End Try
Return termList
End Function
End Class
I think your button is triggering the page load event, and you are losing the selected combobox value.
Do you have a text value as the first option in the combobox? That would cause the conversion error you're seeing.
Avoid binding the combobox on postback with something like
If ispostback = false Then
LoadComboBoxes()
End If

Inserting item into list causes item to be deleted

I currently am trying to insert a score into its proper position. The scores add to their proper position.
However when I add or insert to the contained list it does not expand past its deserialized size. (Note this highscore class is serialized to XML after the AddNewScore function is called. Also it is deserialized when the program is loaded)
The issue only occurs after deserialization.
Here is my serialization code (its contained in a serialization class that manages saving and loading all data types I use)
Public Sub LoadHighScores()
_highScores = DirectCast(LoadXml(highScoreFilePath, highScoreBackupFilePath, _highScoreFileCorrupted, GetType(HighScores)), HighScores)
If (_highScores.scores.Count < 5) Then
_highScores.AddNewScore(New Score("Steve", 9000))
_highScores.AddNewScore(New Score("John", 8000))
_highScores.AddNewScore(New Score("Paul", 7000))
_highScores.AddNewScore(New Score("Alex", 6000))
_highScores.AddNewScore(New Score("Joe", 5000))
SaveHighScores()
End If
End Sub
Private Sub SaveHighScores()
If (File.Exists(highScoreFilePath) And Not _highScoreFileCorrupted) Then
File.Copy(highScoreFilePath, highScoreBackupFilePath, True) 'Backup quiz data
End If
Using FileStream As FileStream = New FileStream(highScoreFilePath, FileMode.Create)
'Dim encryptionStream As CryptoStream = New CryptoStream(FileStream, AESCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
Dim serializer As XmlSerializer = New XmlSerializer(GetType(HighScores))
serializer.Serialize(FileStream, _highScores)
_highScoreFileCorrupted = False
'encryptionStream.Close()
End Using
End Sub
Public Function LoadXml(ByVal filePath As String, ByVal backupFilePath As String, ByRef fileCorrupt As Boolean, ByVal dataType As Type) As Object
Dim returnValue As Object = Activator.CreateInstance(dataType)
If (File.Exists(filePath) Or File.Exists(backupFilePath)) Then
Using fileStream As FileStream = New FileStream(filePath, FileMode.OpenOrCreate)
Using backupFileStream As FileStream = New FileStream(backupFilePath, FileMode.OpenOrCreate)
Dim serializer As XmlSerializer = New XmlSerializer(dataType)
Try
returnValue = serializer.Deserialize(fileStream)
Catch ex As Exception
fileCorrupt = True
Try
returnValue = serializer.Deserialize(backupFileStream)
Catch e As Exception
MessageBox.Show("Backup Corrupted", "Loading error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Try
End Using
End Using
End If
Return returnValue
End Function
Here is my high score class
Imports System.Collections.Generic
Imports System.Xml
Public Class HighScores
Public scores As List(Of Score) = New List(Of Score)
Public userplays As List(Of UserGameLog) = New List(Of UserGameLog)
'Public userPlays As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
Sub New()
End Sub
Private Sub UpdatePlays(ByVal Username As String)
For Each userlog As UserGameLog In userplays
If (userlog.username = Username) Then
userlog.numberOfPlays += 1
Return
End If
Next
userplays.Add(New UserGameLog(Username))
End Sub
Public Function GetNumberOfPlays(ByVal Username As String) As Integer
For Each userlog As UserGameLog In userplays
If (userlog.username = Username) Then
Return userlog.numberOfPlays
End If
Next
Return 0
End Function
Public Function GetHighScores() As List(Of Score)
Return scores
End Function
Public Sub AddNewScore(ByVal score As Score)
UpdatePlays(score.username)
If (scores.Count > 0) Then
For index = 0 To scores.Count - 1
If (scores(index).score < score.score) Then
scores.Insert(index, score)
Exit Sub
End If
Next
End If
scores.Add(score)
End Sub
End Class
Solved my issue, on loading the highscore list i was pulling out the 5 highest scores. Instead of using range of I was assigning a new list the score list and removing the unused values to trim it down to 5 with
Public Function GetHighScores(ByVal maxScoreCount As Integer) As List(Of Score)
Dim scores As List(Of Score) = New List(Of Score)
scores = _highScores.scores
If (scores.Count > maxScoreCount) Then
scores.RemoveRange(maxScoreCount - 1, scores.Count - maxScoreCount)
End If
Return scores
End Function
This was a reference to the prior list and it was cutting out my values. Swapped it out with GetRange and problem is fixed. smacks head

Set SqlParameter in VB.NET?

I'm new with classes and I want to create a SqlCommandManager class and I can't figure out how to pass SqlParameter on my class.
For example if I want to insert data I would just use my class like client below.
'Client
Dim m_SqlComManager as new SQLCommandManager("MyConnectionString")
m_SqlCommandManager.Commandtext = "INSERT INTO [TableName]([Field1],[Field2])VALUES(#Field1,Field2);"
m_SqlCommandManager.Parameters.AddWithValue("#Field1","SomeValue1")
m_SqlCommandManager.Parameters.AddWithValue("#Field2","SomeValue2")
m_SqlCommandManager.ExecuteNonQuery()
'Here is my class
Imports System.Data.SqlClient
Public Class SQLCommandManager
Private m_SqlParameters As SqlParameter()
Private m_Commandtext As String
Private m_ConStr As String
Public WriteOnly Property SQlParameter() As SqlParameter()
Set(ByVal value As SqlParameter())
value = m_SqlParameters
End Set
End Property
Public Property CommandText() As String
Get
Return m_Commandtext
End Get
Set(ByVal value As String)
value = m_Commandtext
End Set
End Property
Public Sub New(ByVal con As String)
m_ConStr = con
End Sub
Public Sub ExecuteNonQuery()
Using con As New SqlConnection(m_ConStr)
Using com As New SqlCommand
com.Connection = con
com.CommandText = m_Commandtext
'Please help
'How can i insert parameter here from client..
If con.State = ConnectionState.Closed Then
con.Open()
End If
com.ExecuteNonQuery()
End Using
End Using
End Sub
End Class
How how can I set the parameters before the ExecuteNonQuery method?
Thanks in advance..
I would do something like this:
Public Class SqlCommandManager
Private m_SqlParameters As List(Of SqlParameter)
Private m_Commandtext As String
Private m_ConStr As String
Public Sub New()
m_SqlParameters = New List(Of SqlParameter)()
End Sub
Public ReadOnly Property SqlParameters() As List(Of SqlParameter)
Get
Return m_SqlParameters
End Get
End Property
Public Property CommandText() As String
Get
Return m_Commandtext
End Get
Set
value = m_Commandtext
End Set
End Property
Public Sub New(con As String)
m_ConStr = con
End Sub
Public Sub ExecuteNonQuery()
Using con As New SqlConnection(m_ConStr)
Using com As New SqlCommand(m_Commandtext, con)
com.Parameters.AddRange(m_SqlParameters.ToArray())
con.Open()
com.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
End Class
What I've changed:
Changed the class name to SqlCommandManager to be in line with Microsoft's recommendations (don't capitalize more than 2 letters in an abbreviation; IO is fine, Sql and Xml should not be all capitalized)
I would use a List(Of SqlParameter) rather than an array - much easier to deal with, much easier to add additional parameters to it
I prefer to pass the CommandText and the SqlConnection right into the constructor of the SqlCommand - that way,you definitely never forget these two vital bits of information!
Just before your .ExecuteQuery, add the parameters defined in your list to the parameter array of the SqlCommand using a single call to .AddRange()

How to get product by its ID?

Database:Microsoft Access Created Settings called ConnStr with value
provider=Microsoft.ACE.OLEDB.12.0; Data Source=c:\sm.accdb Product
table has 4 columns:ProductID, Description, Category, Price I have 3
classes:Manager, Product, ProductManager
Public MustInherit Class Manager
Private _connectionString As String
Protected Property ConnectionString() As String
Get
Return _connectionString
End Get
Set(ByVal value As String)
_connectionString = value
End Set
End Property
Public Sub New(ByVal connStr As String)
ConnectionString = connStr
End Sub
End Class
Public Class Product
Private _id As Integer
Public Property ID() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Private _description As String
Public Property Description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
Private _category As String
Public Property Category() As String
Get
Return _category
End Get
Set(ByVal value As String)
_category = value
End Set
End Property
Private _price As Double
Public Property Price() As Double
Get
Return _price
End Get
Set(ByVal value As Double)
_price = value
End Set
End Property
End Class
Imports System.Data.OleDb
Public Class ProductManager
Inherits Manager
Public Function GetProductByID(ByVal id As Integer) As Product
Dim con = New System.Data.OleDb.OleDbConnection
Dim sql As String = "SELECT * FROM Product WHERE ProductID=#id"
con.Open()
Try
Dim description As String
Dim category As String
Dim price As Double
Dim cmd As New System.Data.OleDb.OleDbCommand(sql, con)
cmd.Parameters.Add(id.ToString, "#id")
cmd.Parameters.Add(description, "#description")
cmd.Parameters.Add(category, "#category")
cmd.Parameters.Add(price.ToString, "#price")
cmd.ExecuteNonQuery()
cmd.Dispose()
cmd = Nothing
Catch ex As Exception
Throw New Exception(ex.ToString(), ex)
Finally
con.Close()
End Try
Return nothing
End Function
End Class
I could not get product from database! I think problem is in ProductManager Class! I am so confused! Please help me!
You must use cmd.ExecuteReader() method
Dim myReader As OledbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While myReader.Read()
Console.WriteLine(myReader.GetString(0))
End While
myReader.Close()