จะ Add ค่าลง DataTable ก่อน เมื่อ Add ค่าตามที่ต้องการแล้วจะ Save ลง DB
Code (VB.NET)
Imports System.Data.SqlClient
Public Class Manage_Package
Inherits System.Web.UI.Page
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strConnString As String
Dim dt As DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
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)
gvST_LoadSizeTest.DataSource = dt
gvST_LoadSizeTest.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 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
End Class
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET, VS 2012 (.NET 4.x)
dt = DirectCast(Session("SizeTable"), DataTable)
strSql="Insert into TableName(F1,F2,F3,F4) Values(@f1,@f2,@f3,@f4) "
objConn.Open
objCmd= New Sqlcommand(strSql,objConn)
For Each dr As Datarow in dt.Rows
with objCmd
คำสั่งเพิ่ม parameter T_T
End with
objCmd.ExecuteNonQuery
objCmd.Parameters.Clear
Next
Code นี้ Error : {"Column 'S_WIDTHTOP' does not belong to table ."} แบบนี้ครับ
Package table
Size table
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 (S_WIDTHTOP,S_WIDTHMID,S_WIDTHBOTTOM,S_LENGTHLEFT,S_LENGTHRIGTH) " &
" VALUES " &
" ('" & dt.Rows(i)("S_WIDTHTOP") & "','" & dt.Rows(i)("S_WIDTHMID") & "','" & dt.Rows(i)("S_WIDTHBOTTOM") & "','" & dt.Rows(i)("S_LENGTHLEFT") & "','" & dt.Rows(i)("S_LENGTHRIGTH") & "')"
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
Package เป็นตารางหลังโดยมี Size เป็นส่วนประกอบ
คือ ผมต้องการนำ ID ของ Package ที่เป็น PK ในตารางตัวมันเอง
แต่เป็น FK ในตาราง Size
แล้วผมจะบันทึก ID ของ Package ลงตาราง Size ยังไงครับ
Package table
Size table
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