## ขึ้น err ว่า
#Cannot insert the value NULL into column 'Id', table 'ibank.dbo.tb_complaint_User'; column does not allow nulls. INSERT fails.
The statement has been terminated.#
Code (VB.NET)
Imports System.Data
Imports System.IO
Partial Class User
Inherits System.Web.UI.Page
Public ObjDb As New Datacenter.Data.clsDBSQLBased(ConfigurationManager.ConnectionStrings("ConStr").ToString)
Dim GetPath As String
Dim fFolder, fName, ServerPath As String
Private Property AssignDate As String
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
'If Session("IDUser") Is Nothing Or Session("IDUser") = "" Then
' Response.Redirect("Login.aspx")
'End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
bindPosition()
bindSLevel()
bindtitle()
If Me.ddlTypeBr.SelectedIndex = 0 Then
Me.ddlBranch.Visible = False
Me.lbBRType.Visible = False
Else
Me.ddlBranch.Visible = True
Me.lbBRType.Visible = True
bindBranch()
End If
End If
End Sub
Protected Sub bindtitle()
Dim sql As String
Dim dv As DataView
sql = "Select Id,Detail FROM tb_complaint_Title "
dv = ObjDb.GetDataSet(sql).Tables(0).DefaultView
If dv.Count > 0 Then
Me.ddltitle.DataSource = dv
Me.ddltitle.DataBind()
Me.ddltitle.Items.Insert(0, "= กรุณาเลือก =")
End If
End Sub
Protected Sub bindPosition()
Dim sql As String
Dim dv As DataView
sql = "SELECT id,detail FROM tb_complaint_Position "
'sql += " ORDER BY Sort_Id "
dv = ObjDb.GetDataSet(sql).Tables(0).DefaultView
If dv.Count > 0 Then
Me.ddlPosition.DataSource = dv
Me.ddlPosition.DataBind()
Me.ddlPosition.Items.Insert(0, "== กรุณาเลือก ==")
End If
End Sub
Protected Sub bindBranch()
Dim sql As String
Dim dv As DataView
sql = "SELECT id,detail FROM tb_complaint_branch "
sql += " where 1=1"
If Me.ddlTypeBr.SelectedValue = "1" Then
sql += " and TypeBr = '1' "
Me.lbBRType.Text = "ฝาย :"
ElseIf Me.ddlTypeBr.SelectedValue = "2" Then
sql += " and TypeBr = '2' "
Me.lbBRType.Text = "สาขา :"
End If
sql += " ORDER BY ID"
dv = ObjDb.GetDataSet(sql).Tables(0).DefaultView
If dv.Count > 0 Then
Me.ddlBranch.DataSource = dv
Me.ddlBranch.DataBind()
Me.ddlBranch.Items.Insert(0, "== กรุณาเลือก ==")
End If
End Sub
Protected Sub bindSLevel()
Dim sql As String
Dim dv As DataView
sql = "SELECT id,detail FROM tb_complaint_SLevel "
'sql += " ORDER BY Sort_Id "
dv = ObjDb.GetDataSet(sql).Tables(0).DefaultView
If dv.Count > 0 Then
Me.ddlSLevel.DataSource = dv
Me.ddlSLevel.DataBind()
Me.ddlSLevel.Items.Insert(0, "== กรุณาเลือก ==")
End If
End Sub
Protected Sub SaveData()
Dim sql As String
Dim ddltitle As String
Dim Name As String
Dim LastName As String
Dim Email As String
Dim Tel As Integer
Dim ddlTypeBr As String
Dim ddlPosition As Integer
Dim ddlBranch As Integer
Dim CreatDate As String
Dim EditDate As String
Dim ddlSLevel As String
Dim myReader As StreamReader
CreatDate = Format(cldCreatDate.SelectedDate.Date, "dd/MM/yyyy")
EditDate = Format(cldEditDate.SelectedDate.Date, "dd/MM/yyyy")
sql = "INSERT INTO tb_complaint_User(title,Name,Lastname,Email,Tel,TypeBr,Position,Branch,CreatDate,EditDate,SLevel)"
sql += " VALUES ('" & Trim(Me.ddltitle.SelectedValue) & "','" & Trim(Me.txtName.Text) & "','" & Trim(Me.txtLastName.Text) & "','" & Trim(Me.txtEmail.Text) & "','" & Trim(Me.txtTel.Text) & "',"
sql += " '" & Trim(Me.ddlTypeBr.SelectedValue) & "','" & Trim(Me.ddlPosition.SelectedValue) & "','" & Trim(Me.ddlBranch.SelectedValue) & "','" & CreatDate & "','" & EditDate & "','" & Trim(Me.ddlSLevel.SelectedValue) & "') "
Response.Write(sql)
ObjDb.ExecuteNonQuery(sql)
Page.ClientScript.RegisterStartupScript(Page.GetType(), "clientScript", "alert('บันทึกข้อมูลเรียบร้อย');", True)
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
SaveData()
End Sub
Protected Sub ddlTypeBr_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTypeBr.SelectedIndexChanged
If Me.ddlTypeBr.SelectedIndex = 0 Then
Me.ddlBranch.Visible = False
Me.lbBRType.Visible = False
Else
Me.ddlBranch.Visible = True
Me.lbBRType.Visible = True
bindBranch()
End If
End Sub
End Class
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET