مساعدة في الشرح + التطوير ان امكن - 0theghost0 - 09-09-16
السلام عليكم
لدي كود اريد فهمة + ان كان هناك كود افضل منه للفهم اسرع ونفس الوظيفة
كود :
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
If DataGridView2.Rows.Count <= 0 Then
MsgBox("Please add some items to print", MsgBoxStyle.Exclamation)
Exit Sub
End If
Dim con As New OleDb.OleDbConnection(My.Settings.POSConnectionString)
con.Open()
Dim sql As String = "Insert into Recipts (ReciptDate,ReciptTotal) Values(:0,:1)"
Dim cmd As New OleDb.OleDbCommand
cmd.Parameters.AddWithValue(":0", Date.Now)
cmd.Parameters.AddWithValue(":1", TextBox4.Text)
With cmd
.Connection = con
.CommandText = sql
End With
cmd.ExecuteNonQuery()
cmd.Dispose()
Dim cmd1 As New OleDb.OleDbCommand
sql = "Select max(ReciptID) as MAXID from Recipts"
With cmd1
.CommandText = sql
.Connection = con
End With
Dim ReciptID As Long = cmd1.ExecuteScalar
cmd1.Dispose()
Dim i As Integer
For i = 0 To DataGridView2.Rows.Count - 1
Dim Barcode As String = DataGridView2.Rows(i).Cells(0).Value
Dim BuyPrice As String = DataGridView2.Rows(i).Cells(2).Value
Dim SellPrice As String = DataGridView2.Rows(i).Cells(3).Value
Dim ItemCount As Integer = DataGridView2.Rows(i).Cells(4).Value
Dim cmd2 As New OleDb.OleDbCommand
sql = "Insert into ReciptDetails values(:0,:1,:2,:3,:4)"
With cmd2
.CommandText = sql
.Connection = con
End With
cmd2.Parameters.AddWithValue(":0", ReciptID)
cmd2.Parameters.AddWithValue(":1", Barcode)
cmd2.Parameters.AddWithValue(":2", ItemCount)
cmd2.Parameters.AddWithValue(":3", BuyPrice)
cmd2.Parameters.AddWithValue(":4", SellPrice)
cmd2.ExecuteNonQuery()
cmd2.Dispose()
Next
con.Close()
con.Dispose()
If Not IsNothing(TextBox6.Text) Then
ReciptImage = DrawRecipt(DataGridView2.Rows, ReciptID, Format(Now.Date, "dd-mm-yyyy"), TextBox4.Text, TextBox5.Text, TextBox8.Text, TextBox7.Text)
If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PrintDoc.PrinterSettings = PrintDialog1.PrinterSettings
PrintDoc.Print()
DataGridView2.Rows.Clear()
TextBox4.Clear()
End If
ElseIf PictureBox1.Image Is Nothing Then
MsgBox("Can't Print receipt please check the settings", MsgBoxStyle.Critical)
Else
MsgBox("You did not setup the printer", MsgBoxStyle.Exclamation)
End If
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical)
End Try
End Sub
الكود هو طباعة الرصيد + تعديل على بيانات المنتج مع الكمية المتوفرة
الكود شغال بدون مشاكل ولكن الكود طويل وصعب الفهم بالنسبة لي احب الاكواد المبسطة والمفهومة بنفس قوة والوظيفة .
شكرا لكم اخواني على المشاهدة و القراءة واشكر كل من حاول المساعدة وطبعا اشكر كل عضو ساعدني سابقا ومازال يساعدني
RE: مساعدة في الشرح + التطوير ان امكن - Adrees - 09-09-16
الكود يتكون من اربعة جزئيات
الجزء الاول حفظ تاريخ الرصيد المجموع
كود :
Dim con As New OleDb.OleDbConnection(My.Settings.POSConnectionString)
con.Open()
Dim sql As String = "Insert into Recipts (ReciptDate,ReciptTotal) Values(:0,:1)"
Dim cmd As New OleDb.OleDbCommand
cmd.Parameters.AddWithValue(":0", Date.Now)
cmd.Parameters.AddWithValue(":1", TextBox4.Text)
With cmd
.Connection = con
.CommandText = sql
End With
cmd.ExecuteNonQuery()
cmd.Dispose()
الجزء الثاني لجلب اكبر قيمة من حقل ReceiptID واستخدام هذه القيمة في رقم رصيد جديد
كود :
Dim cmd1 As New OleDb.OleDbCommand
sql = "Select max(ReciptID) as MAXID from Recipts"
With cmd1
.CommandText = sql
.Connection = con
End With
Dim ReciptID As Long = cmd1.ExecuteScalar
cmd1.Dispose()
الجزء الثالث لحفظ تفاصيل الرصيد والتفاصيل هنا هو عدد غير محدود من الصفوف الموجودة في الــ DataGridView
كود :
Dim i As Integer
For i = 0 To DataGridView2.Rows.Count - 1
Dim Barcode As String = DataGridView2.Rows(i).Cells(0).Value
Dim BuyPrice As String = DataGridView2.Rows(i).Cells(2).Value
Dim SellPrice As String = DataGridView2.Rows(i).Cells(3).Value
Dim ItemCount As Integer = DataGridView2.Rows(i).Cells(4).Value
Dim cmd2 As New OleDb.OleDbCommand
sql = "Insert into ReciptDetails values(:0,:1,:2,:3,:4)"
With cmd2
.CommandText = sql
.Connection = con
End With
cmd2.Parameters.AddWithValue(":0", ReciptID)
cmd2.Parameters.AddWithValue(":1", Barcode)
cmd2.Parameters.AddWithValue(":2", ItemCount)
cmd2.Parameters.AddWithValue(":3", BuyPrice)
cmd2.Parameters.AddWithValue(":4", SellPrice)
cmd2.ExecuteNonQuery()
cmd2.Dispose()
Next
con.Close()
con.Dispose()
الجزء الرابع لطباعة الرصيد
كود :
If Not IsNothing(TextBox6.Text) Then
ReciptImage = DrawRecipt(DataGridView2.Rows, ReciptID, Format(Now.Date, "dd-mm-yyyy"), TextBox4.Text, TextBox5.Text, TextBox8.Text, TextBox7.Text)
If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PrintDoc.PrinterSettings = PrintDialog1.PrinterSettings
PrintDoc.Print()
DataGridView2.Rows.Clear()
TextBox4.Clear()
End If
ElseIf PictureBox1.Image Is Nothing Then
MsgBox("Can't Print receipt please check the settings", MsgBoxStyle.Critical)
Else
MsgBox("You did not setup the printer", MsgBoxStyle.Exclamation)
End If
اخي هذا شرح حسب فهمي المتواضع.
|