 |
|
ถ้า AutoID ผมเก็บจนถึง ID สุดท้าย จะให้แจ้งเตือนความ "ความจุเต็ม" ต้องกำหนดยังไงครับ |
|
 |
|
|
 |
 |
|
น่าจะเพิ่มเงื่อนไขตรงนี้รึป่าวครับ
Code (VB.NET)
If IsDBNull(Count) = True Then
txtRM_AddID.Text = "RM" & "000" & iMAX
Else If iMAX< ค่า ความจุเต็ม Then
iMAX = Count + 1
Else
"ค่า ความจุเต็ม"
End If
ส่วน
Code (VB.NET)
intNum = "RM" & iMAX
แนะนำให้เขียนแบบนี้นะครับ
Code (VB.NET)
intNum = iMAX.ToString("RM0000")
แทน
Code (VB.NET)
If iMAX <= 9 Then
intNum = "RM" & "000" & iMAX
ElseIf iMAX <= 99 Then
intNum = "RM" & "00" & iMAX
ElseIf iMAX <= 999 Then
intNum = "RM" & "0" & iMAX
ElseIf iMAX <= 9999 Then
intNum = "RM" & iMAX
End If
แต่ถ้าถนัดแบบนี้ก็ไม่เป็นไรนะครับ
|
 |
 |
 |
 |
Date :
2017-02-04 14:57:00 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับผม
แต่ผมยังไม่เข้าใจ ^^
ช่วยเขียนโค้ดที่คุณ lamaka.tor บอก
แทนที่โค้ดของผม แล้ว comment สะดวกหรือเปล่าครับ
|
 |
 |
 |
 |
Date :
2017-02-04 15:20:32 |
By :
NUTKA |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าหมายถึง
Code (VB.NET)
intNum = iMAX.ToString("RM0000")
เรา input 55 ก็จะได้ RM0055
เรา input 555 ก็จะได้ RM0555
เรา input 5555 ก็จะได้ RM5555
เท่ากับที่เราใช้โค้ด
Code (VB.NET)
If iMAX <= 9 Then
intNum = "RM" & "000" & iMAX
ElseIf iMAX <= 99 Then
intNum = "RM" & "00" & iMAX
ElseIf iMAX <= 999 Then
intNum = "RM" & "0" & iMAX
ElseIf iMAX <= 9999 Then
intNum = "RM" & iMAX
End If
แต่เขียนแค่ 1 บรรทัด
|
 |
 |
 |
 |
Date :
2017-02-04 15:34:35 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณอย่างสุดซึ้งครับ ทำให้ผมได้ทักษะเพิ่มมา
แต่ยังติดอยู่ที่ ถ้าข้อมูล สุดที่ 9999 แล้วให้มีข้อความแจ้งเตือนครับ
|
 |
 |
 |
 |
Date :
2017-02-04 16:03:38 |
By :
NUTKA |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
If IsDBNull(Count) = True Then
txtRM_AddID.Text = "RM" & "000" & iMAX
Else If iMAX< 9999 Then
iMAX = Count + 1
Else
"ค่า ความจุเต็ม"
End If
|
 |
 |
 |
 |
Date :
2017-02-04 16:07:55 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
If IsDBNull(Count) = True Then
txtRM_AddID.Text = "RM" & "000" & iMAX
ElseIf iMAX < 9999 Then
iMAX = Count + 1
Else
lblMsg.Text = "ค่า ความจุเต็ม"
End If
กรณีนี้ตอนที่ข้อมูลอยู่ที่ RM9998 เพิ่มขึ้น 1 = RM9999
พอผมลองกดเพิ่มอีก 1 มันยังเข้า ขั้นตอนการ Add ครับ
แล้วก็ error
Additional information: Violation of PRIMARY KEY constraint 'PK_RawMaterial_New'. Cannot insert duplicate key in object 'dbo.RawMaterial_New'.
View Detail : "Violation of PRIMARY KEY constraint 'PK_RawMaterial_New'. Cannot insert duplicate key in object 'dbo.RawMaterial_New'." & vbCrLf & "The statement has been terminated."
|
ประวัติการแก้ไข 2017-02-04 16:17:32
 |
 |
 |
 |
Date :
2017-02-04 16:14:56 |
By :
NUTKA |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
insert duplicate key
Code (VB.NET)
If IsDBNull(Count) = True Then
txtRM_AddID.Text = "RM" & "000" & iMAX
ElseIf iMAX <= 9999 Then
iMAX = Count + 1
Else
lblMsg.Text = "ค่า ความจุเต็ม"
'ก็ต้องออกไปด้วยครับ จะ exit sun , close ,return หรืออะไรก็ว่ากันไป
'สรุป คือ คุณหยุดแค่นี้นะเพราะมันเกิน 9999 ไปแล้วประมาณนี้ครับ
End If
|
 |
 |
 |
 |
Date :
2017-02-04 16:54:03 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมมาลองเขียนโค้ดเพื่อให้สั้นลงน่าจะประมาณนี้นะครับ
Code (VB.NET)
Function SQLExecute(_sql As String) As Integer
Dim objConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString())
objConn.Open()
Dim objCmd As SqlCommand = New SqlCommand(strSQL, objConn)
Dim Count As Integer= objCmd.ExecuteScalar()
objConn.Close()
End Function
Private Sub AddAutoID()
Dim Count As Integer = SQLExecute("SELECT MAX(RIGHT(RM_ID,4)) AS RM_ID FROM RawMaterial_New")
Dim iMAX As Integer = IIf(Count <= 0, 1, Count + 1)
If iMAX < 10000 Then
If SQLExecute(strSQL = "INSERT INTO RawMaterial_New (RM_ID) VALUES ('" & (IIf(Count <= 0, 1, Count + 1)).ToString("RM0000") & "')") > 0 Then
MsgBox("บันทึกสำเร็จ")
Else
MsgBox("บันทึกไม่สำเร็จ เศร้าใจสิ้นดี")
End If
Else
MsgBox("มันเกิน 9999 ไปแล้วนะ")
End If
End Sub
ตัด KeyStore เพิ่ม SQLExecute เข้าไปใหม่ น่าจะกระชับขึ้น
|
 |
 |
 |
 |
Date :
2017-02-04 17:23:11 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลองนำมาปรับใช้ พอผมลบข้อมูลออกจาก DB ทั้งหมด แล้วเพิ่มข้อมูลเข้าไปใหม่ตั้งแต่แรก
มัน error แบบนี้ครับ ==> Additional information: Conversion from type 'DBNull' to type 'Integer' is not valid.
Code (VB.NET)
Imports System.Data.SqlClient
Public Class Manage_RawMaterial_New
Inherits System.Web.UI.Page
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strConnString, strSQL As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Sub ConnectDB()
strConnString = ConfigurationManager.ConnectionStrings("con").ConnectionString
objConn = New SqlConnection(strConnString)
End Sub
Private Sub AddAutoID()
Dim intNum As String = Nothing
Dim Count As Integer
ConnectDB()
strSQL = "SELECT MAX(RIGHT(RM_ID,4)) AS RM_ID FROM RawMaterial_New"
objCmd = New SqlCommand(strSQL, objConn)
objConn.Open()
Count = objCmd.ExecuteScalar()
objConn.Close()
Dim iMAX As Integer = IIf(Count <= 0, 1, Count + 1)
If iMAX < 10000 Then
Dim result As Integer
strSQL = "INSERT INTO RawMaterial_New (RM_ID) VALUES ('" & iMAX.ToString("RM0000") & "')"
objCmd = New SqlCommand(strSQL, objConn)
objConn.Open()
result = objCmd.ExecuteNonQuery()
objConn.Close()
MsgBox("บันทึกสำเร็จ")
Else
MsgBox("ข้อมูลเต็ม")
End If
End Sub
Protected Sub btnRM_Add_Click(sender As Object, e As EventArgs) Handles btnRM_Add.Click
AddAutoID()
End Sub
End Class
|
 |
 |
 |
 |
Date :
2017-02-06 09:29:28 |
By :
NUTKA |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เผื่อจะมีประโยชน์ครับ
.NET สร้าง Auto ID Number ใช้สำหรับ Order ID หรือ Member ID รหัสลำดับต่างๆ โดยมีอักษร (VB.Net, C#)
|
 |
 |
 |
 |
Date :
2017-02-06 10:40:16 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|