Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtRMRT_LotNo.Text = Format(Now.Date.ToString("ddMMyyyy", New CultureInfo("en-US"))) & "-"
End Sub
Private Sub AddToDB()
ConnectDB()
objConn.Open()
strSQL = "INSERT INTO RawMaterialReceiveTest (RMRT_LOT) VALUES ('" & txtRMRT_LotNo.Text & "')"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
objConn.Close()
objConn = Nothing
MsgBox("บันทึกข้อมูลสำเร็จ")
Else
MsgBox("ข้อมูลเต็ม")
End If
End Sub
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 RMRT01
Inherits System.Web.UI.Page
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strConnString, strSQL, RMRTID, LotNo As String
Dim dtAdapter As SqlDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Dim result, num, iMAX, Count As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadDropDownRMT()
LoadDropDownUnit()
End If
txtRMRT_ReceiveDate.Text = Format(Now.Date.ToString("dd/MM/yyyy", New CultureInfo("en-US")))
txtRMRT_LotNo.Text = Format(Now.Date.ToString("ddMMyyyy", New CultureInfo("en-US"))) & "-"
End Sub
Public Sub ConnectDB()
strConnString = ConfigurationManager.ConnectionStrings("con").ConnectionString
objConn = New SqlConnection(strConnString)
End Sub
Private Sub LoadDropDownRMT()
Try
ConnectDB()
ddlRMRT_LoadRMT.AppendDataBoundItems = True
strSQL = "Select RMT_RKEY,RMT_NAME From RawMaterialType ORDER BY RMT_NAME ASC"
dtAdapter = New SqlDataAdapter(strSQL, objConn)
ds = New DataSet
objConn.Open()
dtAdapter.Fill(ds)
objConn.Close()
ddlRMRT_LoadRMT.DataSource = ds
ddlRMRT_LoadRMT.DataTextField = "RMT_NAME"
ddlRMRT_LoadRMT.DataValueField = "RMT_RKEY"
ddlRMRT_LoadRMT.DataBind()
ddlRMRT_LoadRMT.Items.Insert(0, New ListItem("กรุณาเลือกประเภทวัตถุดิบ", "0"))
ddlRMRT_LoadRM.Items.Insert(0, New ListItem("กรุณาเลือกวัตถุดิบ", "0"))
ddlRMRT_LoadRMT.SelectedIndex = ddlRMRT_LoadRMT.Items.IndexOf(ddlRMRT_LoadRMT.Items.FindByValue("0"))
ddlRMRT_LoadRM.SelectedIndex = ddlRMRT_LoadRM.Items.IndexOf(ddlRMRT_LoadRM.Items.FindByValue("0"))
ddlRMRT_LoadRM.Enabled = False
Catch ex As Exception
lblMsg01.Text = "Error 01 : " + ex.Message
End Try
End Sub
Private Sub ddlRMRT_LoadRMT_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlRMRT_LoadRMT.SelectedIndexChanged
Try
ConnectDB()
ddlRMRT_LoadRMT.AppendDataBoundItems = True
strSQL = "SELECT RM_ID,RM_NAME FROM RawMaterial WHERE RMT_RKEY=@RMT_RKEY ORDER BY RM_NAME ASC"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.Parameters.AddWithValue("@RMT_RKEY", ddlRMRT_LoadRMT.SelectedItem.Value)
objConn.Open()
ddlRMRT_LoadRM.DataSource = objCmd.ExecuteReader()
ddlRMRT_LoadRM.DataTextField = "RM_NAME"
ddlRMRT_LoadRM.DataValueField = "RM_ID"
ddlRMRT_LoadRM.DataBind()
ddlRMRT_LoadRM.Items.Insert(0, New ListItem("กรุณาเลือกวัตถุดิบ", "0"))
ddlRMRT_LoadRM.SelectedIndex = ddlRMRT_LoadRM.Items.IndexOf(ddlRMRT_LoadRM.Items.FindByValue("0"))
If ddlRMRT_LoadRM.Items.Count > 1 Then
ddlRMRT_LoadRM.Enabled = True
Else
ddlRMRT_LoadRM.Enabled = False
txtRMRT_PDD.Text = String.Empty
txtRMRT_SP.Text = String.Empty
End If
Catch ex As Exception
lblMsg01.Text = "Error 01 : " + ex.Message
Finally
objConn.Close()
End Try
End Sub
Private Sub ddlRMRT_LoadRM_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlRMRT_LoadRM.SelectedIndexChanged
Try
ConnectDB()
strSQL = "SELECT RM.RM_ID, RM.RM_NAME, RM.PDD_RKEY, RM.SP_RKEY, PDD.PDD_RKEY AS Expr1, PDD.PDD_NAME, SP.SP_RKEY AS Expr2, SP.SP_NAME
FROM RawMaterial AS RM
INNER JOIN ProductDescription AS PDD ON PDD.PDD_RKEY = RM.PDD_RKEY
INNER JOIN Supplier AS SP ON SP.SP_RKEY = RM.SP_RKEY
WHERE RM_ID=@RM_ID"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.Parameters.AddWithValue("@RM_ID", ddlRMRT_LoadRM.SelectedItem.Value)
objConn.Open()
Dim sdr As SqlDataReader = objCmd.ExecuteReader()
While sdr.Read()
txtRMRT_PDD.Enabled = True
txtRMRT_SP.Enabled = True
txtRMRT_PDD.Text = sdr("PDD_NAME").ToString()
txtRMRT_SP.Text = sdr("SP_NAME").ToString()
End While
Catch ex As Exception
Throw ex
Finally
objConn.Close()
End Try
End Sub
Private Sub LoadDropDownUnit()
Try
ConnectDB()
ddlRMRT_UN.AppendDataBoundItems = True
strSQL = "SELECT UN_RKEY, UN_NAME FROM UNIT ORDER BY UN_NAME ASC"
dtAdapter = New SqlDataAdapter(strSQL, objConn)
ds = New DataSet
objConn.Open()
dtAdapter.Fill(ds)
objConn.Close()
ddlRMRT_UN.DataSource = ds
ddlRMRT_UN.DataTextField = "UN_NAME"
ddlRMRT_UN.DataValueField = "UN_RKEY"
ddlRMRT_UN.DataBind()
ddlRMRT_UN.Items.Insert(0, New ListItem("กรุณาเลือกหน่วยนับวัตถุดิบ", "0"))
ddlRMRT_UN.SelectedIndex = ddlRMRT_UN.Items.IndexOf(ddlRMRT_UN.Items.FindByValue("0"))
Catch ex As Exception
lblMsg01.Text = "Error 01 : " + ex.Message
End Try
End Sub
Private Sub AddToDB()
ConnectDB()
objConn.Open()
strSQL = "SELECT ISNULL(MAX(RIGHT(RMRT_ID,3)),0) As RMRT_ID FROM RawMaterialReceiveTest"
objCmd = New SqlCommand(strSQL, objConn)
Count = objCmd.ExecuteScalar()
iMAX = IIf(Count <= 0, 1, Count + 1)
If iMAX < 1000 Then
Session("RMRTID") = "RMRT" & CStr(Format(Now.Date.ToString("yyyyMMdd", New CultureInfo("en-US")))) & iMAX.ToString("000")
strSQL = "INSERT INTO RawMaterialReceiveTest (RMRT_ID, RMRT_LOT) VALUES ('" & Session("RMRTID").ToString() & "','" & txtRMRT_LotNo.Text.Trim & "')"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.Parameters.Add("@RMRT_RECEIVEDATE", SqlDbType.Date).Value = Format(Now.Date.ToString("dd-MM-yyyy", New CultureInfo("en-US")))
objCmd.ExecuteNonQuery()
objConn.Close()
objConn = Nothing
MsgBox("บันทึกข้อมูลสำเร็จ")
Else
MsgBox("ข้อมูลเต็ม")
End If
End Sub
Protected Sub btnRMRT_Save_Click(sender As Object, e As EventArgs) Handles btnRMRT_Save.Click
AddToDB()
End Sub
Protected Sub btnRMRT_Cancel_Click(sender As Object, e As EventArgs) Handles btnRMRT_Cancel.Click
End Sub
End Class