ถ้าเรา edit >update > id : A1
เป็น min = 600 max = 800
จะเเสดงว่าข้อความว่า ข้อมูลซ้ำ (เอาไปเปรียบเทียบกับ A1 ตัวที่เหลือค่ะ ส่วนค่า NULL หมายถึง max 2000+ ขึ้นไป)
เรื่องที่สองคือ ตอน update ถ้าเราไม่ใส่อะไรเลยในช่อง max จะทำให้ หมายถึง NULL ค่ะ
อันนี้ โค้ดส่วน update ค่ะ Code (VB.NET)
Private Sub GridView_promotion_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles GridView_promotion.RowUpdating
Using sqlconn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Dim cmd As New OleDbCommand()
cmd.Connection = sqlconn
cmd.CommandText = "UPDATE promotion SET product = ?,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(3).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
End Sub
Private Sub GridView_promotion_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles GridView_promotion.RowUpdating
Dim cap_promotion_id As Integer = GridView_promotion.DataKeys(e.RowIndex).Item(0).ToString
Dim cap_productcode As String = CType(GridView_promotion.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
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 Not InsertProduct(cap_promotion_id, cap_productcode, cap_min, cap_max, cap_dis) Then
Using sqlconn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Dim cmd As New OleDbCommand()
cmd.Connection = sqlconn
cmd.CommandText = "UPDATE promotion SET product = ?,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(3).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("โปรโมชั่นซ้ำ ErrorXYZ ")
End If
End Sub
Public Function InsertProduct(ByVal Promotionid As Integer, ProductCode As String, CapMin As Decimal, CapMax As Decimal, CapDiscount As Decimal) As Boolean
Dim reader2 As OleDbDataReader
Dim comm2 As String
comm2 = " Select promotion_id "
comm2 += " FROM promotion"
comm2 += " where promotion_id = '" & Promotionid & "'"
comm2 += " (select product ,min_price ,max_price,discount "
comm2 += " FROM promotion "
comm2 += " where product = '" & ProductCode & "' "
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
Tag : ASP, Web (ASP.NET), VB.NET, VS 2013 (.NET 4.x)
ข้อ1) อ่านแล้ว งง? ว่าต้องการอะไร
- จะ WHERE ด้วย id: 1,2,3,4,5,6 หรือ WHERE ด้วย code : A1, A2, A3
- ถ้าบอกว่า code นับมามากกว่า 1 ให้บอกว่าซ้ำเลย งั้นก็ select count(code) from abc where code='A1'
- ใน code บรรทัด 12,43 เห็น if InsertProduct(xx1,xx2,xx3,xx4,xx5) else แสดงข้อมูลซ้ำตอนเพิ่มข้อมูล แล้วเกี่ยวอะไรกับ update
ข้อ2) 2. ตอน update ถ้าเราไม่ใส่อะไรเลยในช่อง max จะทำให้ หมายถึง NULL ค่ะ
Dim strmaxP As String = CType(GridView_promotion.Rows(e.RowIndex).Cells(5).Controls(0), TextBox).Text
If (String.IsNullOrEmpty(strmaxP)) Then
cmd.Parameters.Add("@p_max", System.DBNull.Value)
else
cmd.Parameters.Add("@p_max", OleDbType.Decimal).Value = Decimal.Parse(strmaxP)
end if