VB.Net Silverlight 5 SQL Entities Searching - sql

Im making a silverlight 5 application and im using RIA WCS services to connect to sql, i can add data,delete data, edit data, and get all data, but the problem is that i need to retrieve a specific record not the whole entity, when i try the following code just nothing happens:
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
' InitializeComponent()
objctx = New BanksDomainContext
Dim itemType = Branch_NameComboBox.SelectedItem.GetType
Dim pi = itemType.GetProperty(Branch_NameComboBox.DisplayMemberPath)
Dim cbi = pi.GetValue(Branch_NameComboBox.SelectedItem, Nothing).ToString()
Dim BranchName As String = cbi
' Dim query As EntityQuery(Of Branches) = objctx.GetBranchesDetailsQuery(BranchName)
' Dim loadOp As LoadOperation(Of Branches) = Me.objctx.Load(query)
' DataGrid1.ItemsSource = loadOp.Entities
' objctx.Load(query, LoadData, Nothing)
Dim loadOp = Me.objctx.Load(Me.objctx.GetBranchesDetailsQuery(BranchName))
LoadData(loadOp)
End Sub
Private Sub LoadData(lo As LoadOperation)
For Each br As Branches In lo.Entities
AddressTextBlock.Text = br.Address
CoordinatesTextBlock.Text = br.Coordinates
ManagerTextBlock.Text = br.Manager
PhoneTextBlock.Text = br.Phone
FaxTextBlock.Text = br.Fax
Next
End Sub
can someone guide me on how to do it?

The following is the solution to load text boxes from SQL records by selecting the search term from a list box:
Private Sub Branch_NameComboBox_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles Branch_NameComboBox.SelectionChanged
m_PushpinLayer.Children.Clear()
objctx = New BanksDomain
Dim itemType = Branch_NameComboBox.SelectedItem.[GetType]()
Dim pi = itemType.GetProperty(Branch_NameComboBox.DisplayMemberPath)
Dim cbi = pi.GetValue(Branch_NameComboBox.SelectedItem, Nothing).ToString()
Dim BranchName As String = cbi
Dim itemType2 = NameComboBox.SelectedItem.[GetType]()
Dim pi2 = itemType2.GetProperty(NameComboBox.DisplayMemberPath)
Dim cbi2 = pi2.GetValue(NameComboBox.SelectedItem, Nothing).ToString()
Dim BankName As String = cbi2
Dim bb As EntityQuery(Of Branches) = From b In objctx.GetBranchesDetailsQuery(BranchName) Where b.Bank = BankName Select b
Dim res As LoadOperation(Of Branches) = objctx.Load(bb, New Action(Of LoadOperation(Of Branches))(AddressOf GetBeansCompleted), True)
End Sub
Private Sub GetBeansCompleted(args As LoadOperation(Of Branches))
For Each bc As Branches In args.Entities
'
Dim Latitude As Double = bc.Lat
Dim Longitude As Double = bc.Lon
Dim CoO As Location = New Location
CoO.Latitude = Decimal.Parse(Latitude)
CoO.Longitude = Decimal.Parse(Longitude)
Dim BName As String = bc.Branch_Name.ToString
Dim MKColor As Color = GetThisColor(bc.MapKeyColor.ToString)
AddPushPin(BName, CoO, MKColor)
AddressTextBlock.Text = (bc.Address.ToString)
LatTextBlock.Text = (Decimal.Parse(Latitude))
LonTextBlock.Text = (Convert.ToDecimal(Longitude))
ManagerTextBlock.Text = (bc.Manager.ToString)
PhoneTextBlock.Text = (bc.Phone.ToString)
FaxTextBlock.Text = (bc.Fax.ToString)
Next
End Sub

Related

vb.net threading sub slower than call sub

I know this Question was already posted, but it was never really answered, or I did not understand the answer :)
My Problem is that when I call this Sub within the Thread, it takes about 4 sec to execute the code, but if I make a simple Call Sub (Like commented in Code) it takes about 450 ms.
I have a simple program with 2 Subs:
To report Progress on Main Form
To do some "For" Looping
Imports System.Data.SqlClient
Public Class Form1
Public Delegate Sub ProzentDelegate(ByVal Prozent As Double)
Dim G_I_Temp As Integer = 0
Dim G_S_Prüfziffer As String
Dim G_S_Präfix As String
Dim G_I_Zähler As Integer
Dim G_I_Stellen As Integer
Dim G_D_Step As Integer = 1
Dim G_I_Position As Integer
Dim DT_G_Prüftabelle As DataTable
Dim DR_G_Prüftabelle As DataRow
Dim thisLock As New Object
Dim conn As String = "Data Source=SR-SQLWVS;Initial Catalog=Barcode;Integrated Security=True"
Dim sourceconn As New SqlConnection(conn)
Dim adap As SqlDataAdapter
Dim cmd As SqlCommand = New SqlCommand("SELECT TOP(10) * FROM dbo.Prüf_Tabelle", sourceconn)
Dim Thrd_preview As Threading.Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sourceconn.Open()
cmd.Connection = sourceconn
End Sub
Public Sub ReportProgress(ByVal Prozent As Double)
ProgressBar1.Value = Prozent
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RB_Thread.Checked Then
If Thrd_preview IsNot Nothing Then Thrd_preview = Nothing
Thrd_preview = New Threading.Thread(AddressOf DoSomeWork)
Thrd_preview.IsBackground = True
Thrd_preview.Start()
Else
DoSomeWork()
End If
End Sub
Private Sub DoSomeWork()
Dim ds As New DataSet
DT_G_Prüftabelle = ds.Tables.Add("Prüf_Tabelle")
Dim L_I_Counter, L_I_Counter2 As Integer
Dim L_I_Ende As Integer
Dim L_I_Step As Integer
Dim L_I_Nutzen As Integer = 1
L_I_Step = 1
L_I_Ende = Val(TB_Auftrag_neu_Anzahl.Text)
G_I_Stellen = 8
G_S_Prüfziffer = "1"
G_S_Präfix = "00"
If TB_Info_Increment.Text = "" Then TB_Info_Increment.Text = 1
adap = New SqlDataAdapter("SELECT TOP(10) * FROM dbo.Prüf_Tabelle", sourceconn)
adap.FillSchema(DT_G_Prüftabelle, SchemaType.Mapped)
DT_G_Prüftabelle.Rows.Clear()
Dim pre As Date = Now
For L_I_Counter = 1 To (L_I_Ende * L_I_Step) Step L_I_Step
For L_I_Counter2 = 1 To L_I_Nutzen
DR_G_Prüftabelle = DT_G_Prüftabelle.NewRow
DR_G_Prüftabelle.Item("Auftrag_Lfd_nr") = TB_KeyLot.Text
DR_G_Prüftabelle.Item("Position") = G_I_Position
G_I_Position += G_D_Step
DR_G_Prüftabelle.Item("Barcode_soll") = F_Berechnung((Trim(Str(Val(TB_Auftrag_neu_Von.Text) + L_I_Counter - 1))).PadLeft(G_I_Stellen, "0"))
DT_G_Prüftabelle.Rows.Add(DR_G_Prüftabelle)
If ProgressBar1.InvokeRequired Then
ProgressBar1.BeginInvoke(New ProzentDelegate(AddressOf ReportProgress), (L_I_Counter * 100) / (L_I_Ende * L_I_Step))
Else
ReportProgress((L_I_Counter * 100) / (L_I_Ende * L_I_Step))
End If
Next
Next
MsgBox(Now.Subtract(pre).TotalMilliseconds.ToString & " ms.")
End Sub
Function F_Berechnung(ByVal L_Nummer As String) As String
Dim L_B_Gerade As Boolean = False
Dim L_I_Prüfziffer As Integer
Dim L_I_Stellen As Integer
Me.G_I_Temp = 0
Select Case G_S_Prüfziffer
Case "0"
F_Berechnung = L_Nummer
Case "1"
F_Berechnung = G_S_Präfix & Trim(L_Nummer)
Case "2"
F_Berechnung = G_S_Präfix & Trim(L_Nummer)
Case "3"
F_Berechnung = L_Nummer
'
Case "5" '-----Mod 10 gewichtung 31...-----
L_Nummer = G_S_Präfix & Trim(L_Nummer)
L_I_Stellen = Len(L_Nummer)
For Me.G_I_Zähler = L_I_Stellen To 1 Step -1
Me.G_I_Temp += Val(Mid(L_Nummer, Me.G_I_Zähler, 1)) * IIf(L_B_Gerade, 1, 3)
L_B_Gerade = Not L_B_Gerade
Next
L_I_Prüfziffer = IIf(10 - Me.G_I_Temp Mod 10 = 10, 0, 10 - Me.G_I_Temp Mod 10)
F_Berechnung = Trim(L_Nummer) & Trim(Str(L_I_Prüfziffer))
'
Case "6" '-----Mod 10 gewichtung 13...-----
L_Nummer = G_S_Präfix & Trim(L_Nummer)
L_I_Stellen = Len(L_Nummer)
For Me.G_I_Zähler = L_I_Stellen To 1 Step -1
Me.G_I_Temp += Val(Mid(L_Nummer, Me.G_I_Zähler, 1)) * IIf(L_B_Gerade, 3, 1)
L_B_Gerade = Not L_B_Gerade
Next
L_I_Prüfziffer = IIf(10 - Me.G_I_Temp Mod 10 = 10, 0, 10 - Me.G_I_Temp Mod 10)
F_Berechnung = Trim(L_Nummer) & Trim(Str(L_I_Prüfziffer))
'
Case Else
F_Berechnung = L_Nummer
End Select
End Function
I have tried to take out Progress Reporting but the Result is the same.
I hope you have some suggestions or solutions for me. I would be very thankful.
Give these changes a try
Change this
If ProgressBar1.InvokeRequired Then
ProgressBar1.Invoke(New ProzentDelegate(AddressOf ReportProgress), Progr)
Else
ReportProgress(Progr)
End If
to
ReportProgress(Progr)
And change method ReportProgress to
Public Sub ReportProgress(ByVal Prozent As Double)
Me.BeginInvoke(Sub()
ProgressBar1.Value = Prozent
End Sub)
End Sub
See if that makes a difference. The check for InvokeRequired is not needed, the answer is always yes.
Just wanted to report that I found a solution.
The main problem was this line of code in:
DR_G_Prüftabelle.Item("Auftrag_Lfd_nr") = TB_KeyLot.Text
So if I start this Sub from another Thread rather than from a Main Thread, for every single loop it has to go to main thread and to take Paramater from TextBox and to go back in Thread to Loop further.
I took that out, declared a variable at the begining and assigned this TextBox value to it and it works like charm.
Also I took a suggestion from dbasnett to Invoke Reporting every time without If-Else and also the suggestion from Steven Doggart to Report every 100 Loops.
You guys helped a lot. Thanks for that!!

treeview disappeared when i reopen the form

I would like to ask for your help. I am stuck to this for more than two days, I've searched the net but unfortunately got no answer.
I have a program in vb 2008 and a database (SQL Server 2008). I have this form which contains treeview. The items display is selected from the database. When i run the program and open the form the treeview items displayed (at first), but when i closed the form and try to open it again the treeview disappear. I dont know why :( . Why is it disappearing? Can somebody help me please. Thank you.
Below is my code....
#form_load
Private Sub frmProfile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call conecDB()
Call initCMD()
FillTable()
CreateTree() 'create the treeview node
findNode() 'find and checked the selected nodes that was given to the profile
End Sub
'the functions
Private Sub FillTable()
'tv1.Nodes.Clear()
dtable.Columns.Add("ID", GetType(Integer))
dtable.Columns.Add("NAME", GetType(String))
dtable.Columns.Add("PARENT", GetType(Integer))
dtable.Columns.Add("LEVEL", GetType(Integer))
qSQL = "select mod_id,name,parent,level,sort,mnu_name from module where status='A' and parent!=-1"
With comDB
.CommandText = qSQL
rdDB = .ExecuteReader
End With
Do While rdDB.Read
dtable.Rows.Add(rdDB!mod_id, rdDB!name.ToString(), rdDB!parent)
My.Application.DoEvents()
Loop
For i = 0 To dtable.Rows.Count - 1
Dim ID1 As String = dtable.Rows(i).Item("ID").ToString
dtable.Rows(i).Item("LEVEL") = FindLevel(ID1, 0)
Next
rdDB.Close()
End Sub
Private Function FindLevel(ByVal ID As String, ByRef Level As Integer) As Integer
For i = 0 To dtable.Rows.Count - 1
Dim ID1 As String = dtable.Rows(i).Item("ID").ToString
Dim Parent1 As String = dtable.Rows(i).Item("PARENT").ToString
If ID = ID1 Then
If Parent1 = 0 Then
Return Level
Else
Level += 1
FindLevel(Parent1, Level)
End If
End If
Next
Return Level
End Function
Private Sub CreateTree()
tv1.Nodes.Clear()
Dim MaxLevel1 As Integer = CInt(dtable.Compute("MAX(LEVEL)", ""))
Dim i, j As Integer
For i = 0 To MaxLevel1
Dim Rows1() As DataRow = dtable.Select("LEVEL = " & i)
For j = 0 To Rows1.Count - 1
Dim ID1 As String = Rows1(j).Item("ID").ToString
Dim Name1 As String = Rows1(j).Item("NAME").ToString
'Dim mName As String = Rows1(j).Item("mNAME").ToString
Dim Parent1 As String = Rows1(j).Item("PARENT").ToString
If Parent1 = 0 Then
tv1.Nodes.Add(ID1, Name1)
Else
Dim TreeNodes1() As TreeNode = tv1.Nodes.Find(Parent1, True)
If TreeNodes1.Length > 0 Then
TreeNodes1(0).Nodes.Add(ID1, Name1)
End If
End If
Next
Next
End Sub
Private Sub findNode()
Dim rName As String = String.Empty
Dim b As Boolean = True
qSQL = "select access_id,mnu_name from profile_details where prof_id=" & lblPID.Text & ""
With comDB
.CommandText = qSQL
rdDB = .ExecuteReader
End With
Do While rdDB.Read
rName = rdDB!access_id.ToString()
Try
Dim arr As TreeNode() = tv1.Nodes.Find(rName, b)
For i = 0 To arr.Length - 1
tv1.SelectedNode = arr(i)
tv1.SelectedNode.Checked = True
Next
Catch
MsgBox(Err)
End Try
Loop
rdDB.Close()
End Sub

Update old data and add new data using tableadapter in vb.net

My code should update old records and at the same time if the a new record is found it should likewise insert it in the DB... I am using table adpater in doing this method.
Here is the code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim pta As New PHDSTableAdapters.productdatabaseTableAdapter
pta.Updateproduct(TextBox1.Text, ComboBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text)
pta.Fill(myds.productdatabase)
Dim lta As New PHDSTableAdapters.lotnoTableAdapter
Dim lt = lta.GetDataBylotno(TextBox5.Text)
Dim l As phaccess.PHDS.lotnoRow = lt.Rows(0)
Dim i As Integer
For i = 0 To DGV.Rows.Count - 1
For Each l In myds.lotno
Dim lot As String = DGV.Rows(i).Cells(1).Value
Dim del As Date = DGV.Rows(i).Cells(2).Value
Dim exp As Date = DGV.Rows(i).Cells(3).Value
Dim quantity As Integer = DGV.Rows(i).Cells(4).Value
Dim sup = DGV.Rows(i).Cells(5).Value
Dim disc = DGV.Rows(i).Cells(6).Value
If l.productid = TextBox5.Text Then
Dim lotnumber As String = l.lotnumber
If l.lotnumber <> lot Then
'the error occurs in the insert statement as it would create duplicates 'of the index...the index of the table is the lot number
lta.Insert(TextBox5.Text, lot, del, exp, quantity, sup, disc)
Else
lta.Updateedit(del, exp, quantity, sup, disc, lot)
lta.Fill(myds.lotno)
End If
End If
If lot = "" Then
closeform()
lta.Fill(myds.lotno)
Button3.Enabled = False
Button1.Visible = True
Button3.Visible = False
Button1.Enabled = False
Exit Sub
End If
Next
Next
End Sub
If you need anything else to help me solve this please do ask.
thank you
you can use "DataTable" to upadate DataGridView in your table
For displaying data:
Dim sql As String = "SELECT * FROM table_name"
sCommand = New SqlCommand(sql, conn)
sAdapter = New SqlDataAdapter(sCommand)
sBuilder = New SqlCommandBuilder(sAdapter)
sDs = New DataSet()
sAdapter.Fill(sDs, "table_name")
sTable = sDs.Tables("table_name")
DataGridView1.DataSource = sTable
for updating:
sAdapter.Update(sTable)
i hope this helps

Barcode parsing

For example I have this form:
As you can see I have Barcode textbox, and 6 more textboxes. What I need is a code that will parse my barcode from the textbox and fill in the remaining fields. The parenthesis ie (), and the numbers inside it, have to be deleted.
Here is example of my barcode
(1)CODE1(3)NAME(4)SURNAME(8)CODE2(10)CODE3(12)CODE4
For now I have this code:
Private Sub Barcode_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Barcode.Leave
Dim Duzina As String = Me.Barcode.Text.Length
Dim I As Integer = 0
Dim Slog As String = ""
Dim Rec As String = Me.Barcode.Text
For I = 4 To Duzina
If Rec.Substring(I, 1) = "(" Then
Me.Surname.Text = Slog
Exit For
End If
Slog = Slog + Rec.Substring(I, 0)
Next
End Sub
So when the barcode loads into the first textbox, it should fill up all the other textboxes.
My simple solution
Private Sub Barcode_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Barcode.Leave
Dim codes = barcodeTextBox.Text.Split("("c)
Dim code1 = codes(1).Split(")"c)
Dim name = codes(2).Split(")"c)
Dim surename = codes(3).Split(")"c)
Dim code2 = codes(4).Split(")"c)
Dim code3 = codes(5).Split(")"c)
Dim code4 = codes(6).Split(")"c)
sureNameTextBox.Text = surename(0).ToString()
nameTextBox.Text = name(0).ToString()
code1TextBox.Text = code1(0).ToString()
code2TextBox.Text = code2(0).ToString()
code3TextBox.Text = code3(0).ToString()
code4TextBox.Text = code4(0).ToString()
End Sub
Alternative solution
Private Sub Barcode_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Barcode.Leave
Dim codes = barcodeTextBox.Text.Split("("c)
Dim code1 = codes(1).Split(")"c).FirstOrDefault()
Dim name = codes(2).Split(")"c).FirstOrDefault()
Dim surename = codes(3).Split(")"c).FirstOrDefault()
Dim code2 = codes(4).Split(")"c).FirstOrDefault()
Dim code3 = codes(5).Split(")"c).FirstOrDefault()
Dim code4 = codes(6).Split(")"c).FirstOrDefault()
sureNameTextBox.Text = surename
nameTextBox.Text = name
code1TextBox.Text = code1
code2TextBox.Text = code2
code3TextBox.Text = code3
code4TextBox.Text = code4
End Sub
I have done this in this way
Private Function CitanjeBarkoda() As Boolean
Dim Duzina As String = Me.TextBox8.Text.Length
Dim I As Integer = 0
Dim Slog As String = ""
Dim Rec As String = Me.TextBox8.Text
Dim BrOz As Integer = 0
Dim BrZz As Integer = 0
Dim NizOz As New ArrayList
Dim NizZz As New ArrayList
Dim Slog1 As String = ""
For I = 0 To Duzina - 1
If Rec.Substring(I, 1) = "(" Then
BrOz = BrOz + 1
NizOz.Add(I)
End If
If Rec.Substring(I, 1) = ")" Then
BrZz = BrZz + 1
NizZz.Add(I)
End If
Next
Me.TextBox10.Text = Rec.Substring(NizZz(0) + 1, NizOz(1) - NizZz(0) - 1)
Me.IMEOSOBE = Rec.Substring(NizZz(1) + 1, NizOz(2) - NizZz(1) - 1)
Me.PREZIMEOSOBE = Rec.Substring(NizZz(2) + 1, NizOz(3) - NizZz(2) - 1)
Me.TextBox3.Text = Rec.Substring(NizZz(3) + 1, NizOz(4) - NizZz(3) - 1)
Me.BRKART = Rec.Substring(NizZz(4) + 1, NizOz(5) - NizZz(4) - 1)
Me.TextBox5.Text = Rec.Substring(NizZz(5) + 1, NizOz(6) - NizZz(5) - 1)
return true
End function
Actually, the title is a little misleading. It should be string parsing and not barcode parsing. Anyway, you can try this.
Private Sub Barcode_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Barcode.Leave
Dim pvsBarcodeRAW As String = Trim(Me.Barcode.Text.Length)
Dim voNVC As New System.Collections.Specialized.NameValueCollection
Dim vasCodePairs() As String = Split(pvsBarcodeRAW, "(")
For Each vsCodePair As String In vasCodePairs
vsCodePair = Trim(vsCodePair)
If vsCodePair.Length > 0 Then
Dim vasCodePair() As String = Split(vsCodePair, ")")
If vasCodePair.Length = 2 Then
Select Case vasCodePair(0)
Case 1
voNVC("Code1") = vasCodePair(1)
Case 3
voNVC("Name") = vasCodePair(1)
Case 4
voNVC("SurName") = vasCodePair(1)
Case 8
voNVC("Code2") = vasCodePair(1)
Case 10
voNVC("Code3") = vasCodePair(1)
Case 12
voNVC("Code4") = vasCodePair(1)
End Select
End If
End If
Next
'Now you can do whatever you want with the variables in the voNVC variable.
SurName.Text = voNVC("SurName")
End Sub
Put $I in a Code 39 barcode with keymapping enabled on the scanner. $I will act like a TAB key on the keyboard.
One scan of
CODE1$INAME$ISURNAME$ICODE2$ICODE3$ICODE4
Will populate this:
CODE1
NAME
SURNAME
CODE2
CODE3
CODE4
With that said, Code 39 is not good for long barcodes. You could use QR, but tabs are a bit more complex there, still doable though. You would also need a 2D scanner.

The select command property has not been initialized before calling 'Fill'

I am working on a Windows forms project that connects to a Microsoft Access database, reads the the file, does some math and then provides some basic statistics back. Right now I am teaching myself VB and I know the code below could be more efficient. However, right now I am just trying to make it functional.
The program filters the data it needs via sql, and there are several sql statements. I separated the code for each of the sql statements and into a subroutine so that I could call each one when the form loads and also when the user clicks a button to update. The program works fine on the form load, however, when you click the update button you get the following error on the 'odaCalls.Fill' in subroutine Count(): "The select command property has not been initialized before calling 'Fill'.
Any help is greatly appreciated. I have searched on google and tried the suggestions found there but have not found a fix.
Option Explicit On
Public Class Form1
'Count() Variables
Dim strSQL = "SELECT * FROM tblcallLog"
Dim strPath As String = "Provider=Microsoft.ACE.OLEDB.12.0 ;" _
& "Data Source=C:\callLogRev2_be.accdb"
Dim odaCalls As New OleDb.OleDbDataAdapter(strSQL, strPath)
Dim datCallCount As New DataTable
Dim intCount As Integer = 0
'LiveCalls() variables
Dim strSQLLive As String = "SELECT * FROM tblcallLog WHERE callLive=True"
Dim odaCallsLive As New OleDb.OleDbDataAdapter(strSQLLive, strPath)
Dim datCallLive As New DataTable
Dim intCallLiveCount As Integer = 0
Dim decCallLivePct As Decimal
'TransferCalls() variables
Dim strSQLTransfered As String = _
"SELECT * FROM tblcallLog WHERE callName LIKE '% transfer %' OR callName LIKE 'transfer%'"
Dim odaCallsTransfered As New OleDb.OleDbDataAdapter(strSQLTransfered, strPath)
Dim datCallTransfered As New DataTable
Dim intCallTransfered As Integer = 0
Dim decCallTranfered As Decimal
'SingleStaffCall() Variables
Dim strSQLSingleStaff As String = _
"SELECT * FROM tblcallLog WHERE callName LIKE '% single %' OR callName LIKE 'single%'"
Dim odaCallSingleStaff As New OleDb.OleDbDataAdapter(strSQLSingleStaff, strPath)
Dim datCallSingleStaff As New DataTable
Dim intCallSingleStaff As Integer = 0
Dim decCallSingleStaff As Decimal
'SingleStaffCallsLive() Variables
Dim strSQLSingleStaffLive As String = _
"SELECT * FROM tblcallLog WHERE callName LIKE '% single %' OR callName LIKE 'single%' AND callLive=True"
Dim odaCallSingleStaffLive As New OleDb.OleDbDataAdapter(strSQLSingleStaffLive, strPath)
Dim datCallSingleStaffLive As New DataTable
Dim intCallSingleStaffLive As Integer = 0
Dim decCallSingleStaffLive As Decimal
'CallToday() Variables
Dim strSQLToday As String = _
"SELECT * FROM tblcallLog WHERE startDate = date()"
Dim odaCallToday As New OleDb.OleDbDataAdapter(strSQLToday, strPath)
Dim datCallToday As New DataTable
Dim intCallToday As New Integer
'CallTodayLive() Variables
Dim strSQLTodayLiveCalls As String = _
"SELECT * FROM tblcallLog WHERE callLive=TRUE AND startDate = date()"
Dim odaCallTodayLive As New OleDb.OleDbDataAdapter(strSQLTodayLiveCalls, strPath)
Dim datCallTodayLive As New DataTable
Dim intCallTodayLive As New Integer
Dim decCallTodayLive As New Decimal
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Count()
LiveCalls()
TransferCalls()
SingleStaffCalls()
SingleStaffCallsLive()
CallToday()
CallTodayLive()
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
'Checks the database for updates when user pushes the update button on the static data tab.
Count()
LiveCalls()
TransferCalls()
SingleStaffCalls()
SingleStaffCallsLive()
CallToday()
CallTodayLive()
End Sub
Private Sub Count()
'database connect and call count
odaCalls.Fill(datCallCount)
odaCalls.Dispose()
intCount = datCallCount.Rows.Count
lblTotalCallsData.Text = intCount.ToString
lblTotalCallsData.Visible = True
End Sub
Private Sub LiveCalls()
'determine number of live calls
odaCallsLive.Fill(datCallLive)
odaCallsLive.Dispose()
intCallLiveCount = datCallLive.Rows.Count
lblcallsLiveData.Text = intCallLiveCount.ToString
lblcallsLiveData.Visible = True
decCallLivePct = intCallLiveCount / intCount
lblPctCallsLiveData.Text = decCallLivePct.ToString("P")
lblPctCallsLiveData.Visible = True
End Sub
Private Sub TransferCalls()
'determine the number of transfer calls
odaCallsTransfered.Fill(datCallTransfered)
odaCallsTransfered.Dispose()
intCallTransfered = datCallTransfered.Rows.Count
lblTotalTransferCallsData.Text = intCallTransfered.ToString
lblTotalTransferCallsData.Visible = True
decCallTranfered = intCallTransfered / intCount
lblPctTransferCallsData.Text = decCallTranfered.ToString("P")
lblPctTransferCallsData.Visible = True
End Sub
Private Sub SingleStaffCalls()
'determine the number of single staff calls and percentage of total
odaCallSingleStaff.Fill(datCallSingleStaff)
odaCallSingleStaff.Dispose()
intCallSingleStaff = datCallSingleStaff.Rows.Count
lblTotalSingleStaffCallData.Text = intCallSingleStaff.ToString
lblTotalSingleStaffCallData.Visible = True
decCallSingleStaff = intCallSingleStaff / intCount
lblPctSingleStaffCallsData.Text = decCallSingleStaff.ToString("P")
End Sub
Private Sub SingleStaffCallsLive()
'determine the percentage of single staff calls taken live
odaCallSingleStaffLive.Fill(datCallSingleStaffLive)
odaCallSingleStaffLive.Dispose()
intCallSingleStaffLive = datCallSingleStaffLive.Rows.Count
decCallSingleStaffLive = intCallSingleStaffLive / intCount
lblPctSingleStaffCallsLiveData.Visible = True
lblPctSingleStaffCallsLiveData.Text = decCallSingleStaffLive.ToString("P")
End Sub
Private Sub CallToday()
'determine values for todays date
odaCallToday.Fill(datCallToday)
odaCallToday.Dispose()
intCallToday = datCallToday.Rows.Count
lblTotalCallsTodayData.Text = intCallToday
lblTotalCallsTodayData.Visible = True
End Sub
Private Sub CallTodayLive()
'determine the number of live calls today
odaCallTodayLive.Fill(datCallTodayLive)
odaCallTodayLive.Dispose()
intCallTodayLive = datCallTodayLive.Rows.Count
lblCallsTodayLiveData.Text = intCallTodayLive.ToString
lblCallsTodayLiveData.Visible = True
'Statement to error correct so the database doesn't force the program to divide by zero
Try
decCallTodayLive = intCallTodayLive / intCallToday
Catch Exception As DivideByZeroException
lblPctCallsTodayLiveData.Text = "0.00%"
lblPctCallsTodayLiveData.Visible = True
Catch Exception As OverflowException
decCallTodayLive = 0
End Try
lblPctCallsTodayLiveData.Text = decCallTodayLive.ToString("P")
lblPctCallsTodayLiveData.Visible = True
End Sub
End Class
The problem is that you are disposing the dataadapters immediately after filling them.
This is why it works on form load, but not after. It would be better practice to create the and destroy the dataadapters in the methods where they are used instead of creating them on spec at the form level.