الكود بيقبل الصور محتاج اضيف معا pdf
كود :
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
.image1 = pbox.Image
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 = ID
.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
.image1 = pbox.Image
.imagechange = imagechange
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
'Dim bm As Bitmap = pbox.Image
'Dim BO1 As Boolean = True
'Try
' If bm.Width > Nothing Then BO1 = False
'Catch ex As Exception
'End Try
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
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")
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
SaveLog(MyBase.Text, Me.Text, "تمت عملية فتح النافذة التالية : " & MyBase.Text)
LoadData()
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
b = temp.Rows(0).Item("image1")
Dim ms As New MemoryStream(b)
pbox.Image = Image.FromStream(ms)
ms.Dispose()
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
LoadOneDetail(DataGridView1.CurrentRow.Cells(clID.Name).Value)
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
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)|*.jpg;*.jpeg;*.gif;*.bmp;*.png;*ico"
ofd.ShowDialog()
If ofd.FileName = "" Then Exit Sub
pbox.Image = Image.FromFile(ofd.FileName)
imagechange = True
End Sub
Private Sub pbox_Click(sender As Object, e As EventArgs) Handles pbox.Click
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 txtcode_TextChanged(sender As Object, e As EventArgs) Handles txtcode.TextChanged
End Sub
Private Sub txtfloor_TextChanged(sender As Object, e As EventArgs) Handles txtfloor.TextChanged
End Sub
Private Sub txtoffice_TextChanged(sender As Object, e As EventArgs) Handles txtoffice.TextChanged
End Sub
Private Sub txtSerial_TextChanged(sender As Object, e As EventArgs) Handles txtSerial.TextChanged
End Sub
Private Sub txtpc_TextChanged(sender As Object, e As EventArgs) Handles txtpc.TextChanged
End Sub
Private Sub txtDescription_TextChanged(sender As Object, e As EventArgs) Handles txtDescription.TextChanged
End Sub
Private Sub Label8_Click(sender As Object, e As EventArgs) Handles Label8.Click
End Sub
Private Sub Label7_Click(sender As Object, e As EventArgs) Handles Label7.Click
End Sub
Private Sub txtComment_TextChanged(sender As Object, e As EventArgs) Handles txtComment.TextChanged
End Sub
Private Sub txtDatee_ValueChanged(sender As Object, e As EventArgs) Handles txtDatee.ValueChanged
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub ofd_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ofd.FileOk
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub Label9_Click(sender As Object, e As EventArgs) Handles Label9.Click
End Sub
Private Sub Label10_Click(sender As Object, e As EventArgs) Handles Label10.Click
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 = r.Cells("clID").Value
Dim cmd As New SqlCommand("Select [image1] 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
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 Sub
End Class
كود الكلاس
Imports System.Data.SqlClient
Imports System.Drawing.Imaging
Imports System.IO
Public Class PC
Property ID As String
Property Name As String
Property code As String
Property floor As String
Property office As String
Property serial As String
Property laptob As String
Property pc As String
Property description As String
Property comment As String
Property usbm As String
Property datee As Date
Property SNram1 As String
Property SNram2 As String
Property SNram3 As String
Property SNram4 As String
Property image1 As Image
Property imagechange As Boolean
Function Add() As Integer
Dim temp As Integer
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 = "AddPC"
Cmd.CommandType = CommandType.StoredProcedure
Cmd.Connection = Cnn
Cmd.Parameters.AddWithValue("@ID", ID)
Cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = Name
Cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = code
Cmd.Parameters.Add("@floor", SqlDbType.NVarChar).Value = floor
Cmd.Parameters.Add("@office", SqlDbType.NVarChar).Value = office
Cmd.Parameters.Add("@serial", SqlDbType.NVarChar).Value = serial
Cmd.Parameters.Add("@laptob", SqlDbType.NVarChar).Value = laptob
Cmd.Parameters.Add("@pc", SqlDbType.NVarChar).Value = pc
Cmd.Parameters.Add("@description", SqlDbType.NVarChar).Value = description
Cmd.Parameters.Add("@comment", SqlDbType.NVarChar).Value = comment
Cmd.Parameters.Add("@usbm", SqlDbType.NVarChar).Value = usbm
Cmd.Parameters.Add("@datee", SqlDbType.Date).Value = datee
Cmd.Parameters.Add("@txtram1", SqlDbType.NVarChar).Value = SNram1
Cmd.Parameters.Add("@txtram2", SqlDbType.NVarChar).Value = SNram2
Cmd.Parameters.Add("@txtram3", SqlDbType.NVarChar).Value = SNram3
Cmd.Parameters.Add("@txtram4", SqlDbType.NVarChar).Value = SNram4
Dim imgByteArray() As Byte
Dim stream As New MemoryStream
image1.Save(stream, ImageFormat.Jpeg)
imgByteArray = stream.ToArray()
Cmd.Parameters.AddWithValue("@image1", imgByteArray)
stream.Close()
Cnn.Open()
If Cmd.ExecuteNonQuery() <> 0 Then
temp = True
End If
If Cmd IsNot Nothing Then
Cmd.Dispose()
Cmd = Nothing
End If
If Cnn IsNot Nothing Then
Cnn.Close()
Cnn.Dispose()
Cnn = Nothing
End If
Return temp
End Function
Public Img() As Byte = Nothing
Private pbox As Object
Public Function GetImg(ByVal FilePath As String) As Byte()
Dim Strem As FileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
Dim Reader As BinaryReader = New BinaryReader(Strem)
Dim Img() As Byte = Reader.ReadBytes(Strem.Length)
Reader.Close()
Strem.Close()
Return Img
End Function
Function Edit(id As Integer) As Boolean
Dim temp As Boolean
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 = "Editpc"
Cmd.CommandType = CommandType.StoredProcedure
Cmd.Connection = Cnn
Cmd.Parameters.AddWithValue("@ID", id)
Cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = Name
Cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = code
Cmd.Parameters.Add("@floor", SqlDbType.NVarChar).Value = floor
Cmd.Parameters.Add("@office", SqlDbType.NVarChar).Value = office
Cmd.Parameters.Add("@serial", SqlDbType.NVarChar).Value = serial
Cmd.Parameters.Add("@laptob", SqlDbType.NVarChar).Value = laptob
Cmd.Parameters.Add("@pc", SqlDbType.NVarChar).Value = pc
Cmd.Parameters.Add("@description", SqlDbType.NVarChar).Value = description
Cmd.Parameters.Add("@comment", SqlDbType.NVarChar).Value = comment
Cmd.Parameters.Add("@usbm", SqlDbType.NVarChar).Value = usbm
Cmd.Parameters.Add("@datee", SqlDbType.Date).Value = datee
Cmd.Parameters.Add("@txtram1", SqlDbType.NVarChar).Value = SNram1
Cmd.Parameters.Add("@txtram2", SqlDbType.NVarChar).Value = SNram2
Cmd.Parameters.Add("@txtram3", SqlDbType.NVarChar).Value = SNram3
Cmd.Parameters.Add("@txtram4", SqlDbType.NVarChar).Value = SNram4
Try
Cnn.Open()
If Cmd.ExecuteNonQuery() <> 0 Then
temp = True
If imagechange = True Then
Cmd = New SqlCommand
Cmd.CommandText = "editimagem"
Cmd.CommandType = CommandType.StoredProcedure
Cmd.Connection = Cnn
Cmd.Parameters.AddWithValue("@ID", id)
Dim imgByteArray() As Byte
Dim stream As New MemoryStream
image1.Save(stream, ImageFormat.Jpeg)
imgByteArray = stream.ToArray()
Cmd.Parameters.AddWithValue("@image1", imgByteArray)
Cmd.ExecuteNonQuery()
stream.Close()
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
Shared Function Delete(id As Integer) As Boolean
Dim temp As Boolean
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 = "Deletepc"
Cmd.CommandType = CommandType.StoredProcedure
Cmd.Connection = Cnn
Cmd.Parameters.AddWithValue("@ID", id)
Try
Cnn.Open()
If Cmd.ExecuteNonQuery() <> 0 Then
temp = True
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
End Class