How do I insert millions of records with speed and at once into an sql table using vb.net? - sql

I have am filling gridview with data and insert record 1 by 1 into an sql table using a stored procedure which has only insert statement.
This is ok but when records are in millions then this takes a considerable time. I want to insert in speed into the table and should not take much time.
Try
con.ConnectionString = v_ObjConn
Dim ParamName(8), ParamType(8), ParamValue(8) As String
ParamName(0) = "#WOCODE" : ParamType(0) = SqlDbType.VarChar
ParamName(1) = "#WODESC" : ParamType(1) = SqlDbType.VarChar
ParamName(2) = "#WOTYPE" : ParamType(2) = SqlDbType.VarChar
ParamName(3) = "#TAGNUMBER" : ParamType(3) = SqlDbType.VarChar
ParamName(4) = "#PMCODE" : ParamType(4) = SqlDbType.VarChar
ParamName(5) = "#STATUS" : ParamType(5) = SqlDbType.VarChar
ParamName(6) = "#COMPLETEDDATE" : ParamType(6) = SqlDbType.Date
ParamName(7) = "#LASTUPDATE" : ParamType(7) = SqlDbType.Date
lblWaitingMsg.Text = "Importing records into the VAIL-Plant. Please wait..."
For Each row As GridViewRow In gv_InforWO.Rows
Dim chkBoxSelect As CheckBox = CType(row.FindControl("chkSelect"), CheckBox)
If chkBoxSelect.Checked = True Then
ParamValue(0) = row.Cells(1).Text.ToString().Replace("&", "and").Replace(" ", String.Empty)
ParamValue(1) = row.Cells(2).Text.ToString().Replace("&", "and").Replace(" ", String.Empty)
ParamValue(2) = row.Cells(3).Text.ToString().Replace("&", "and").Replace(" ", String.Empty)
ParamValue(3) = row.Cells(4).Text.ToString().Replace("&", "and").Replace(" ", String.Empty)
ParamValue(4) = row.Cells(5).Text.ToString().Replace("&", "and").Replace(" ", String.Empty)
ParamValue(5) = row.Cells(6).Text.ToString().Replace("&", "and").Replace(" ", String.Empty)
ParamValue(6) = Convert.ToDateTime(row.Cells(7).Text)
ParamValue(7) = Convert.ToDateTime(row.Cells(8).Text)
Func.SP_ExecDML(con, "[dbo].[SP_InsertWorkOrders_InforEAM]", ParamName, ParamType, ParamValue)
End If
Next
MsgBox("Records have been imported/updated.")
BindGridInfoWO()
Catch ex As Exception
MsgBox(ex.Message)
Finally
lblWaitingMsg.Text = ""
End Try

Related

Program not responding fast enough

To get started I created a program at work to read our network tools and store the retrieved data to our database. No problem but the way I set it up originally was every tool had its own instance and was hard coded for each tool.
Now I was tasked with making the program more user friendly so I can send it to other places. So now I database the IP address of tool and location on assembly line so I can cross reference while the code is executing.
On the new program I created an array of my Tool Reader class:
Dim TB(50) As ReadAtlascopco
Then I run through this loop on form load:
Private dict As New Dictionary(Of Timer, Label)()
Private Sub CreateTimers()
For i = 0 To IPCount
TB(i) = New ReadAtlascopco
' timer(i) = New Timerarray
Next
For i As Integer = 1 To IPCount
C = i - 1
timer = New Timer() With {.Interval = 250, .Enabled = True, .Tag = i.ToString}
AddHandler timer.Tick, (AddressOf timer_Tick)
' timer.Interval = (100 * i)
label = New Label()
label.Name = "label" & i
label.Location = New Point(10, 10 + i * 25)
label.Font = New Font("Sans Serif", 9, FontStyle.Bold)
label.AutoSize = True
'label.Width = 100
'label.AutoEllipsis = False
label.TabIndex = i
label.Visible = True
Me.Controls.Add(label)
dict(timer) = label
TB(C).ipaddress = TBData(C)
TB(C).StartConnection()
timer.Enabled = True
timer.Start()
Next
Then I go through the timer tick event. Everything works great up until this point. Once I go through the tick event it starts lagging and I miss data.
Is there a way to speed this process up?
Here is the tick event:
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
Dim Stop_Num(50) As String
Dim Line_Name(50) As String
Dim t As Timer = DirectCast(sender, Timer)
Dim s As Integer = Val(t.Tag) - 1
Param1 = TBData(s)
CWE(s) = False
If Activestat(s) = True Then
Dim SQL1 As String = "SELECT Stop_Num FROM TBL_Added_Boxes Where(IP_Address = N'" + Param1 + "')"
Dim output1 As String
Using cn = New SqlConnection(My.Settings.Torque_InfoConnectionString)
Using cmd = New SqlCommand(SQL1, cn)
cn.Open()
Try
Dim dr1 = cmd.ExecuteReader()
While dr1.Read()
output1 = dr1("Stop_Num").ToString()
End While
Catch ex As SqlException
WriteToErrorLog("Error pulling data from the database in reguards to torque box info (Stop Number).", ex.ToString(), "Failed to retreive Torque controler Info .")
End Try
cn.Close()
End Using
End Using
Stop_Num(0) = output1
Dim SQL2 As String = "SELECT Line_On FROM TBL_Added_Boxes Where(IP_Address = N'" + Param1 + "')"
Dim output2 As String
Using cn = New SqlConnection(My.Settings.Torque_InfoConnectionString)
Using cmd = New SqlCommand(SQL2, cn)
cn.Open()
Try
Dim dr2 = cmd.ExecuteReader()
While dr2.Read()
output2 = dr2("Line_On").ToString()
End While
Catch ex As SqlException
WriteToErrorLog("Error pulling data from the database in reguards to torque box info (Line On).", ex.ToString(), "Failed to retreive Torque controler Info .")
End Try
cn.Close()
End Using
End Using
Line_Name(0) = output2
If Line_Name(0) = "Line 1" Then
Newlinename(s) = "Line1Serial"
ElseIf Line_Name(0) = "Line 2" Then
Newlinename(s) = "Line2Serial"
ElseIf Line_Name(0) = "Line 3" Then
Newlinename(s) = "Line3Serial"
End If
Param3 = Stop_Num(0)
Param2 = Newlinename(s)
Dim SQL As String = "SELECT " + Newlinename(s) + " FROM TBL_RunningLineStatus Where(Stop_Number = N'" + Stop_Num(0) + "')"
Dim output As String
Using cn = New SqlConnection(My.Settings.ScanBypassConnectionString)
Using cmd = New SqlCommand(SQL, cn)
cn.Open()
Try
Dim dr = cmd.ExecuteReader()
While dr.Read()
output = dr(Param2).ToString()
End While
Catch ex As SqlException
WriteToErrorLog("Error Pulling Data From The Database In reguards to Seat Data.", ex.ToString(), "Failed to retreive buildline data.")
End Try
cn.Close()
End Using
End Using
TBSData(s) = output
If Killall = "True" Then
t.Stop()
Else
Try
TB(s).GetData()
If TB(s).Colorselect = 0 Then
dict(t).BackColor = System.Drawing.Color.GreenYellow
dict(t).ForeColor = Color.Black
ElseIf TB(s).Colorselect = 1 Then
TB(s).ipaddress = TBData(s)
TB(s).StartConnection()
dict(t).BackColor = System.Drawing.Color.Red
dict(t).ForeColor = Color.White
ElseIf TB(s).Colorselect = 2 Then
dict(t).BackColor = System.Drawing.Color.Purple
dict(t).ForeColor = Color.White
End If
If Olddata(s) <> TB(s).TorboxData(20) Then
Cname(s) = TB(s).TorboxData(2)
Tstatus(s) = TB(s).TorboxData(7)
TValue(s) = TB(s).TorboxData(19)
Sdata(s) = TBSData(s)
Tfinal(s) = TB(s).TorboxData(18)
TPset(s) = TB(s).TorboxData(30)
TFinalAngle(s) = TB(s).TorboxData(24)
RDAmin(s) = TB(s).TorboxData(25)
RDAmax(s) = TB(s).TorboxData(26)
RDAactual(s) = TB(s).TorboxData(27)
RDAStat(s) = TB(s).TorboxData(15)
Anglemin(s) = TB(s).TorboxData(21)
Anglemax(s) = TB(s).TorboxData(22)
AngleFT(s) = TB(s).TorboxData(23)
Anglestat(s) = TB(s).TorboxData(13)
Tormin(s) = TB(s).TorboxData(16)
Tormax(s) = TB(s).TorboxData(17)
TTTights(s) = TB(s).TorboxData(28)
TTSerial(s) = TB(s).TorboxData(29)
Olddata(s) = TB(s).TorboxData(20)
CWE(s) = True
dict(t).Text = t.Tag + " ) " + "Controler Name : " + TB(s).TorboxData(2) + ": Status : " + TB(s).Connectiontext + " : Date : " + Now() + " :: SERIAL # : " + Sdata(s) + " : From : " + Newlinename(s) + " :: Stop : " + Param3 + ""
' .Text = t.Tag + " ) " + "Controler Name : " + TB(s).TorboxData(2) + ": Status : " + TB(s).Connectiontext + " : Date : " + Now() + " :: SERIAL # : " + Sdata(s) + " : From : " + Newlinename(s) + " :: Stop : " + Param3 + "")
dict(t).BackColor = System.Drawing.Color.Yellow
Else
If TB(s).networkconnected = True Then
dict(t).AutoSize = True
'dict(t).Text = "Controler Name : " + TB(s).TorboxData(2) + ": Status : " + TB(s).Connectiontext + " : Date : " + Now()
dict(t).BackColor = System.Drawing.Color.Yellow
dict(t).ForeColor = Color.Black
Else
dict(t).Text = TB(s).Connectiontext + " :::: " + Now() + " :: " + TBData(s)
dict(t).BackColor = System.Drawing.Color.Red
dict(t).ForeColor = Color.White
End If
End If
Catch ex As Exception
WriteToErrorLog("Error Reading the torque box .", ex.Message, "Failed read torque data.")
Finally
'If CWE(s) = True Then
' Call Data_Entry()
'End If
If TValue(s) <> "" And CWE(s) = True Then
Try
con.ConnectionString = My.Settings.ScanBypassConnectionString
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO TBL_Torque_Value1 (Serial_Num, Controler_Name, Torque_Status, Torque_Value, Date_Time,Extra_1, Extra_2, Extra_3,RundownMin,RundownMax,RundownAct,RundownStat,AngleMin,AngleMax,AngleFT,AngleStat,TorqueMin,TorqueMax,ToolTightens,Toolserialnum) VALUES(#p1,#p2, #p3, #p4, #p5, #p6, #p7, #p8,#p9, #p10, #p11, #p12, #p13, #p14, #p15, #p16, #p17, #p18, #p19, #p20)"
cmd.Parameters.Add("#p1", SqlDbType.NVarChar, 50)
cmd.Parameters.Add("#p2", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p3", SqlDbType.NVarChar, 50)
cmd.Parameters.Add("#p4", SqlDbType.NVarChar, 50)
cmd.Parameters.Add("#p5", SqlDbType.DateTime2, 7)
cmd.Parameters.Add("#p6", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p7", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p8", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p9", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p10", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p11", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p12", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p13", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p14", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p15", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p16", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p17", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p18", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p19", SqlDbType.NVarChar, 250)
cmd.Parameters.Add("#p20", SqlDbType.NVarChar, 250)
cmd.Parameters("#p1").Value = Sdata(s)
cmd.Parameters("#p2").Value = Cname(s)
cmd.Parameters("#p3").Value = Tstatus(s)
cmd.Parameters("#p4").Value = TValue(s)
cmd.Parameters("#p5").Value = Now()
cmd.Parameters("#p6").Value = Tfinal(s)
cmd.Parameters("#p7").Value = TFinalAngle(s) + " Deg"
cmd.Parameters("#p8").Value = "P:" + TPset(s)
cmd.Parameters("#p9").Value = RDAmin(s)
cmd.Parameters("#p10").Value = RDAmax(s)
cmd.Parameters("#p11").Value = RDAactual(s)
cmd.Parameters("#p12").Value = RDAStat(s)
cmd.Parameters("#p13").Value = Anglemin(s)
cmd.Parameters("#p14").Value = Anglemax(s)
cmd.Parameters("#p15").Value = AngleFT(s)
cmd.Parameters("#p16").Value = Anglestat(s)
cmd.Parameters("#p17").Value = Tormin(s)
cmd.Parameters("#p18").Value = Tormax(s)
cmd.Parameters("#p19").Value = TTTights(s)
cmd.Parameters("#p20").Value = TTSerial(s)
cmd.ExecuteNonQuery()
Catch ex As Exception
WriteToErrorLog("Error Inserting Data into The Database In reguards to Torque Data.", ex.Message, "Failed to Insert Torque Data.")
con.Close()
cmd.Parameters.Clear()
Cname(s) = ""
Tstatus(s) = ""
TValue(s) = ""
Sdata(s) = ""
Tfinal(s) = ""
TFinalAngle(s) = ""
TPset(s) = ""
RDAmin(s) = ""
RDAmax(s) = ""
RDAactual(s) = ""
RDAStat(s) = ""
Anglemin(s) = ""
Anglemax(s) = ""
AngleFT(s) = ""
Anglestat(s) = ""
Tormin(s) = ""
Tormax(s) = ""
TTTights(s) = ""
TTSerial(s) = ""
Finally
con.Close()
cmd.Parameters.Clear()
Cname(s) = ""
Tstatus(s) = ""
TValue(s) = ""
Sdata(s) = ""
Tfinal(s) = ""
TFinalAngle(s) = ""
TPset(s) = ""
RDAmin(s) = ""
RDAmax(s) = ""
RDAactual(s) = ""
RDAStat(s) = ""
Anglemin(s) = ""
Anglemax(s) = ""
AngleFT(s) = ""
Anglestat(s) = ""
Tormin(s) = ""
Tormax(s) = ""
TTTights(s) = ""
TTSerial(s) = ""
End Try
End If
End Try
End If
Else
dict(t).AutoSize = True
dict(t).Text = "Torque Box is not active :: " + TBData(s) + " ::"
dict(t).BackColor = System.Drawing.Color.Black
dict(t).ForeColor = Color.White
End If
End Sub
The class that is being referenced is one that I created and is working but like I said everything is slow. It seems to hang for 3 to 5 seconds in a single tick event.
As of now I only have 21 tools setup and it will take about 15 to 30 seconds to cycle through all.

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

No rows modified when updating records

I have 5 tab pages, with a Datagridview in each pages. Each tab represents a table in my database. The problem is, when updating, only the first table is being updated. Here's my code:
Private Sub update_Click(sender As Object, e As EventArgs) Handles updateBtn.Click
'/// UPDATING TABLE - employee
Try
Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
conn.Open()
Dim command As New OleDbCommand("UPDATE employee SET [Last_Name] = #lname, [First_Name]= #fname, MI = #mi, Bday = #bday, Age = #age, [Nationality] = #natio, [Contact_Number] = #contact, Sex = #gender, Address = #address, Department = #department, [Position] = #position, TIN = #tin, SSS = #sss, Pagibig = #pagibig, PhilHealth = #phh, Termi_Resi = #termiresi, [Date_hired] = #datehired, [Rate_Month] = #rm, [Date_End] = #dateTermiResi, Status = #status, [Father_Name] = #father, [Mother_Name] = #mother, Civil = #civil, Spouse = #spouse, [Number_of_Children] = #numberchild, Child1 = #child1, Child2 = #child2, Child3 = #child3, Child4 = #child4, Child5 = #child5, Child6 = #child6, Child7 = #child7, Child8 = #child8, Child9 = #child9 WHERE [EmployID] = #numberemp", conn)
With command.Parameters
.AddWithValue("#lname", lname.Text)
.AddWithValue("#fname", fname.Text)
.AddWithValue("#mi", mi.Text)
.AddWithValue("#bday", bday.Value)
.AddWithValue("#age", age.Text)
.AddWithValue("#natio", natio.Text)
.AddWithValue("#contact", contact.Text)
.AddWithValue("#gender", gender.Text)
.AddWithValue("#address", address.Text)
.AddWithValue("#department", department.Text)
.AddWithValue("#position", position.Text)
.AddWithValue("#tim", tin.Text)
.AddWithValue("#sss", sss.Text)
.AddWithValue("#pagibig", pagibig.Text)
.AddWithValue("#phh", phh.Text)
.AddWithValue("#termiresi", termiresi.Text)
.AddWithValue("#datehired", datehired.Value)
.AddWithValue("#rm", rm.Text)
.AddWithValue("#dateTermiResi", dateTermiResi.Value)
.AddWithValue("#status", status.Text)
.AddWithValue("#father", father.Text)
.AddWithValue("#mother", mother.Text)
.AddWithValue("#civil", civil.Text)
.AddWithValue("#spouse", spouse.Text)
.AddWithValue("#numberchild", numberchild.Text)
.AddWithValue("#child1", child1.Text)
.AddWithValue("#child2", child2.Text)
.AddWithValue("#child3", child3.Text)
.AddWithValue("#child4", child4.Text)
.AddWithValue("#child5", child5.Text)
.AddWithValue("#child6", child6.Text)
.AddWithValue("#child7", child7.Text)
.AddWithValue("#child8", child8.Text)
.AddWithValue("#child9", child9.Text)
.AddWithValue("#numberemp", numberemp.Text)
End With
command.ExecuteNonQuery()
MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
command.Dispose()
conn.Close()
clearall()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
'/// UPDATING TABLE - education
Try
Using conn2 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
conn2.Open()
Dim command2 As New OleDbCommand("UPDATE education SET Level_Educ = #hlevelEduc, Degree = #degree, Post_Degree = #postdegree, Elem = #elem, Year_one = #year1, Highschool = #highschool, Year_two = #year2, College = #college, Year_three = #year3, Training_School = #trainingS, Year_four = #year4, Other_Educ = #otherEduc WHERE [EmployID] = #numberemp1", conn2)
numberemp1.Text = numberemp.Text
With command2.Parameters
.AddWithValue("#hlevelEduc", hlevelEduc.Text)
.AddWithValue("#elem", elem.Text)
.AddWithValue("#year1", year1.Text)
.AddWithValue("#highschool", highschool.Text)
.AddWithValue("#year2", year2.Text)
.AddWithValue("#college", college.Text)
.AddWithValue("#year3", year3.Text)
.AddWithValue("#degree", degree.Text)
.AddWithValue("#postdegree", postdegree.Text)
.AddWithValue("#trainingS", trainingS.Text)
.AddWithValue("#year4", year4.Text)
.AddWithValue("#otherEduc", otherEduc.Text)
.AddWithValue("#numberemp1", numberemp1.Text)
End With
Dim rows = command2.ExecuteNonQuery()
If rows = 0 Then
MessageBox.Show("No rows modified")
Else
MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
command2.Dispose()
conn2.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
'/// UPDATING TABLE - employment
Try
Using conn3 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
conn3.Open()
Dim command3 As New OleDbCommand("UPDATE employment SET Cname1 = #cname1, Posi1 = #posi1, Dept1 = #dept1, Tm1 = #tm1, Reason1 = #reason1, Cname2 = #cname2, Posi2 = #posi2, Dept2 = #dept2, Tm2 = #tm2, Reason2 = #reason2, Cname3 = #cname3, Posi3 = #posi3, Dept3 = #dept3, Tm3 = #tm3, Reason3 = #reason3, Cname4 = #cname4, Posi4 = #posi4, Dept4 = #dept4, Tm4 = #tm4, Reason4 = #reason4, Cname5 = #cname5, Posi5 = #posi5, Dept5 = #dept5, Tm5 = #tm5, Reason5 = #reason5, Cname6 = #cname6, Posi6 = #posi6, Dept6 = #dept6, Tm6 = #tm6, Reason6 = #reason6 WHERE [EmployID] = #numberemp2", conn3)
numberemp2.Text = numberemp.Text
With command3.Parameters
.AddWithValue("#cname1", cname1.Text)
.AddWithValue("#posi1", posi1.Text)
.AddWithValue("#dept1", dept1.Text)
.AddWithValue("#tm1", tm1.Text)
.AddWithValue("#reason1", reason1.Text)
.AddWithValue("#cname2", cname2.Text)
.AddWithValue("#posi2", posi2.Text)
.AddWithValue("#dept2", dept2.Text)
.AddWithValue("#tm2", tm2.Text)
.AddWithValue("#reason2", reason2.Text)
.AddWithValue("#cname3", cname3.Text)
.AddWithValue("#posi3", posi3.Text)
.AddWithValue("#dept3", dept3.Text)
.AddWithValue("#tm3", tm3.Text)
.AddWithValue("#reason3", reason3.Text)
.AddWithValue("#cname4", cname4.Text)
.AddWithValue("#posi4", posi4.Text)
.AddWithValue("#dept4", dept4.Text)
.AddWithValue("#tm4", tm4.Text)
.AddWithValue("#reason4", reason4.Text)
.AddWithValue("#cname5", cname5.Text)
.AddWithValue("#posi5", posi5.Text)
.AddWithValue("#dept5", dept5.Text)
.AddWithValue("#tm5", tm5.Text)
.AddWithValue("#reason5", reason5.Text)
.AddWithValue("#cname6", cname6.Text)
.AddWithValue("#posi6", posi6.Text)
.AddWithValue("#dept6", dept6.Text)
.AddWithValue("#tm6", tm6.Text)
.AddWithValue("#reason6", reason6.Text)
.AddWithValue("#numberemp2", numberemp2.Text)
End With
Dim rows = command3.ExecuteNonQuery()
If rows = 0 Then
MessageBox.Show("No rows modified")
Else
MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
command3.Dispose()
conn3.Close()
clearall()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
'/// UPDATING TABLE - disciplinary
Try
Using conn4 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
conn4.Open()
Dim command4 As New OleDbCommand("UPDATE disciplinary SET Offense1 = #o1, Action1 = #ao1, Dateoff1 = #do1, Offense2 = #o2, Action2 = #ao2, Dateoff2 = #do2, Offense3 = #o3, Action3 = #ao3, Dateoff3 = #do3, Offense4 = #o4, Action4 = #ao4, Dateoff4 = #do4, Offense5 = #o5, Action5 = #ao5, Dateoff5 = #do5, Offense6 = #o6, Action6 = #ao6, Dateoff6 = #do6, Offense7 = #o7, Action7 = #ao7, Dateoff7 = #do7, Offense8 = #o8, Action8 = #ao8, Dateoff8 = #do8, Offense9 = #o9, Action9 = #ao9, Dateoff9 = #do9, Offense10 = #10, Action10 = #ao10, Dateoff10 = #do10 WHERE [EmployID] = #numberemp3 ", conn4)
numberemp3.Text = numberemp.Text
With command4.Parameters
.AddWithValue("#o1", o1.Text)
.AddWithValue("#ao1", ao1.Text)
.AddWithValue("#do1", do1.Value)
.AddWithValue("#o2", o2.Text)
.AddWithValue("#ao2", ao2.Text)
.AddWithValue("#do2", do2.Value)
.AddWithValue("#o3", o3.Text)
.AddWithValue("ao3", ao3.Text)
.AddWithValue("#do3", do3.Value)
.AddWithValue("#o4", o4.Text)
.AddWithValue("#ao4", ao4.Text)
.AddWithValue("#do4", do4.Value)
.AddWithValue("#o5", o5.Text)
.AddWithValue("#ao5", ao5.Text)
.AddWithValue("#do5", do5.Value)
.AddWithValue("#o6", o6.Text)
.AddWithValue("#ao6", ao6.Text)
.AddWithValue("#do6", do6.Value)
.AddWithValue("#o7", o7.Text)
.AddWithValue("#ao7", ao7.Text)
.AddWithValue("#do7", do7.Value)
.AddWithValue("#o8", o8.Text)
.AddWithValue("#ao8", ao8.Text)
.AddWithValue("#do8", do8.Value)
.AddWithValue("#o9", o9.Text)
.AddWithValue("#ao9", ao9.Text)
.AddWithValue("#do9", do9.Value)
.AddWithValue("#o10", o10.Text)
.AddWithValue("#ao10", ao10.Text)
.AddWithValue("#do10", do10.Value)
.AddWithValue("#numberemp3", numberemp3.Text)
End With
Dim rows = command4.ExecuteNonQuery()
If rows = 0 Then
MessageBox.Show("No rows modified")
Else
MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
conn4.Close()
clearall()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
'/// UPDATING TABLE - trainings
Try
Using conn5 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Employee.mdb;")
conn5.Open()
Dim command5 As New OleDbCommand("UPDATE trainings SET ts1 = #ts1, dts1 = #dts1, ts2 = #ts2, dts2 = #dts2, ts3 = #ts3, dts3 = #dts3, ts4 = #ts4, dts4 = #dts4, ts5 = #ts5, dts5 = #dts5, ts6 = #ts6, dts6 = #dts6, ts7 = #ts7, dts7 = #dts7, ts8 = #ts8, dts8 = #dts8, ts9 = #ts9, dts9 = #dts9, ts10 = #ts10, dts10 = #dts10, ts11 = #ts11, dts11 = #dts11, ts12 = #ts12, dts12 = #dts12, ts13 = #ts13, dts13 = #dts13, ts14 = #ts14, dts14 = #dts14, ts15 = #ts15, dts15 = #dts15, ts16 = #ts16, dts16 = #dts16, ts17 = #ts17, dts17 = #dts17, ts18 = #ts18, dts18 = #dts18, ts19 = #ts19, dts19 = #dts19, ts20 = #ts20, dts20 = #dts20, ts21 = #ts21, dts21 = #dts21, ts22 = #ts22, dts22 = #dts22, ts23 = #ts23, dts23 = #dts23, ts24 = #ts24, dts24 = #dts24 WHERE [EmployID] = #numberemp4 ", conn5)
numberemp4.Text = numberemp.Text
With command5.Parameters
.AddWithValue("#ts1", ts1.Text)
.AddWithValue("#dts1", dts1.Value)
.AddWithValue("#ts2", ts2.Text)
.AddWithValue("#dts2", dts2.Value)
.AddWithValue("#ts3", ts3.Text)
.AddWithValue("#dts3", dts3.Value)
.AddWithValue("#ts4", ts4.Text)
.AddWithValue("#dts4", dts4.Value)
.AddWithValue("#ts5", ts5.Text)
.AddWithValue("#dts5", dts5.Value)
.AddWithValue("#ts6", ts6.Text)
.AddWithValue("#dts6", dts6.Value)
.AddWithValue("#ts7", ts7.Text)
.AddWithValue("#dts7", dts7.Value)
.AddWithValue("#ts8", ts8.Text)
.AddWithValue("#dts8", dts8.Value)
.AddWithValue("#ts9", ts9.Text)
.AddWithValue("#dts9", dts9.Value)
.AddWithValue("#ts10", ts10.Text)
.AddWithValue("#dts10", dts10.Value)
.AddWithValue("#ts11", ts11.Text)
.AddWithValue("#dts11", dts11.Value)
.AddWithValue("#ts12", ts12.Text)
.AddWithValue("#dts12", dts12.Value)
.AddWithValue("#ts13", ts13.Text)
.AddWithValue("#dts13", dts13.Value)
.AddWithValue("#ts14", ts14.Text)
.AddWithValue("#dts14", dts14.Value)
.AddWithValue("#ts15", ts15.Text)
.AddWithValue("#dts15", dts15.Value)
.AddWithValue("#ts16", ts16.Text)
.AddWithValue("#dts16", dts16.Value)
.AddWithValue("#ts17", ts17.Text)
.AddWithValue("#dts17", dts17.Value)
.AddWithValue("#ts18", ts18.Text)
.AddWithValue("#dts18", dts18.Value)
.AddWithValue("#ts19", ts19.Text)
.AddWithValue("#dts19", dts19.Value)
.AddWithValue("#ts20", ts20.Text)
.AddWithValue("#dts20", dts20.Value)
.AddWithValue("#ts21", ts21.Text)
.AddWithValue("#dts21", dts21.Value)
.AddWithValue("#ts22", ts22.Text)
.AddWithValue("#dts22", dts22.Value)
.AddWithValue("#ts23", ts23.Text)
.AddWithValue("#dts23", dts23.Value)
.AddWithValue("#ts24", ts24.Text)
.AddWithValue("#dts24", dts24.Value)
.AddWithValue("#numberemp4", numberemp4.Text)
End With
Dim rows = command5.ExecuteNonQuery()
If rows = 0 Then
MessageBox.Show("No rows modified")
Else
MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
command5.Dispose()
conn5.Close()
disableall()
clearall()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Add1.Visible = True
btnReg.Visible = True
updateBtn.Visible = False
RefreshDGV()
End Sub
I always get
No rows modified
when updating the second up to the fifth table.
Here's my form to make it clear if it is too confusing..
Assuming that you want to load data to tab pages when needed:
Use the Load event to load data for Tap Page 1
hook up to event Selecting of TabControl to load data for the other pages
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e) {
if (e.TabPage.Name == "TabPage1") {
//Load Data for Tab page 1
DataTable dt = new DataTable();
dt.Load(reader);
grid2.DataSource = dt;
}
}

commandtext property has not been initialized vb.net

I am new to vb.net and tried to execute the code its working fine but when I am scheduling the code in another server in network by running the exe file only, then I am getting the error as:
"commandtext property has not been initialized"
Sub send_SMS()
Console.WriteLine("Begining of the send_SMS() ")
Dim dt As New DataTable
Dim st1, sql As String
Dim fetcheduser, fetchedpno As String
Dim J As Integer
J = Now.Hour
Console.WriteLine("J Now.Hour =: " + J.ToString)
'A shift
If J >= 14 And J < 22 Then
sql = " SELECT * FROM YKPI_VALUE "
sql = sql & " WHERE KPV_USER_ID='QEXP' "
sql = sql & " AND KPV_TYP_ID='A' "
sql = sql & " AND KPV_FROM_DATE=TO_CHAR(SYSDATE,'YYYYMMDD') "
Console.WriteLine("J = 14 ")
st.Text = "A"
End If
'B Shift
If J >= 22 And J < 6 Then
sql = " SELECT * FROM YKPI_VALUE "
sql = sql & " WHERE KPV_USER_ID='QEXP' "
sql = sql & " AND KPV_TYP_ID='B' "
sql = sql & " AND KPV_FROM_DATE=TO_CHAR(SYSDATE,'YYYYMMDD') "
Console.WriteLine("J = 22 ")
st.Text = "B"
End If
'C Shift
If J >= 6 And J < 14 Then
sql = " SELECT * FROM YKPI_VALUE "
sql = sql & " WHERE KPV_USER_ID='QEXP' "
sql = sql & " AND KPV_TYP_ID='C' "
sql = sql & " AND KPV_FROM_DATE=TO_CHAR(SYSDATE-1,'YYYYMMDD') "
Console.WriteLine("J = 6 ")
st.Text = "C"
DAT = DAT.AddDays(-1)
End If
Dim DA As New OracleDataAdapter(sql, oraconn)
DA.Fill(dt)
Console.WriteLine("Before dt.Rows count ")
Dim MaxRows, MaxColums, inc As Integer
MaxRows = dt.Rows.Count
MaxColums = dt.Columns.Count
Dim multiarray(,) As String = New String(MaxRows, MaxColums) {}
Console.WriteLine("Value of MaxRows = " + MaxRows.ToString)
Console.WriteLine("Value of MaxColums = " + MaxColums.ToString)
If (inc <> MaxRows) Then
'inc = inc + 1
Console.WriteLine("Inside dt.Rows and Before for loop ")
For inc = 0 To MaxRows - 1
'For col = 0 To MaxColums
dt.Rows(inc).Item(10) = dt.Rows(inc).Item(10).ToString.Replace("Q", "")
multiarray(inc, 1) = dt.Rows(inc).Item(2).ToString()
multiarray(inc, 2) = dt.Rows(inc).Item(4).ToString()
multiarray(inc, 3) = dt.Rows(inc).Item(10).ToString()
Next
End If
Dim queryusr As String
queryusr = ""
Dim sql_CMD_TD As New OracleCommand(sql, oraconn)
If Not oraconn.State = ConnectionState.Open Then
oraconn.Open()
End If
Dim doNotSendSMS = False
Dim sqldr As OracleDataReader = sql_CMD_TD.ExecuteReader()
If sqldr.HasRows Then
oraconn.Close()
queryusr = " SELECT TSU_USER,TSU_USERNAME FROM T_SDM_USERS "
queryusr = queryusr & "WHERE TSU_SCREENID='QESMS'AND TSU_VALIDITY='Y' "
dt = New DataTable
DA = New OracleDataAdapter(queryusr, oraconn)
DA.Fill(dt)
If Not doNotSendSMS Then
Dim ora_CMD_SMS As New OracleCommand(queryusr, oraconn)
If oraconn.State = ConnectionState.Closed Then
oraconn.Open()
End If
st1 = st.Text
Dim ORA_DR As OracleDataReader = ora_CMD_SMS.ExecuteReader()
If ORA_DR.HasRows Then
While ORA_DR.Read()
fetcheduser = ORA_DR("TSU_USERNAME")
fetchedpno = ORA_DR("TSU_USER")
Console.WriteLine("Before insertIntoSMS function call ")
Console.WriteLine(" Value of st1 :" + st1.ToString)
insertIntoSMS_hbf(fetcheduser, fetchedpno, st1, multiarray, MaxRows, MaxColums)
Console.WriteLine("Outside of insertIntoSMS function call ")
End While
ORA_DR.Close()
End If
If oraconn.State = ConnectionState.Open Then
oraconn.Close()
End If
End If
End If
End Sub

Automatic chart update with new data entry

My chart loads data from a DataGridView.
I want to automatically update my chart with new data if new values are inserted into the DataGridView.
My chart is bound to table1 and table2 in my DataGridView which gets values from a DataTable. Here is a small portion of the code:
Dim myData As New DataTable
wcAdapter.SelectCommand = wcCommand
wcAdapter.Fill(myData)
-
Chart1.DataSource = myData
Chart1.Series("Series1").ValueMemberX = "table1"
Chart1.Series("Series1").ValueMembersY = "table2"
Here is the complete code:
Try
wcconn.Open()
Dim wcCommand As New MySqlCommand()
''telesales name
' Dim wcQuery = "SELECT ID, Telesales, SUBSTRING(lastupdatedate, 1, 10) as 'Day', SUBSTRING(lastupdatetime FROM -9 FOR 6) as 'Time' FROM ratingout where Telesales='" & cbTelesales.Text & "' and lastupdatedate= '" & newDate & "' and lastupdatedate is not null and lastupdatetime is not null ORDER BY lastupdatetime ;"
' wcCommand.Connection = wcconn
' wcCommand.CommandText = wcQuery
Dim newDate As String
newDate = dateWorkCheck.Text
newDate = newDate.Replace("/", "-")
Dim y, m, d As String
y = newDate.Substring(6, 4)
m = newDate.Substring(3, 2)
d = newDate.Substring(0, 2)
newDate = y & "-" & m & "-" & d
Dim wcQuery = "SELECT ID, Telesales, lastupdatedate as 'Day', SUBSTRING(lastupdatetime FROM -8 FOR 2) as 'Time' FROM ratingout where Telesales='" & cbTelesales.Text & "' and lastupdatedate= '" & newDate & "' and lastupdatedate is not null and lastupdatetime is not null ORDER BY lastupdatetime ;"
wcCommand.Connection = wcconn
wcCommand.CommandText = wcQuery
Dim wcData As New DataTable
wcAdapter.SelectCommand = wcCommand
wcAdapter.Fill(wcData)
Dim i = 0
If wcData.Rows.Count = 0 Then
wcAdapter.Dispose()
Try
Dim wQuery = "SELECT ID, Telesales, lastupdatedate as 'Day', SUBSTRING(lastupdatetime FROM -8 FOR 2) as 'Time' FROM ratingout where Telesales='" & cbTelesales.Text & "' and lastupdatedate= '" & dateWorkCheck.Text & "' and lastupdatedate is not null and lastupdatetime is not null ORDER BY lastupdatetime ;"
wcCommand.Connection = wcconn
wcCommand.CommandText = wQuery
Dim wData As New DataTable
wcAdapter.SelectCommand = wcCommand
wcAdapter.Fill(wData)
wData.Columns.Add("tt")
wData.Columns.Add("num")
wcData.Columns.Add("tt")
wcData.Columns.Add("num")
'dgvWorkCheck.AutoSizeRowsMode = DataGridViewAutoSizeRowMode.AllCells
Dim dr As DataRow
For Each dr In wData.Rows
If lastV Is Nothing OrElse Not ColumnEqual(lastV, dr("Time")) Then
''check if first value is nothing
If lastV = Nothing Then
lastV = "00"
l = "0"
Else
dr("tt") = lastV
dr("num") = l
'wcData.Tables("ratingout").Rows(I)("ID") = dr("ID")
End If
ListBox1.Items.Add(lastV & " <--> " & l)
lastV = dr("Time")
l = 1
ElseIf lastV Is Nothing OrElse ColumnEqual(lastV, dr("Time")) Then
l += 1
'Dim series1 As New Series()
'series1.Points.Add(l)
End If
For I = I To wData.Rows.Count
If I <> wData.Rows.Count Then
I += 1
If i = wData.Rows.Count Then
dr("tt") = lastV
dr("num") = l
ListBox1.BeginUpdate()
ListBox1.Items.Add(dr("Telesales") & " between[" & lastV & " and 17:00, ] <--> " & l & "[ records ]")
ListBox1.EndUpdate()
End If
GoTo n
Else
MsgBox("last data")
End If
Next
n:
Next
txtRec.Text = wData.Rows.Count
dgvWorkCheck.DataSource = wData
''chart
Dim ChartArea2 As ChartArea = New ChartArea()
Dim Legend2 As Legend = New Legend()
Dim Series2 As Series = New Series()
Dim Chart2 = New Chart()
Me.Controls.Add(Chart2)
ChartArea2.AxisX.LabelStyle.Angle = -90
ChartArea2.AxisX.LabelStyle.Interval = 1
ChartArea2.AxisY.LabelStyle.Angle = -90
ChartArea2.AxisY.LabelStyle.Interval = 5
ChartArea2.Name = "ChartArea2"
Chart2.ChartAreas.Add(ChartArea2)
Legend2.Name = "Legend2"
Chart2.Legends.Add(Legend2)
Chart2.Location = New System.Drawing.Point(12, 113)
Chart2.Name = "Chart2"
Series2.ChartArea = "ChartArea2"
Series2.Legend = "Legend2"
Series2.Name = "Series2"
Chart2.Series.Add(Series2)
Chart2.Size = New System.Drawing.Size(1145, 604)
Chart2.TabIndex = 0
Chart2.Text = "Chart2"
Chart2.Series("Series2").XValueMember = "tt"
Chart2.Series("Series2").YValueMembers = "num"
Chart2.DataSource = dgvWorkCheck.DataSource
Chart2.DataBind()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Exit Try
since the new data is inserted into a database, you will only need to rebind the gridview to it's source in order to display the new incoming data.
You should isolate the code that binds data to your chart in a function and have it called every time a new field gets inserted:
Function FillChartWithData()
Dim myData As New DataTable
wcAdapter.SelectCommand = wcCommand
wcAdapter.Fill(myData)
...
Chart1.Series("Series1").ValueMemberX = "table1"
Chart1.Series("Series1").ValueMembersY = "table2"
End Function
EDIT
I looked at your coded and it seems you're missing the part responsible for inserting new data inside the 'ratingout' table. You should create a function that allows you to insert new data, something along the line of:
Dim insertRating = "INSERT INTO ratingout VALUES (#NewTeleSalesName, #NewDate);"
Dim insertCmd As New MySqlCommand(insertRating, wcconn)
insertCmd.Parameters.Add("#NewTeleSalesName", MySqlDbType.VarChar, 255, "teleSalesName")
insertCmd.Parameters.Add("#NewDate", MySqlDbType.Datetime, 8, New DateTime(2010, 8, 5))
insertCmd.ExecuteNonQuery()
In order to update my chart bargraph named CashChart (which was databound to a BindingSource) I had to do the following:
To clear the chart information,
Clear the bounding source information
And then re-assign the bounding source information: for example:
CashChart.Series(0).Points.Clear()
CashChart.DataSource = ""
CashChart.DataSource = ESTADOINSTANTANEOBindingSource
Before, only my DataTable was updating, but after these commands, I was able to get the bargraph to update with new values in the table.