ตอนนี้เก็บเป็น ID ของ Package และ Size เก็บเป็น Identity ครับ
ID ของ Package ที่เป็น Identity ไปเก็บเป็น FK ของตาราง Size อย่างไรครับ
ตาราง Package มี ID เป็น PK
ตาราง Size มี FK เป็น ID ของ Package
Code (VB.NET)
Imports System.Data.SqlClient
Imports System.Globalization
Public Class Manage_Package
Inherits System.Web.UI.Page
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strConnString, strSQL As String
Dim dtAdapter As SqlDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Dim result As Integer
Dim TopWidth As Decimal
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
LoadSizeData()
End If
lblPK_AddReceiveDate.Text = Format(Now.Date.ToString("dd/MM/yyyy", New CultureInfo("en-US")))
End Sub
Public Sub ConnectDB()
strConnString = ConfigurationManager.ConnectionStrings("con").ConnectionString
objConn = New SqlConnection(strConnString)
End Sub
Private Sub LoadSizeData()
If IsNothing(Session("SizeTable")) Then
dt = New DataTable()
dt.Columns.Add(New DataColumn("TopWidth", GetType(String)))
dt.Columns.Add(New DataColumn("MidWidth", GetType(String)))
dt.Columns.Add(New DataColumn("BottomWidth", GetType(String)))
dt.Columns.Add(New DataColumn("WidthAVG", GetType(String)))
dt.Columns.Add(New DataColumn("LeftLength", GetType(String)))
dt.Columns.Add(New DataColumn("RigthLength", GetType(String)))
dt.Columns.Add(New DataColumn("LengthAVG", GetType(String)))
Session("SizeTable") = dt
End If
dt = DirectCast(Session("SizeTable"), DataTable)
gvS_LoadSize.DataSource = dt
gvS_LoadSize.DataBind()
End Sub
Private Sub AddToTable()
Dim dr As DataRow = dt.NewRow
dr("TopWidth") = txtPK_WidthTop.Text.Trim
dr("MidWidth") = txtPK_WidthMid.Text.Trim
dr("BottomWidth") = txtPK_WidthBottom.Text.Trim
dr("WidthAVG") = ((CType(txtPK_WidthTop.Text.Trim, Decimal) + CType(txtPK_WidthMid.Text.Trim, Decimal) + CType(txtPK_WidthBottom.Text.Trim, Decimal)) / 3).ToString("n2")
dr("LeftLength") = txtPK_LengthLeft.Text.Trim
dr("RigthLength") = txtPK_LengthRigth.Text.Trim
dr("LengthAVG") = ((CType(txtPK_LengthLeft.Text.Trim, Decimal) + CType(txtPK_LengthRigth.Text.Trim, Decimal)) / 2).ToString("n2")
dt.Rows.Add(dr)
Session("SizeTable") = dt
LoadSizeData()
Reset()
End Sub
Private Sub AddToDB()
ConnectDB()
objConn.Open()
strSQL = "INSERT INTO Package (PK_RECEIVEDATE) " &
" VALUES " &
" (@PK_RECEIVEDATE)"
Dim objCmd As New SqlCommand(strSQL, objConn)
objCmd.Parameters.Add("@PK_RECEIVEDATE", SqlDbType.Date).Value = Format(Now.Date.ToString("dd-MM-yyyy", New CultureInfo("en-US")))
objCmd.ExecuteNonQuery()
dt = DirectCast(Session("SizeTable"), DataTable)
For i = 0 To dt.Rows.Count - 1
strSQL = "INSERT INTO Size (PK_ID,S_WIDTHTOP,S_WIDTHMID,S_WIDTHBOTTOM,S_LENGTHLEFT,S_LENGTHRIGTH) " &
" VALUES " &
" ('" & dt.Rows(i)("TopWidth") & "','" & dt.Rows(i)("MidWidth") & "','" & dt.Rows(i)("BottomWidth") & "','" & dt.Rows(i)("LeftLength") & "','" & dt.Rows(i)("RigthLength") & "')"
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
.ExecuteNonQuery()
End With
Next
objConn.Close()
objConn = Nothing
End Sub
Private Sub Reset()
txtPK_WidthTop.Text = String.Empty
txtPK_WidthMid.Text = String.Empty
txtPK_WidthBottom.Text = String.Empty
txtPK_LengthLeft.Text = String.Empty
txtPK_LengthRigth.Text = String.Empty
End Sub
Private Sub ResetAll()
Reset()
Response.Redirect("~/Manage_Package.aspx")
End Sub
Protected Sub btnPK_AddTo_Click(sender As Object, e As EventArgs) Handles btnPK_AddTo.Click
AddToTable()
End Sub
Protected Sub btnPK_Cancel_Click(sender As Object, e As EventArgs) Handles btnPK_Cancel.Click
ResetAll()
End Sub
Protected Sub btnPK_Save_Click(sender As Object, e As EventArgs) Handles btnPK_Save.Click
AddToDB()
End Sub
End Class
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET, VS 2012 (.NET 4.x)
Imports System.Data.SqlClient
Imports System.Globalization
Public Class Test_Package
Inherits System.Web.UI.Page
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strConnString, strSQL As String
Dim dtAdapter As SqlDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Dim result, num, iMAX, Count As Integer
Dim TopWidth As Decimal
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblPK_AddReceiveDate.Text = Format(Now.Date.ToString("dd/MM/yyyy", New CultureInfo("en-US")))
If IsPostBack Then
LoadSizeData()
End If
End Sub
Public Sub ConnectDB()
strConnString = ConfigurationManager.ConnectionStrings("con").ConnectionString
objConn = New SqlConnection(strConnString)
End Sub
Private Sub LoadSizeData()
If IsNothing(Session("SizeTable")) Then
dt = New DataTable()
dt.Columns.Add(New DataColumn("TopWidth", GetType(String)))
dt.Columns.Add(New DataColumn("MidWidth", GetType(String)))
dt.Columns.Add(New DataColumn("BottomWidth", GetType(String)))
dt.Columns.Add(New DataColumn("WidthAVG", GetType(String)))
dt.Columns.Add(New DataColumn("LeftLength", GetType(String)))
dt.Columns.Add(New DataColumn("RigthLength", GetType(String)))
dt.Columns.Add(New DataColumn("LengthAVG", GetType(String)))
Session("SizeTable") = dt
End If
dt = DirectCast(Session("SizeTable"), DataTable)
gvS_LoadSize.DataSource = dt
gvS_LoadSize.DataBind()
End Sub
Private Sub AddToTable()
If txtPK_WidthTop.Text.Trim <> String.Empty And txtPK_WidthMid.Text.Trim <> String.Empty And txtPK_WidthBottom.Text.Trim <> String.Empty And txtPK_LengthLeft.Text.Trim <> String.Empty And txtPK_LengthRigth.Text.Trim <> String.Empty Then
Dim dr As DataRow = dt.NewRow
'TopWidth = txtPK_WidthTop.Text.Trim
'dr("TopWidth") = TopWidth
dr("TopWidth") = txtPK_WidthTop.Text.Trim
dr("MidWidth") = txtPK_WidthMid.Text.Trim
dr("BottomWidth") = txtPK_WidthBottom.Text.Trim
'dr("WidthAVG") = ((CType(txtPK_WidthTop.Text.Trim, Integer) + CType(txtPK_WidthMid.Text.Trim, Integer) + CType(txtPK_WidthBottom.Text.Trim, Integer)) / 3).ToString("#,##0.00")
'dr("WidthAVG") = ((CType(txtPK_WidthTop.Text.Trim, Integer) + CType(txtPK_WidthMid.Text.Trim, Integer) + CType(txtPK_WidthBottom.Text.Trim, Integer)) / 3).ToString("#0.00")
dr("WidthAVG") = ((CType(txtPK_WidthTop.Text.Trim, Decimal) + CType(txtPK_WidthMid.Text.Trim, Decimal) + CType(txtPK_WidthBottom.Text.Trim, Decimal)) / 3).ToString("n2")
dr("LeftLength") = txtPK_LengthLeft.Text.Trim
dr("RigthLength") = txtPK_LengthRigth.Text.Trim
'dr("LengthAVG") = ((CType(txtPK_LengthLeft.Text.Trim, Integer) + CType(txtPK_LengthRigth.Text.Trim, Integer)) / 2).ToString("#,##0.00")
'dr("LengthAVG") = ((CType(txtPK_LengthLeft.Text.Trim, Integer) + CType(txtPK_LengthRigth.Text.Trim, Integer)) / 2).ToString("#0.00")
dr("LengthAVG") = ((CType(txtPK_LengthLeft.Text.Trim, Decimal) + CType(txtPK_LengthRigth.Text.Trim, Decimal)) / 2).ToString("n2")
dt.Rows.Add(dr)
Session("SizeTable") = dt
LoadSizeData()
Reset()
ElseIf txtPK_WidthTop.Text.Trim = String.Empty And txtPK_WidthMid.Text.Trim <> String.Empty And txtPK_WidthBottom.Text.Trim <> String.Empty And txtPK_LengthLeft.Text.Trim <> String.Empty And txtPK_LengthRigth.Text.Trim <> String.Empty Then
MsgBox("กรุณากรอกความกว้างส่วนบน")
txtPK_WidthTop.Focus()
ElseIf txtPK_WidthTop.Text.Trim <> String.Empty And txtPK_WidthMid.Text.Trim = String.Empty And txtPK_WidthBottom.Text.Trim <> String.Empty And txtPK_LengthLeft.Text.Trim <> String.Empty And txtPK_LengthRigth.Text.Trim <> String.Empty Then
MsgBox("กรุณากรอกความกว้างส่วนกลาง")
txtPK_WidthMid.Focus()
ElseIf txtPK_WidthTop.Text.Trim <> String.Empty And txtPK_WidthMid.Text.Trim <> String.Empty And txtPK_WidthBottom.Text.Trim = String.Empty And txtPK_LengthLeft.Text.Trim <> String.Empty And txtPK_LengthRigth.Text.Trim <> String.Empty Then
MsgBox("กรุณากรอกความกว้างส่วนล่าง")
txtPK_WidthBottom.Focus()
ElseIf txtPK_WidthTop.Text.Trim <> String.Empty And txtPK_WidthMid.Text.Trim <> String.Empty And txtPK_WidthBottom.Text.Trim <> String.Empty And txtPK_LengthLeft.Text.Trim = String.Empty And txtPK_LengthRigth.Text.Trim <> String.Empty Then
MsgBox("กรุณากรอกความยาวด้านซ้าย")
txtPK_LengthLeft.Focus()
ElseIf txtPK_WidthTop.Text.Trim <> String.Empty And txtPK_WidthMid.Text.Trim <> String.Empty And txtPK_WidthBottom.Text.Trim <> String.Empty And txtPK_LengthLeft.Text.Trim <> String.Empty And txtPK_LengthRigth.Text.Trim = String.Empty Then
MsgBox("กรุณากรอกความยาวด้านขวา")
txtPK_LengthRigth.Focus()
Else
MsgBox("กรุณากรอกข้อมูลให้ครบถ้วน")
txtPK_WidthTop.Focus()
End If
End Sub
Protected Sub btnPK_Add_Click(sender As Object, e As EventArgs) Handles btnPK_Add.Click
'AddToTable()
AddTODB()
End Sub
Private Sub AddTODB()
Try
ConnectDB()
objConn.Open()
strSQL = "SELECT ISNULL(MAX(RIGHT(PK_ID,2)),0) As PK_ID FROM Package"
objCmd = New SqlCommand(strSQL, objConn)
Count = objCmd.ExecuteScalar()
iMAX = IIf(Count <= 0, 1, Count + 1)
If iMAX < 100 Then
strSQL = "INSERT INTO Package (PK_ID, PK_RECEIVEDATE) VALUES ('" & "PK" & CStr(Format(Now.Date.ToString("yyyyMMdd", New CultureInfo("en-US")))) & iMAX.ToString("00") & "',@PK_RECEIVEDATE)"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.Parameters.Add("@PK_RECEIVEDATE", SqlDbType.Date).Value = Format(Now.Date.ToString("dd-MM-yyyy", New CultureInfo("en-US")))
objCmd.ExecuteNonQuery()
'ResetAll()
'GridViewRawMaterial()
'MsgBox("เพิ่มวัตถุดิบสำเร็จ")
Else
MsgBox("ข้อมูลเต็ม")
End If
strSQL = "SELECT ISNULL(MAX(RIGHT(S_ID,2)),0) As S_ID FROM Size"
objCmd = New SqlCommand(strSQL, objConn)
Count = objCmd.ExecuteScalar()
iMAX = IIf(Count <= 0, 1, Count + 1)
If iMAX < 100 Then
strSQL = "INSERT INTO Size (S_ID) VALUES ('" & "S" & CStr(Format(Now.Date.ToString("yyyyMMdd", New CultureInfo("en-US")))) & iMAX.ToString("00") & "')"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
'ResetAll()
'GridViewRawMaterial()
MsgBox("เพิ่มวัตถุดิบสำเร็จ")
Else
MsgBox("ข้อมูลเต็ม")
End If
objConn.Close()
Catch ex As Exception
Response.Write("Error : " + ex.Message)
End Try
End Sub
Protected Sub btnPK_Save_Click(sender As Object, e As EventArgs) Handles btnPK_Save.Click
AddTODB()
End Sub
End Class