السلام عليكم..
ممكن شرح مبسسسسسط للكود هذا..؟وخصوصا عند زر الاضافة ..احسه ملخبط...اذا في كود اسهل منه ياليت..
Imports System.Data.SqlClient
Public Class frm_add_product
Private da As New SqlDataAdapter("SELECT * FROM Products", cn)
Private ds As New DataSet
Private ProductsBS As BindingSource = New BindingSource
Dim SQLstr As String = "SELECT * FROM Products"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(cn.State.ToString)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.Open()
Try
da.Fill(ds, "Products")
dg.DataSource = ds.Tables("Products")
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
da.InsertCommand = New SqlCommand("INSERT INTO Products VALUES (@Product_id,@Cust_id,@Product_name,@Fare_product,@Price,@Qty_produced,@qty_sold,@Qty_damaged,@Qty_Remainder)", cn)
da.InsertCommand.Parameters.Add("@Product_id", SqlDbType.Int).Value = TextBox1.Text
da.InsertCommand.Parameters.Add("@Cust_id", SqlDbType.Int).Value = TextBox2.Text
da.InsertCommand.Parameters.Add("@Product_name", SqlDbType.NVarChar).Value = TextBox3.Text
da.InsertCommand.Parameters.Add("@Fare_product", SqlDbType.Int).Value = TextBox4.Text
da.InsertCommand.Parameters.Add("@Price", SqlDbType.Money).Value = TextBox5.Text
da.InsertCommand.Parameters.Add("@Qty_produced", SqlDbType.Int).Value = TextBox9.Text
da.InsertCommand.Parameters.Add("@qty_sold", SqlDbType.Int).Value = TextBox6.Text
da.InsertCommand.Parameters.Add("@Qty_damaged", SqlDbType.Int).Value = TextBox7.Text
da.InsertCommand.Parameters.Add("@Qty_Remainder", SqlDbType.Int).Value = TextBox8.Text
da.InsertCommand.ExecuteNonQuery()
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
da.DeleteCommand = New SqlCommand("DELETE FROM Products WHERE Product_id = '" & Trim(TextBox1.Text) & "'", cn)
' da.DeleteCommand.Parameters.Add("@Product_id", SqlDbType.Int).Value = ds.Tables(0).Rows(ProductsBS.Position)(0)
da.DeleteCommand.ExecuteNonQuery()
'ds.Clear()
da.Fill(ds)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
da.SelectCommand = New SqlCommand("SELECT * FROM Products ", cn)
ds.Clear()
Try
da.Fill(ds, "Products")
dg.DataSource = ds.Tables("Products")
Catch ex As Exception
End Try
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim SavInto As New SqlCommand
Dim DataAdapter1 As New SqlDataAdapter(SQLstr, cn)
SavInto.Connection = cn
SavInto.CommandType = CommandType.Text
SavInto.CommandText = "DELETE FROM Products WHERE Product_id ='" & Trim(TextBox1.Text) & "'"
'cn.Open()
SavInto.ExecuteNonQuery()
ds.Clear()
DataAdapter1.Fill(ds, "Products")
'cn.Close()
MsgBox("تمت عملية الحذف في قاعدة البيانات بنجاح")
' ChangePosition()
End Sub
End Classوكمان يطلع لي الرساله التاليه عند اضافة سجل جديد ...مالمشكلة ..؟ومالحل..؟
ياااليت يكون الرد سريع..
جزااااكم الله كل خير...