22-02-20, 04:37 PM
قبل البحث
بعد البحث
كود البحث
[/code]
بعد البحث
كود البحث
كود :
Imports System.Data.SqlClient
Imports System.IO
Public Class Form9
Public ID As Integer
Private Function Add() As Boolean
Dim temp As Boolean
Dim pc As New PC
With pc
.ID = GetCount("PC")
.Name = txtname.Text.Trim
.code = txtcode.Text.Trim
.floor = txtfloor.Text.Trim
.office = txtoffice.Text.Trim
.serial = txtSerial.Text.Trim
.laptob = txtLaptob.Text.Trim
.pc = txtpc.Text.Trim
.description = txtDescription.Text.Trim
.comment = txtComment.Text.Trim
.usbm = txtUsbModam.Text.Trim
.datee = txtDatee.Value
.SNram1 = txtram1.Text.Trim
.SNram2 = txtram2.Text.Trim
.SNram3 = txtram3.Text.Trim
.SNram4 = txtram4.Text.Trim
.datafile = IO.File.ReadAllBytes(Me.filename)
.extfile = IO.Path.GetExtension(Me.filename)
pbox.Image = Nothing
End With
temp = pc.Add()
pc = Nothing
Return temp
End Function
Private Function Edit() As Boolean
Dim temp As Boolean
Dim pc As New PC
With pc
.ID = GetCount("PC")
.Name = txtname.Text.Trim
.code = txtcode.Text.Trim
.floor = txtfloor.Text.Trim
.office = txtoffice.Text.Trim
.serial = txtSerial.Text.Trim
.laptob = txtLaptob.Text.Trim
.pc = txtpc.Text.Trim
.description = txtDescription.Text.Trim
.comment = txtComment.Text.Trim
.usbm = txtUsbModam.Text.Trim
.datee = txtDatee.Value
.SNram1 = txtram1.Text.Trim
.SNram2 = txtram2.Text.Trim
.SNram3 = txtram3.Text.Trim
.SNram4 = txtram4.Text.Trim
If filename <> "" Then
.datafile = IO.File.ReadAllBytes(Me.filename)
.extfile = IO.Path.GetExtension(Me.filename)
End If
pbox.Image = Nothing
End With
temp = pc.Edit(ID)
pc = Nothing
Return temp
End Function
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles Edit_Button.Click
If (String.IsNullOrWhiteSpace(txtname.Text) AndAlso String.IsNullOrWhiteSpace(txtcode.Text) AndAlso String.IsNullOrWhiteSpace(txtfloor.Text) AndAlso String.IsNullOrWhiteSpace(txtoffice.Text) AndAlso String.IsNullOrWhiteSpace(txtSerial.Text) AndAlso String.IsNullOrWhiteSpace(txtLaptob.Text) AndAlso String.IsNullOrWhiteSpace(txtpc.Text) AndAlso String.IsNullOrWhiteSpace(txtDescription.Text) AndAlso String.IsNullOrWhiteSpace(txtComment.Text) AndAlso String.IsNullOrWhiteSpace(txtUsbModam.Text) Or IsNothing(pbox.Image)) Then
MessageBox.Show("يجب ملئ الخانات قبل الاضافه")
Return
End If
If ID = 0 Then
SaveLog("إضافة سجل", Me.Text, "تمت عملية إضافة سجل جديد بإسم : " & txtname.Text)
If Add() Then
LoadData()
ClearForm()
End If
Else
SaveLog("تعديل سجل", Me.Text, "تمت عملية تعديل سجل بإسم : " & txtname.Text)
If Edit() Then
LoadData()
ClearForm()
End If
End If
End Sub
Function LoadData() As DataTable
Dim temp As New DataTable
If Convert.ToBoolean(GetValueFromRegistry("WindowsOrSqlServer")) = True Then
Cnn = New SqlConnection(CnnString)
ElseIf Convert.ToBoolean(GetValueFromRegistry("WindowsOrSqlServer")) = False Then
Cnn = New SqlConnection(CnnString1)
End If
'Dim da As New SqlDataAdapter("Select * From PC", Cnn)
'Dim dt As New DataTable
'da.Fill(dt)
'Me.DataGridView1.DataSource = dt
Cmd = New SqlCommand
Cmd.CommandText = "Select * From PC"
Cmd.CommandType = CommandType.Text
Cmd.Connection = Cnn
Try
Cnn.Open()
temp.Load(Cmd.ExecuteReader())
DataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = temp
FillDesignDGV(DataGridView1, "Select * From PC")
DataGridView1.DataSource = temp ' هنا يتم ربط البيانات
' لديم امرين لتعبئة الداتاجريد المفروض واحد منهم
For Each r As DataGridViewRow In DataGridView1.Rows
If r.Cells("extfile").Value.ToString.ToLower = ".pdf" Then
r.Cells("Column1").Value = My.Resources.pdf
Else
r.Cells("Column1").Value = My.Resources.jpeg
End If
Next
Catch ex As SqlException
MsgBox(ex.Message)
Finally
If Cmd IsNot Nothing Then
Cmd.Dispose()
Cmd = Nothing
End If
If Cnn IsNot Nothing Then
Cnn.Close()
Cnn.Dispose()
Cnn = Nothing
End If
End Try
Return temp
End Function
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.DataGridView1.AutoGenerateColumns = False
SaveLog(MyBase.Text, Me.Text, "تمت عملية فتح النافذة التالية : " & MyBase.Text)
LoadData()
DataGridView1.AllowUserToDeleteRows = False
DataGridView1.AllowUserToAddRows = False
DataGridView1.ReadOnly = True
Label16.Text = (From x In DataGridView1.Rows Where x.Cells("cllaptob").Value.Equals("yes")).Count
Label19.Text = (From x In DataGridView1.Rows Where x.Cells("clpc").Value.Equals("yes")).Count
End Sub
Function LoadOneDetail(id As Integer) As DataTable
Dim temp As New DataTable
If Convert.ToBoolean(GetValueFromRegistry("WindowsOrSqlServer")) = True Then
Cnn = New SqlConnection(CnnString)
ElseIf Convert.ToBoolean(GetValueFromRegistry("WindowsOrSqlServer")) = False Then
Cnn = New SqlConnection(CnnString1)
End If
Cmd = New SqlCommand
Cmd.CommandText = "Select * From PC where id = @id "
Cmd.CommandType = CommandType.Text
Cmd.Parameters.Add("@Id", SqlDbType.Int).Value = id
Cmd.Connection = Cnn
Try
Cnn.Open()
temp.Load(Cmd.ExecuteReader())
If temp.Rows.Count <> 0 Then
Me.ID = temp.Rows(0).Item("ID")
txtname.Text = temp.Rows(0).Item("Name").ToString
txtcode.Text = temp.Rows(0).Item("code").ToString
txtfloor.Text = temp.Rows(0).Item("floor").ToString
txtoffice.Text = temp.Rows(0).Item("office").ToString
txtSerial.Text = temp.Rows(0).Item("serial").ToString
txtLaptob.Text = temp.Rows(0).Item("laptob").ToString
txtpc.Text = temp.Rows(0).Item("pc").ToString
txtDescription.Text = temp.Rows(0).Item("description").ToString
txtComment.Text = temp.Rows(0).Item("comment").ToString
txtUsbModam.Text = temp.Rows(0).Item("usbm").ToString
txtDatee.Value = temp.Rows(0).Item("datee")
txtram1.Text = temp.Rows(0).Item("txtram1").ToString
txtram2.Text = temp.Rows(0).Item("txtram2").ToString
txtram3.Text = temp.Rows(0).Item("txtram3").ToString
txtram4.Text = temp.Rows(0).Item("txtram4").ToString
Dim b() As Byte = temp.Rows(0).Item("datafile")
If temp.Rows(0).Item("extfile") = ".pdf" Then
pbox.Image = My.Resources.pdf
Else
Dim ms As New MemoryStream(b)
pbox.Image = Image.FromStream(ms)
ms.Dispose()
End If
End If
Catch ex As SqlException
MsgBox(ex.Message)
Finally
If Cmd IsNot Nothing Then
Cmd.Dispose()
Cmd = Nothing
End If
If Cnn IsNot Nothing Then
Cnn.Close()
Cnn.Dispose()
Cnn = Nothing
End If
End Try
Return temp
End Function
Dim imagechange As Boolean
Private Sub DataGridView1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseDoubleClick
Dim id As Integer = Val(DataGridView1.CurrentRow.Cells("clID").Value.ToString)
LoadOneDetail(id)
imagechange = False
End Sub
Private Sub ClearForm()
ID = 0
For Each ctl In Me.Controls
If TypeOf (ctl) Is TextBox Then
CType(ctl, TextBox).Clear()
ElseIf TypeOf (ctl) Is DateTimePicker Then
CType(ctl, DateTimePicker).Value = Today.Date
End If
Next
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles Delete_Button.Click
If ID = 0 Then
MsgBox("Set the detail you want to delete.")
Else
If MsgBox("Are you sure you want to delete?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
SaveLog("حذف سجل", Me.Text, "تمت عملية حذف سجل بإسم : " & txtname.Text)
If PC.Delete(ID) Then
LoadData()
ClearForm()
End If
End If
End If
End Sub
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles Add_Button.Click
ClearForm()
pbox.Image = Nothing
End Sub
Dim filename As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Import_Button.Click
ofd.FileName = ""
ofd.Filter = "Image Files (JPEG,GIF,BMP,PNG,ICO,PDF)|*.jpg;*.jpeg;*.gif;*.bmp;*.png;*.ico;*.pdf"
If ofd.ShowDialog() = DialogResult.OK Then
Me.filename = ofd.FileName
Dim ext = IO.Path.GetExtension(filename)
If ext.ToLower = ".pdf" Then
pbox.Image = My.Resources.pdf
Else
pbox.Image = Image.FromFile(filename)
imagechange = True
End If
End If
End Sub
Private Sub txtname_TextChanged(sender As Object, e As EventArgs) Handles txtname.TextChanged
''If txtname Is Nothing Then
'' txtname.BackColor = Color.DarkCyan
'' ' txtname.BackColor = Color.DarkCyan
'End If
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim temp As New DataTable
If Convert.ToBoolean(GetValueFromRegistry("WindowsOrSqlServer")) = True Then
Cnn = New SqlConnection(CnnString)
ElseIf Convert.ToBoolean(GetValueFromRegistry("WindowsOrSqlServer")) = False Then
Cnn = New SqlConnection(CnnString1)
End If
Dim r As DataGridViewRow = Me.DataGridView1.CurrentRow
Dim id As Integer = Val(r.Cells("clID").Value.ToString)
Dim sr As Integer = r.Index + 1
If MsgBox("هل تريد تحميل الصورة رقم: " & sr, MsgBoxStyle.YesNo) <> MsgBoxResult.Yes Then Exit Sub
Dim cmd As New SqlCommand("Select [datafile] From [PC] WHERE [iD]=@iD", Cnn)
cmd.Parameters.AddWithValue("@iD", id)
If Cnn.State <> ConnectionState.Open Then Cnn.Open()
Dim obj As Object = cmd.ExecuteScalar
Cnn.Close()
If obj IsNot Nothing AndAlso Not IsDBNull(obj) Then
If r.Cells("extfile").Value = ".pdf" Then
Dim SaveFileAs As New SaveFileDialog
SaveFileAs.Title = "Save File As"
SaveFileAs.Filter = "PDF Files (PDF)|*.pdf"
If SaveFileAs.ShowDialog() = DialogResult.OK Then
IO.File.WriteAllBytes(SaveFileAs.FileName, obj)
MsgBox("تم حفظ الملف")
End If
Else
Dim stream As New IO.MemoryStream(CType(obj, Byte()))
Dim img As Image = Image.FromStream(stream)
Dim SaveFileAs As New SaveFileDialog
SaveFileAs.Title = "Save File As"
SaveFileAs.Filter = "Image Files (JPEG)|*.jpg;*.jpeg"
If SaveFileAs.ShowDialog() = DialogResult.OK Then
img.Save(SaveFileAs.FileName, Imaging.ImageFormat.Jpeg)
MsgBox("تم حفظ الصورة")
End If
End If
End If
End Sub
Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
' يترك فاضي
End Sub
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If sender.Columns(e.ColumnIndex).Name = "clSR" Then e.Value = e.RowIndex + 1
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
'Try
' If DataGridView1.Rows(e.RowIndex).Cells("extfile").Value = ".pdf" Then
' DataGridView1.Rows(e.RowIndex).Cells("Column1").Value = My.Resources.pdf
' Else
' DataGridView1.Rows(e.RowIndex).Cells("Column1").Value = My.Resources.jpeg
' End If
'Catch ex As Exception
'End Try
End Sub
Private Sub _TextChanged(sender As Object, e As EventArgs) Handles txtLaptob.TextChanged
If txtLaptob.Text = "" Then
txtLaptob.BackColor = Color.Red
Else
txtLaptob.BackColor = Color.Black
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim SqlStr As String = String.Empty
SqlStr = "SELECT name FROM Pc where [code] like N'%" & TextBox1.Text & "%' or [name] Like N'%" & TextBox1.Text & "%'"
' SqlStr = "SELECT * FROM Pc where [Name,code] like N'%" & TextBox1.Text & "%' or [ Name,code,floor,office,serial,laptob,pc,description,datee] Like N'%" & TextBox1.Text & "%'"
FillDesignDGV(DataGridView1, SqlStr)
End Sub
End Class