จะเก็บข้อมูลของพนักงาน โดยที่มี Database คือ Personal กับ Position
ในตาราง Personal จะมีฟิลด์ Position_id เป็น FK เพื่อที่จะเชื่อมโยงกันว่าพนักงานคนใด ทำตำแหน่งใด
Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Public Class Record_Per
Inherits System.Web.UI.Page
Dim srtconn As String
Dim position As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
srtconn = "Data Source=PRO7_ALL;Initial Catalog=SRC;Integrated Security=True"
Dim conn As New SqlConnection(srtconn)
conn.Open()
Dim sb As New StringBuilder()
sb.Append("SELECT TOP 1 per_id FROM Personal ORDER BY per_id Desc")
Dim sqlSave = sb.ToString()
Dim Lastper_id As String = ""
Dim com As New SqlCommand()
Dim dr As SqlDataReader
With com
.Connection = conn
.CommandType = CommandType.Text
.CommandText = sqlSave
dr = .ExecuteReader()
If dr.HasRows Then
dr.Read()
Dim Currentper_id = dr.GetString(0)
Dim Running As Integer = Convert.ToInt32(Currentper_id.Substring(1, Currentper_id.Length - 1))
Running += 1
Lastper_id = "P" & Running.ToString("0000")
Else
Lastper_id = "P0001"
End If
End With
dr.Close()
txtperid.Text = Lastper_id
conn.Close()
End Sub
Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnsave.Click
SqlDataSource1.InsertParameters("per_id").DefaultValue = txtperid.Text
SqlDataSource1.InsertParameters("per_email").DefaultValue = txtpermail.Text
SqlDataSource1.InsertParameters("per_password").DefaultValue = txtperpass.Text
SqlDataSource1.InsertParameters("per_name").DefaultValue = txtpername.Text
SqlDataSource1.InsertParameters("position_id").DefaultValue = RadioButton1.Text
SqlDataSource1.InsertParameters("position_id").DefaultValue = RadioButton2.Text
SqlDataSource1.InsertParameters("position_id").DefaultValue = RadioButton3.Text
SqlDataSource1.Insert()
cleardata()
Response.Redirect("Record_Per.aspx")
End Sub
Private Sub cleardata()
txtperid.Text = ""
txtpermail.Text = ""
txtperpass.Text = ""
txtpername.Text = ""
RadioButton1.Text = ""
RadioButton2.Text = ""
RadioButton3.Text = ""
End Sub
Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton1.CheckedChanged
RadioButton1.Checked = True
'position = "1"
RadioButton1.Text = "1"
End Sub
Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton2.CheckedChanged
RadioButton2.Checked = True
'position = "2"
RadioButton2.Text = "2"
End Sub
Protected Sub RadioButton3_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton3.CheckedChanged
RadioButton3.Checked = True
'position = "3"
RadioButton3.Text = "3"
End Sub
End Class
Tag : ASP, Ms SQL Server 2008, VBScript, Web (ASP.NET), Windows