Public Function InsertProduct(ByVal Productid As Integer, CapMin As Decimal, CapMax As Decimal, CapDiscount As Decimal) As Boolean
Dim reader2 As OleDbDataReader
Dim comm2 As String
comm2 = " UPDATE promotion"
comm2 += " SET min_price = '" & CapMin & "' , max_price = '" & CapMax & "', discount = '" & CapDiscount & "' "
comm2 += " where promotion_id = '" & Productid & "' "
comm2 += " ( select product ,min_price,max_price,discount "
comm2 += " FROM promotion "
comm2 += " where promotion_id = '" & Productid & "' "
comm2 += " and (( '" & CapMin & "' >= min_price and '" & CapMin & "' <= isnull(max_price,99999999)) "
comm2 += " or ('" & CapMax & "' >= min_price and '" & CapMax & "' <= isnull(max_price,99999999)))) "
Dim OleComm2 As New OleDbCommand(comm2, sqlconn)
sqlconn.Open()
reader2 = OleComm2.ExecuteReader(CommandBehavior.CloseConnection)
If reader2.HasRows Then
Return False
Else
Return True
End If
End Function
เรียกใช้... Code (VB.NET)
Private Sub GridView_promotion_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles GridView_promotion.RowUpdating
' Dim cap_product_code As String = CType(GridView_promotion.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
Dim cap_product_id As String = GridView_promotion.DataKeys(e.RowIndex).Item(0).ToString
Dim cap_min As Decimal = CType(GridView_promotion.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text
Dim cap_max As Decimal = CType(GridView_promotion.Rows(e.RowIndex).Cells(5).Controls(0), TextBox).Text
Dim cap_dis As Decimal = CType(GridView_promotion.Rows(e.RowIndex).Cells(7).Controls(0), TextBox).Text
If InsertProduct(cap_product_id, cap_min, cap_max, cap_dis) Then
Using sqlconn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Dim cmd As New OleDbCommand()
cmd.Connection = sqlconn
'product = ?,
cmd.CommandText = "UPDATE promotion SET min_price = ?, max_price = ?, discount = ? WHERE promotion_id = ?"
cmd.CommandType = CommandType.Text
Dim strpromotion_id As String = GridView_promotion.DataKeys(e.RowIndex).Item(0).ToString
Dim a As TextBox = GridView_promotion.Rows(e.RowIndex).Cells(4).Controls(0)
' Dim strproduct As String = CType(GridView_promotion.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
Dim strminP As Decimal = CType(GridView_promotion.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text
Dim strmaxP As Decimal = CType(GridView_promotion.Rows(e.RowIndex).Cells(5).Controls(0), TextBox).Text
Dim strDis As Decimal = CType(GridView_promotion.Rows(e.RowIndex).Cells(7).Controls(0), TextBox).Text
' cmd.Parameters.Add("@p_product", OleDbType.VarChar).Value = strproduct
cmd.Parameters.Add("@p_min", OleDbType.Decimal).Value = strminP
cmd.Parameters.Add("@p_max", OleDbType.Decimal).Value = strmaxP
cmd.Parameters.Add("@p_dis", OleDbType.Decimal).Value = strDis
cmd.Parameters.Add("@p_id", OleDbType.Integer).Value = strpromotion_id
sqlconn.Open()
cmd.ExecuteNonQuery()
sqlconn.Close()
End Using
GridView_promotion.EditIndex = -1
BindGridView()
lblAdd.Visible = True
Else
Response.Write("โปรโมชั่นซ้ำ!!! ลองใส่ใหม่อีกทีละกัน ")
End If
End Sub