No rows modified when updating records - vb.net

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;
}
}

Related

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

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

EF query hits database more than one time

maybe is a simple issue but I'm pretty new to EF so:
I have this query:
Dim InvoicesQuery = (From i As Invoice In dbContext.Invoices.AsNoTracking() Where i.InvoiceNumber = -1
Select New InvoicesWithDetails With {
.InvoiceDetails = i.InvoicesDetails,
.InvoiceDetailsUnmerged = i.InvoicesDetailsUnmergeds,
.Examination = Nothing,
.Applicant = Nothing,
.InvoiceNumber = i.InvoiceNumber,
.InvoiceYear = i.InvoiceYear,
.DocCode = i.DocCode,
.CompanyId = i.CompanyId,
.InvoiceDate = i.InvoiceDate,
.NetAmount = i.NetAmount,
.AmountPaid = i.AmountPaid,
.AmountToPay = i.AmountToPay,
.VAT = i.VAT,
.AdditionalTax = i.AdditionalTax,
.Status = i.Status,
.Amount = i.Amount,
.ExaminationId = i.ExaminationId,
.ReceiverName = If(i.OtherReceiverName Is Nothing, i.ReceiverName, i.OtherReceiverName),
.ReceiverAddress = If(i.OtherReceiverAddress Is Nothing, i.ReceiverAddress, i.OtherReceiverAddress),
.ReceiverCity = If(i.OtherReceiverCity Is Nothing, i.ReceiverCity, i.OtherReceiverCity),
.ReceiverTaxCode = If(i.OtherReceiverTaxCode Is Nothing, i.ReceiverTaxCode, i.OtherReceiverTaxCode),
.ReceiverCompanyTaxCode = i.ReceiverCompanyTaxCode,
.ReceiverZipCode = If(i.OtherReceiverZipCode Is Nothing, i.ReceiverZipCode, i.OtherReceiverZipCode),
.IsCreditNote = i.IsCreditNote,
.HasCreditNote = i.HasCreditNote,
.FixedAdditionalAmountOnInvoice = i.FixedAdditionalAmountOnInvoice,
.CashFlowPaymentModeId = If(i.CashFlowPaymentModeId IsNot Nothing, i.CashFlowPaymentModeId.Trim, Nothing),
.AgreementDeductible = i.AgreementDeductible,
.IsPatientInvoiceRecipient = i.IsPatientInvoiceRecipient,
.IsApplicantInvoiceRecipient = i.IsApplicantInvoiceRecipient,
.IsDoctorInvoiceRecipient = i.IsDoctorInvoiceRecipient,
.DoctorId = If(i.DoctorId IsNot Nothing, i.DoctorId, Nothing),
.Doctor = Nothing,
.CreditNoteParentInvoice = i.CreditNoteParentInvoice,
.CreditNoteParentInvoiceYear = i.CreditNoteParentInvoiceYear,
.IsDeferredInvoice = i.IsDeferredInvoice,
.IsExtracted = If(i.IsExtracted IsNot Nothing, i.IsExtracted, False),
.IsExportedToHOST = If(i.IsExportedToHOST IsNot Nothing, i.IsExportedToHOST, False),
.ApplicantId = i.ApplicantId,
.InvoiceHasVAT = i.InvoiceHasVAT,
.VAT_Percentage = i.VAT_Percentage,
.IssuedBy = i.IssuedBy,
.MaskedCounter = i.MaskedCounter,
.DocTypeId = i.DocTypeId,
.OtherReceiverName = i.OtherReceiverName,
.OtherReceiverAddress = i.OtherReceiverAddress,
.OtherReceiverCity = i.OtherReceiverCity,
.OtherReceiverTaxCode = i.OtherReceiverTaxCode,
.OtherReceiverZipCode = i.OtherReceiverZipCode,
.CashFlows = i.CashFlows}).ToList
InvoicesWithDetails is a class that hinerits Invoice; Invoice is a db entity
After this call I loop through the results like this:
For Each i As InvoicesWithDetails In InvoicesQuery.Where(Function(iwd) iwd.CompanyDescription Is Nothing And iwd.CompanyId IsNot Nothing)
i.CompanyDescription = ReturnCompanyDescription(i.CompanyId)
Next
For each cycle in the loop the database is called.
I thought that after the .ToList in the first call I could iterate the results without invoke the database.
What is wrong?
Thanks in advance
Ok, I worked more on that and I tried a different approach:
This is the new query, targeting the Invoice entity:
Dim InvoicesQuery As List(Of Invoice)
InvoicesQuery = (From i As Invoice In dbContext.Invoices.AsNoTracking Where
i.InvoiceDate >= FromDate And i.InvoiceDate <= ToDate).ToList
After that I create a new istance of InvoiceWithDetails and set all the properties like this:
For Each i As Invoice In InvoicesQuery
Dim newInvoiceWithDetails As New InvoicesWithDetails
With newInvoiceWithDetails
.InvoiceDetails = i.InvoicesDetails
.InvoiceDetailsUnmerged = i.InvoicesDetailsUnmergeds
.Examination = Nothing
.Applicant = Nothing
.InvoiceNumber = i.InvoiceNumber
.InvoiceYear = i.InvoiceYear
.DocCode = i.DocCode
.CompanyId = i.CompanyId
.InvoiceDate = i.InvoiceDate
.NetAmount = i.NetAmount
.AmountPaid = i.AmountPaid
.AmountToPay = i.AmountToPay
.VAT = i.VAT
.AdditionalTax = i.AdditionalTax
.Status = i.Status
.Amount = i.Amount
.ExaminationId = i.ExaminationId
.ReceiverName = If(i.OtherReceiverName Is Nothing, i.ReceiverName, i.OtherReceiverName)
.ReceiverAddress = If(i.OtherReceiverAddress Is Nothing, i.ReceiverAddress, i.OtherReceiverAddress)
.ReceiverCity = If(i.OtherReceiverCity Is Nothing, i.ReceiverCity, i.OtherReceiverCity)
.ReceiverTaxCode = If(i.OtherReceiverTaxCode Is Nothing, i.ReceiverTaxCode, i.OtherReceiverTaxCode)
.ReceiverCompanyTaxCode = i.ReceiverCompanyTaxCode
.ReceiverZipCode = If(i.OtherReceiverZipCode Is Nothing, i.ReceiverZipCode, i.OtherReceiverZipCode)
.IsCreditNote = i.IsCreditNote
.HasCreditNote = i.HasCreditNote
.FixedAdditionalAmountOnInvoice = i.FixedAdditionalAmountOnInvoice
.CashFlowPaymentModeId = If(i.CashFlowPaymentModeId IsNot Nothing, i.CashFlowPaymentModeId.Trim, Nothing)
.AgreementDeductible = i.AgreementDeductible
.IsPatientInvoiceRecipient = i.IsPatientInvoiceRecipient
.IsApplicantInvoiceRecipient = i.IsApplicantInvoiceRecipient
.IsDoctorInvoiceRecipient = i.IsDoctorInvoiceRecipient
.DoctorId = If(i.DoctorId IsNot Nothing, i.DoctorId, Nothing)
.Doctor = Nothing
.CreditNoteParentInvoice = i.CreditNoteParentInvoice
.CreditNoteParentInvoiceYear = i.CreditNoteParentInvoiceYear
.IsDeferredInvoice = i.IsDeferredInvoice
.IsExtracted = If(i.IsExtracted IsNot Nothing, i.IsExtracted, False)
.IsExportedToHOST = If(i.IsExportedToHOST IsNot Nothing, i.IsExportedToHOST, False)
.ApplicantId = i.ApplicantId
.InvoiceHasVAT = i.InvoiceHasVAT
.VAT_Percentage = i.VAT_Percentage
.IssuedBy = i.IssuedBy
.MaskedCounter = i.MaskedCounter
.DocTypeId = i.DocTypeId
.OtherReceiverName = i.OtherReceiverName
.OtherReceiverAddress = i.OtherReceiverAddress
.OtherReceiverCity = i.OtherReceiverCity
.OtherReceiverTaxCode = i.OtherReceiverTaxCode
.OtherReceiverZipCode = i.OtherReceiverZipCode
.CashFlows = i.CashFlows
End With
FinalList.Add(newInvoiceWithDetails)
For each cycle and each property value, the database is invoked
Again, what is wrong?

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

how to get picture from database to picture box by using textbox lost focus

Private Sub tunjukrekod()
Dim cn As MySqlConnection = New MySqlConnection()
cn.ConnectionString = ("server=localhost; userid=root; password=; database=payrollsystemdb;")
cn.Open()
cmd.Connection = cn
Try
If TextBoxEmployeeID.Text <> "" Then
dt.Clear()
cmd = New MySqlCommand("select * from employeedetail where EmployeeID='" & TextBoxEmployeeID.Text & "'", cn)
da = New MySqlDataAdapter(cmd)
da.Fill(dt)
TextBoxEmployeeID.Text = dt.Rows(0).Item(0)
TextBoxEmployeeName.Text = dt.Rows(0).Item(1)
TextBoxChineseName.Text = dt.Rows(0).Item(2)
If dt.Rows(0).Item(3) = "Active" Then
RadioButtonActiveStatus.Checked = True
ElseIf dt.Rows(0).Item(3) = "Inactive" Then
RadioButtonInactiveStatus.Checked = True
End If
TextBoxbranchID.Text = dt.Rows(0).Item(4)
TextBoxAccessLevel.Text = dt.Rows(0).Item(5)
RichTextBoxAddress.Text = dt.Rows(0).Item(6)
TextBoxTel1.Text = dt.Rows(0).Item(7)
TextBoxTel2.Text = dt.Rows(0).Item(8)
TextBoxMobile.Text = dt.Rows(0).Item(9)
TextBoxEmail.Text = dt.Rows(0).Item(10)
TextBoxICNew.Text = dt.Rows(0).Item(11)
TextBoxICOld.Text = dt.Rows(0).Item(12)
TextBoxPassport.Text = dt.Rows(0).Item(13)
DateTimePickerpassportexpire.Value = dt.Rows(0).Item(14)
TextBoxPermit.Text = dt.Rows(0).Item(15)
DateTimePickerpermitexpire.Value = dt.Rows(0).Item(16)
DateTimePickerBirthDate.Value = dt.Rows(0).Item(17)
TextBoxreligionPD.Text = dt.Rows(0).Item(18)
TextBoxAge.Text = dt.Rows(0).Item(19)
TextBoxracePD.Text = dt.Rows(0).Item(20)
TextBoxcitizenshipPD.Text = dt.Rows(0).Item(21)
TextBoxsexPD.Text = dt.Rows(0).Item(22)
TextBoxmaritalPD.Text = dt.Rows(0).Item(23)
If dt.Rows(0).Item(24) = "yes" Then
RadioButtonyesC.Checked = True
ElseIf dt.Rows(0).Item(24) = "No" Then
RadioButtonnoC.Checked = True
End If
If dt.Rows(0).Item(25) = "yes" Then
RadioButtonYesEOT.Checked = True
ElseIf dt.Rows(0).Item(25) = "No" Then
RadioButtonnoEOT.Checked = True
End If
TextBoxNameSpouse.Text = dt.Rows(0).Item(26)
TextBoxICNewSpouse.Text = dt.Rows(0).Item(27)
TextBoxICOldSpouse.Text = dt.Rows(0).Item(28)
RichTextBoxAddressSpouse.Text = dt.Rows(0).Item(29)
TextBoxIncomeSpouse.Text = dt.Rows(0).Item(30)
TextBoxBranchSpouse.Text = dt.Rows(0).Item(31)
If dt.Rows(0).Item(32) = "yes" Then
RadioButtonyesworking.Checked = True
ElseIf dt.Rows(0).Item(32) = "No" Then
RadioButtonNoworking.Checked = True
End If
TextBoxChildrenSpouse.Text = dt.Rows(0).Item(33)
If dt.Rows(0).Item(34) = "Auto" Then
RadioButtonAutoAuMaSpouse.Checked = True
ElseIf dt.Rows(0).Item(34) = "Manual" Then
RadioButtonManualAuMaSpouse.Checked = True
End If
TextBoxpositionED.Text = dt.Rows(0).Item(35)
TextBoxdepartmentED.Text = dt.Rows(0).Item(36)
TextBoxdivisionED.Text = dt.Rows(0).Item(37)
TextBoxsectionED.Text = dt.Rows(0).Item(38)
TextBoxlocationED.Text = dt.Rows(0).Item(39)
TextBoxgradeED.Text = dt.Rows(0).Item(40)
TextBoxcategoryED.Text = dt.Rows(0).Item(41)
TextBoxworkgroupED.Text = dt.Rows(0).Item(42)
TextBoxformularateED.Text = dt.Rows(0).Item(43)
DateTimePickerjoindateED.Value = dt.Rows(0).Item(44)
DateTimePickerconfirmdateED.Value = dt.Rows(0).Item(45)
DateTimePickerresigneddateED.Value = dt.Rows(0).Item(46)
TextBoxacbankED.Text = dt.Rows(0).Item(47)
If dt.Rows(0).Item(48) = "IC New" Then
RadioButtonICNewACBank.Checked = True
ElseIf dt.Rows(0).Item(48) = "IC Old" Then
RadioButtonICOldACBank.Checked = True
ElseIf dt.Rows(0).Item(48) = "Passport No" Then
RadioButtonPassportNoACBank.Checked = True
ElseIf dt.Rows(0).Item(48) = "Permit No" Then
RadioButtonPermitNoACBank.Checked = True
End If
TextBoxACNoED.Text = dt.Rows(0).Item(49)
TextBoxHRDFundED.Text = dt.Rows(0).Item(50)
If dt.Rows(0).Item(51) = "IC New" Then
RadioButtonICNewHRDFund.Checked = True
ElseIf dt.Rows(0).Item(51) = "IC Old" Then
RadioButtonICOldHRDFund.Checked = True
ElseIf dt.Rows(0).Item(51) = "Passport No" Then
RadioButtonPassportNoHRDFund.Checked = True
ElseIf dt.Rows(0).Item(51) = "Permit No" Then
RadioButtonPermitNoHRDFund.Checked = True
End If
TextBoxProbationED.Text = dt.Rows(0).Item(52)
TextBoxSalaryED.Text = dt.Rows(0).Item(53)
TextBoxNoticeResignED.Text = dt.Rows(0).Item(54)
TextBoxEmployerNamePE.Text = dt.Rows(0).Item(55)
RichTextBoxAddressPE.Text = dt.Rows(0).Item(56)
TextBoxTelNo1PE.Text = dt.Rows(0).Item(57)
TextBoxTelNo2PE.Text = dt.Rows(0).Item(58)
TextBoxFaxPE.Text = dt.Rows(0).Item(59)
TextBoxPICPE.Text = dt.Rows(0).Item(60)
TextBoxPositionPE.Text = dt.Rows(0).Item(61)
DateTimePickerdatejoinPE.Value = dt.Rows(0).Item(62)
TextBoxSalaryPE.Text = dt.Rows(0).Item(63)
DateTimePickerdateresignPE.Value = dt.Rows(0).Item(64)
TextBoxReasonPE.Text = dt.Rows(0).Item(65)
TextBoxEPFNoGD.Text = dt.Rows(0).Item(66)
If dt.Rows(0).Item(67) = "IC New" Then
RadioButtonICNewICTypeEPFGD.Checked = True
ElseIf dt.Rows(0).Item(67) = "IC Old" Then
RadioButtonICOldICTypeEPFGD.Checked = True
ElseIf dt.Rows(0).Item(67) = "Passport No" Then
RadioButtonPassportNoICTypeEPFGD.Checked = True
ElseIf dt.Rows(0).Item(67) = "Permit No" Then
RadioButtonPermitNoICTypeEPFGD.Checked = True
End If
TextBoxInitialGD.Text = dt.Rows(0).Item(68)
TextBoxNKGD.Text = dt.Rows(0).Item(69)
TextBoxepftableGD.Text = dt.Rows(0).Item(70)
TextBoxkwspGD.Text = dt.Rows(0).Item(71)
TextBoxIncomeTaxGD.Text = dt.Rows(0).Item(72)
If dt.Rows(0).Item(73) = "IC New" Then
RadioButtonICNewICTypeIncomeGD.Checked = True
ElseIf dt.Rows(0).Item(73) = "IC Old" Then
RadioButtonICOldICTypeIncomeGD.Checked = True
ElseIf dt.Rows(0).Item(73) = "Passport No" Then
RadioButtonPassportNoICTypeIncomeGD.Checked = True
ElseIf dt.Rows(0).Item(73) = "Permit No" Then
RadioButtonPermitNoICTypeIncomeGD.Checked = True
End If
TextBoxbranchincomeGD.Text = dt.Rows(0).Item(74)
TextBoxpcbcodeGD.Text = dt.Rows(0).Item(75)
TextBoxincometaxdeptGD.Text = dt.Rows(0).Item(76)
TextBoxSocsoNoGD.Text = dt.Rows(0).Item(77)
If dt.Rows(0).Item(78) = "IC New" Then
RadioButtonICNewICTypeSocsoGD.Checked = True
ElseIf dt.Rows(0).Item(78) = "IC Old" Then
RadioButtonICOldICTypeSocsoGD.Checked = True
ElseIf dt.Rows(0).Item(78) = "Passport No" Then
RadioButtonPassportNoICTypeSocsoGD.Checked = True
ElseIf dt.Rows(0).Item(78) = "Permit No" Then
RadioButtonPermitNoICTypeSocsoGD.Checked = True
End If
TextBoxBranchSocsoGD.Text = dt.Rows(0).Item(79)
TextBoxSocsoOfficeGD.Text = dt.Rows(0).Item(80)
TextBoxsocsotypeGD.Text = dt.Rows(0).Item(81)
TextBoxboardofsocsoGD.Text = dt.Rows(0).Item(82)
TextBoxTHAccGD.Text = dt.Rows(0).Item(83)
If dt.Rows(0).Item(84) = "IC New" Then
RadioButtonICNewICTypeTHGD.Checked = True
ElseIf dt.Rows(0).Item(84) = "IC Old" Then
RadioButtonICOldICTypeTHGD.Checked = True
ElseIf dt.Rows(0).Item(84) = "Passport No" Then
RadioButtonPassportNoICTypeTHGD.Checked = True
ElseIf dt.Rows(0).Item(84) = "Permit No" Then
RadioButtonPermitNoICTypeTHGD.Checked = True
End If
TextBoxthcGD.Text = dt.Rows(0).Item(85)
'Dim ms As New MemoryStream(changephoto(CInt(khaiEDForm2.DataGridViewfind.SelectedCells(0).Value)))
'PictureBox1.Image = Image.FromStream(ms)
cmd = New MySqlCommand("select * from salarydetail", cn)
da.SelectCommand = cmd
da.Fill(dtsalary)
End If
Catch ex As Exception
'TextBoxEmployeeName.Focus()
End Try
End Sub
'Function changephoto(ByVal photo As Integer) As Byte()
' Dim cn As MySqlConnection = New MySqlConnection()
' cn.ConnectionString = ("server=localhost; userid=root; password=; database=payrollsystemdb;")
' cn.Open()
' '.CommandText = System.Data.CommandType.Text
' With cmd
' .Connection = cn
' .CommandText = "SELECT Imageblob FROM employeedetail WHERE EmployeeID=" & khaiEDForm2.DataGridViewfind.SelectedRows(0).Cells(0).Value
' End With
' Dim myphoto() As Byte = CType(cmd.ExecuteScalar(), Byte())
' cn.Close()
' Return myphoto
'End Function
Private Sub TextBoxEmployeeID_LostFocus(sender As Object, e As EventArgs) Handles TextBoxEmployeeID.LostFocus
If TextBoxEmployeeID.Text = "" Then
TextBoxEmployeeID.Select()
Else
tunjukrekod()
tunjukdgv()
End If
addrow()
End Sub
How I Can Rewrite the code? because the code cam retrive picture only when from form2 to form1.
I used visual studio express 2014, MySQL Database using PHPMYADMIN, MySQLConnection.
ok . i got the solution ! finally !
dr = cmd.ExecuteReader()
If (dr.HasRows) Then
While (dr.Read())
With Me
'fetch image from database
Dim imgBytes() As Byte = dr("Imageblob") 'image field
Dim image As Bitmap = New Bitmap(New System.IO.MemoryStream(imgBytes)) 'convert binary to image
.PictureBox1.Image = image 'show picture to picture box
End With
End While
Else
MsgBox("No records Found!")
End If