如何使用文本框丢失焦点从数据库到图片框获取图片

时间:2016-04-01 03:59:37

标签: vb.net

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

我如何重写代码?因为只有从form2到form1时,代码才会生成反转图片。

我使用visual studio express 2014MySQL Database使用PHPMYADMINMySQLConnection

1 个答案:

答案 0 :(得分:0)

好的我得到了解决方案!终于!

            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