ขอสอบถามปัญหาเพิ่มข้อมูลไม่ได้ คืออยากให้ pimary Key เป็น auto run
กำหนดตอนสร้าง tb ครับ ให้ไปเซ็ตที่ columm properties ---> กดที่ + ของ identity ---> is identity ให้เป็น yes ก็เสร็จครับ
แล้วก็ตอนเขียน insert ไม่ต้อง insert field ที่เป็น auto นะครับ
Date :
2010-11-23 15:35:17
By :
kanchen
การใช้ CommandBuilder เพื่อสร้างชุดคำสั่ง Insert/Update/Delete ให้ SqlDataAdapter ก็น่าจะใช้ได้แล้วนะ
คุณลองตั้ง BreakPoint ไว้ที่บรรทัดคำสั่ง da.Update ใน Event ที่คุณจะเพิ่มข้อมูล แล้ว Post ให้ดูหน่อยว่า
CommandText ข้างล่างนี้เป็นอย่างไร
da.InsertCommand.CommandText
da.UpdateCommand.CommandText
da.DeleteCommand.CommandText
Date :
2010-11-23 15:37:43
By :
watcharop
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlupdate As String
Dim strConn As String
strConn = "Data Source=.\SQLEXPRESS;Initial Catalog=SSPEGIS2;Integrated Security=True"
sqlupdate = "update land "
sqlupdate &= " set PARCEL_CODE = '" & txtPARCEL_CODE.Text & "',"
sqlupdate &= " TAMBON_TH= '" & txtTAMBON_TH.Text & "',"
sqlupdate &= " PICTURE_CODE= '" & txtPICTURE_CODE.Text & "',"
sqlupdate &= " UTM_CODE= '" & txtUTM_CODE.Text & "',"
sqlupdate &= " UTM_NO= '" & txtUTM_NO.Text & "',"
sqlupdate &= " UTM_NO_P= '" & txtUTM_NO_P.Text & "',"
sqlupdate &= " UTM_RATIO= '" & txtUTM_RATIO.Text & "',"
sqlupdate &= " LDOC_NO= '" & txtLDOC_NO.Text & "',"
sqlupdate &= " TAXRATE= '" & txtTAXRATE.Text & "',"
sqlupdate &= " EXTENUATE= '" & txtEXTENUATE.Text & "',"
sqlupdate &= " SURVEY_NO= '" & txtSURVEY_NO.Text & "',"
sqlupdate &= " LAND_NO= '" & txtLAND_NO.Text & "',"
sqlupdate &= " DOL_RAI= '" & txtDOL_RAI.Text & "',"
sqlupdate &= " DOL_NGAN= '" & txtDOL_NGAN.Text & "',"
sqlupdate &= " DOL_WA= '" & txtDOL_WA.Text & "',"
sqlupdate &= " TAXED_RAI= '" & txtTAXED_RAI.Text & "',"
sqlupdate &= " TAXED_NGAN= '" & txtTAXED_NGAN.Text & "',"
sqlupdate &= " TAXED_WA= '" & txtTAXED_WA.Text & "',"
sqlupdate &= " OWNERID= '" & txtOWNERID.Text & "',"
sqlupdate &= " OCCUID= '" & txtOCCUID.Text & "',"
'ส่วนของการบันทึก combobox
sqlupdate &= " LDOC_TYPEID= '" & CStr(cboLDOC_TYPE.SelectedValue) & "',"
sqlupdate &= " LAND_USEID= '" & CStr(cboLAND_USE.SelectedValue) & "',"
sqlupdate &= " AREAID= '" & CStr(cboAREATAX.SelectedValue) & "',"
sqlupdate &= " DISTRICT_TH= " & Val(txtDISTRICT_TH.Text) & ","
sqlupdate &= " PROVINCE_TH= " & Val(txtPROVINCE_TH.Text) & ""
sqlupdate &= " where LANDID = '" & lblLANDID.Text & "'"
Try
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
land = New SqlCommand
With land
.CommandType = CommandType.Text
.CommandText = sqlupdate
.Connection = Conn
.ExecuteNonQuery()
End With
Catch ErrorToEdit As Exception
MessageBox.Show(ErrorToEdit.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
add_state = False
MessageBox.Show("บันทึกข้อมูลเรียบร้อยเเล้ว ", "แจ้งให้ทราบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
ผมเขียนโค๊ดเเบบนี้ครับ ต้องระบุ textbox ครับ เพราะมันมีหลายตาราง ครับ
Date :
2010-11-23 16:09:44
By :
pisansri
Code ที่ให้มาเป็นการอัพเดตข้อมูลที่มีอยู่แล้ว
แล้ว Code ที่ใช้เพื่อเพิ่มข้อมูลในตารางได้ทำหรือยังครับ
ขอดูหน่อยครับ
ตกลง LANDID เป็นตัวเลขรีเปล่าครับ
ถ้าใช่ บรรทัดนี้
sqlupdate &= " where LANDID = '" & lblLANDID.Text & "'"
แก้เป็นอย่างนี้ จะดีกว่า
sqlupdate &= " where LANDID = " & lblLANDID.Text
Date :
2010-11-23 16:22:45
By :
watcharop
Imports System.Data
Imports System.Data.SqlClient
Public Class frmland
Dim Conn As SqlConnection = New SqlConnection
Dim da As SqlDataAdapter
Dim add_state As Boolean = False
Dim dt As New DataTable
Dim db As New DataTable
Dim land As SqlCommand
Private bin As New BindingSource
Dim objCurrencyMgr As CurrencyManager
Dim currRecPos As Integer
Dim addNewMode As Boolean
Dim ds As DataSet = New DataSet
Dim IsFind As Boolean = False
Dim acProductList As New AutoCompleteStringCollection()
Private Sub Land_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String
strConn = "Data Source=.\SQLEXPRESS;Initial Catalog=SSPEGIS2;Integrated Security=True"
Conn = New SqlConnection(strConn)
Dim sqlland As String = ""
sqlland = "SELECT * FROM land ORDER BY LANDID"
If IsFind = True Then
ds.Tables("land").Clear()
End If
da = New SqlDataAdapter(sqlland, Conn)
da.Fill(db)
Dim cmb As SqlCommandBuilder
cmb = New SqlCommandBuilder(da)
bin.DataSource = db
txtLANDID.DataBindings.Add("Text", bin, "LANDID")
txtPARCEL_CODE.DataBindings.Add("Text", bin, "PARCEL_CODE")
txtTAMBON_TH.DataBindings.Add("Text", bin, "TAMBON_TH")
txtDISTRICT_TH.DataBindings.Add("Text", bin, "DISTRICT_TH")
txtPROVINCE_TH.DataBindings.Add("Text", bin, "PROVINCE_TH")
txtUTM_CODE.DataBindings.Add("Text", bin, "UTM_CODE")
txtUTM_NO.DataBindings.Add("Text", bin, "UTM_NO")
txtUTM_NO_P.DataBindings.Add("Text", bin, "UTM_PAGE")
txtSURVEY_NO.DataBindings.Add("Text", bin, "SURVEY_NO")
txtLAND_NO.DataBindings.Add("Text", bin, "LAND_NO")
txtLDOC_NO.DataBindings.Add("Text", bin, "LDOC_NO")
txtDOL_RAI.DataBindings.Add("Text", bin, "DOL_RAI")
txtDOL_NGAN.DataBindings.Add("Text", bin, "DOL_NGAN")
txtDOL_WA.DataBindings.Add("Text", bin, "DOL_WA")
txtTAXED_RAI.DataBindings.Add("Text", bin, "TAXED_RAI")
txtTAXED_NGAN.DataBindings.Add("Text", bin, "TAXED_NGAN")
txtTAXED_WA.DataBindings.Add("Text", bin, "TAXED_WA")
txtEXTENUATE.DataBindings.Add("Text", bin, "EXTENUATE")
lblEVALUATE_TAX.DataBindings.Add("Text", bin, "EVALUATE_TAX")
txtTAXRATE.DataBindings.Add("Text", bin, "TAXRATE")
txtPICTURE_CODE.DataBindings.Add("Text", bin, "PICTURE_CODE")
txtOWNERID.DataBindings.Add("Text", bin, "OWNERID")
txtOCCUID.DataBindings.Add("Text", bin, "OWNERID")
Dim sqlpopulation As String = ""
sqlpopulation = "SELECT land.LANDID, land.PARCEL_CODE, land.PICTURE_CODE, land.SUBBLOCK, land.AREAID, land.EXTENID, land.OWNERID, land.OCCUID, land.LDOC_TYPEID, land.MOOID, land.SOINAME, land.ROADNAME, land.VILLAGEID, land.TAMBONID, land.DISTRICTID, land.PROVINCEID, land.TAMBON_TH, land.DISTRICT_TH, land.PROVINCE_TH, land.LDOC_NO, land.UTM_RATIO, land.UTM_CODE, land.UTM_NO_P, land.UTM_NO, land.UTM_PAGE, land.LAND_NO, land.SURVEY_NO, land.DOL_RAI, land.DOL_NGAN, land.DOL_WA, land.LAND_VALUE, land.LAND_RENTID, land.LAND_USEID, land.USED_RAI, land.USED_NGAN, land.USED_WA, land.ZONE, land.BLOCK, land.LOT, land.TAXED_RAI, land.TAXED_NGAN, land.TAXED_WA, land.TOTAL_RAI, land.MINUS_RAI, land.TAXRATE, land.EXTENUATE, land.EXTEN_DOLRAI, land.EVALUATE_TAX, land.EVALUATE_DATE, land.ENTDATE, land.LASTUPDATE, land.YEAR_LAND, land.COST_YEAR, land.PICTURE, land.REMARK, population.PID, population.POPCODE, population.POPID, population.NAMECODE, population.TITLEID, population.FIRSTNAME, population.LASTNAME, population.BIRTHDAY, population.PERSONTYPEID, population.HOUSENO, population.MOO, population.SOINAME, population.ROADNAME, population.VILLAGEID, population.VILLAGENAME_TH, population.TAMBON_TH, population.DISTRICT_TH, population.PROVINCE_TH, population.TAMBONID, population.DISTRICTID, population.PROVINCEID, population.REGION, population.ZIPCODE, population.TELEPHONE, population.EMAIL, population.LOCALID, population.TAXID, population.SEXID, population.NATIONID, population.ORIGINID, population.RELIGIONID, population.PICTURE, population.LASTUPDATE, population.USERID, population.MACID, population.MACIP FROM land LEFT JOIN population ON land.OWNERID = population.PID ORDER BY LANDID,PID"
da = New SqlDataAdapter(sqlpopulation, Conn)
da.Fill(dt)
Dim cmd As SqlCommandBuilder
cmd = New SqlCommandBuilder(da)
bin.DataSource = dt
txtFIRSTNAME.DataBindings.Add("Text", bin, "FIRSTNAME")
txtLASTNAME.DataBindings.Add("Text", bin, "LASTNAME")
txtNAMECODE.DataBindings.Add("Text", bin, "NAMECODE")
txtPOPID.DataBindings.Add("Text", bin, "POPID")
txtHOUSENO.DataBindings.Add("Text", bin, "HOUSENO")
txtMOO.DataBindings.Add("Text", bin, "MOO")
txtVILLAGENAME_TH.DataBindings.Add("Text", bin, "VILLAGENAME_TH")
txtSOINAME.DataBindings.Add("Text", bin, "SOINAME")
txtROADNAME.DataBindings.Add("Text", bin, "ROADNAME")
txtTAMBONID.DataBindings.Add("Text", bin, "TAMBON_TH")
txtDISTRICTID.DataBindings.Add("Text", bin, "DISTRICT_TH")
txtPROVINCE.DataBindings.Add("Text", bin, "PROVINCE_TH")
txtZIPCODE.DataBindings.Add("Text", bin, "ZIPCODE")
'ส่วนของเจ้าของทรัพย์สิน
txtNAMECODEID.DataBindings.Add("Text", bin, "NAMECODE")
POPID.DataBindings.Add("Text", bin, "POPID")
FIRSTNAME.DataBindings.Add("Text", bin, "FIRSTNAME")
LASTNAME.DataBindings.Add("Text", bin, "LASTNAME")
Dim sqlland_use As String = ""
sqlland_use = "SELECT * FROM land_use ORDER BY LAND_USEID"
da = New SqlDataAdapter(sqlland_use, Conn)
da.Fill(ds, "land_use")
Dim sqlldoc_type As String = ""
sqlldoc_type = "SELECT * FROM ldoc_type ORDER BY LDOC_TYPEID"
da = New SqlDataAdapter(sqlldoc_type, Conn)
da.Fill(ds, "ldoc_type")
Dim sqlareatax As String = ""
sqlareatax = "SELECT * FROM areatax ORDER BY AREAID"
da = New SqlDataAdapter(sqlareatax, Conn)
da.Fill(ds, "areatax")
With cboLAND_USE
.DisplayMember = "USED_DESC"
.ValueMember = "LAND_USEID"
.DataSource = ds.Tables("land_use")
.DataBindings.Add("SelectedValue", bin, "LAND_USEID")
End With
With cboLDOC_TYPE
.DisplayMember = "LDOC_TYPE"
.ValueMember = "LDOC_TYPEID"
.DataSource = ds.Tables("ldoc_type")
.DataBindings.Add("SelectedValue", bin, "LDOC_TYPEID")
End With
With cboAREATAX
.DisplayMember = "AREANAME"
.ValueMember = "AREAID"
.DataSource = ds.Tables("areatax")
.DataBindings.Add("SelectedValue", bin, "AREAID")
End With
Dim sqltitlecode As String = ""
sqltitlecode = "SELECT * FROM titlecode ORDER BY TITLEID"
da = New SqlDataAdapter(sqltitlecode, Conn)
da.Fill(ds, "titlecode")
With CboTITLE_NAME
.DisplayMember = "TITLE_NAME"
.ValueMember = "TITLEID"
.DataSource = ds.Tables("titlecode")
.DataBindings.Add("SelectedValue", bin, "TITLEID")
End With
With txtTITLE_NAMEID
.DisplayMember = "TITLE_NAME"
.ValueMember = "TITLEID"
.DataSource = ds.Tables("titlecode")
.DataBindings.Add("SelectedValue", bin, "TITLEID")
End With
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlupdate As String
Dim sqladd As String
Dim strConn As String
strConn = "Data Source=.\SQLEXPRESS;Initial Catalog=SSPEGIS;Integrated Security=True"
sqlupdate = "update land "
sqlupdate &= " set PARCEL_CODE = '" & txtPARCEL_CODE.Text & "',"
sqlupdate &= " TAMBON_TH= '" & txtTAMBON_TH.Text & "',"
sqlupdate &= " PICTURE_CODE= '" & txtPICTURE_CODE.Text & "',"
sqlupdate &= " UTM_CODE= '" & txtUTM_CODE.Text & "',"
sqlupdate &= " UTM_NO= '" & txtUTM_NO.Text & "',"
sqlupdate &= " UTM_NO_P= '" & txtUTM_NO_P.Text & "',"
sqlupdate &= " UTM_RATIO= '" & txtUTM_RATIO.Text & "',"
sqlupdate &= " LDOC_NO= '" & txtLDOC_NO.Text & "',"
sqlupdate &= " TAXRATE= '" & txtTAXRATE.Text & "',"
sqlupdate &= " EXTENUATE= '" & txtEXTENUATE.Text & "',"
sqlupdate &= " SURVEY_NO= '" & txtSURVEY_NO.Text & "',"
sqlupdate &= " LAND_NO= '" & txtLAND_NO.Text & "',"
sqlupdate &= " DOL_RAI= '" & txtDOL_RAI.Text & "',"
sqlupdate &= " DOL_NGAN= '" & txtDOL_NGAN.Text & "',"
sqlupdate &= " DOL_WA= '" & txtDOL_WA.Text & "',"
sqlupdate &= " TAXED_RAI= '" & txtTAXED_RAI.Text & "',"
sqlupdate &= " TAXED_NGAN= '" & txtTAXED_NGAN.Text & "',"
sqlupdate &= " TAXED_WA= '" & txtTAXED_WA.Text & "',"
sqlupdate &= " OWNERID= '" & txtOWNERID.Text & "',"
sqlupdate &= " OCCUID= '" & txtOCCUID.Text & "',"
'ส่วนของการบันทึก combobox
sqlupdate &= " LDOC_TYPEID= '" & CStr(cboLDOC_TYPE.SelectedValue) & "',"
sqlupdate &= " LAND_USEID= '" & CStr(cboLAND_USE.SelectedValue) & "',"
sqlupdate &= " AREAID= '" & CStr(cboAREATAX.SelectedValue) & "',"
sqlupdate &= " DISTRICT_TH= " & Val(txtDISTRICT_TH.Text) & ","
sqlupdate &= " PROVINCE_TH= " & Val(txtPROVINCE_TH.Text) & ""
sqlupdate &= " where LANDID = '" & txtLANDID.Text & "'"
sqladd = " insert into land (LANDID,PARCEL_CODE,TAMBON_TH,PICTURE_CODE,UTM_CODE,UTM_NO,UTM_RATIO,LDOC_NO,TAXRATE,EXTENUATE,SURVEY_NO,LAND_NO,DOL_RAI,DOL_NGAN,DOL_WA,TAXED_RAI,TAXED_NGAN,TAXED_WA,OWNERID,OCCUID,DISTRICT_TH,PROVINCE_TH)"
sqladd &= " values ('" & txtLANDID.Text & "',"
sqladd &= " '" & txtPARCEL_CODE.Text & "',"
sqladd &= "'" & txtTAMBON_TH.Text & "',"
sqladd &= "'" & txtPICTURE_CODE.Text & "',"
sqladd &= "'" & txtUTM_CODE.Text & "',"
sqladd &= "'" & txtUTM_NO.Text & "',"
sqladd &= "'" & txtUTM_NO_P.Text & "',"
sqladd &= "'" & txtUTM_RATIO.Text & "',"
sqladd &= "'" & txtLDOC_NO.Text & "',"
sqladd &= "'" & txtTAXRATE.Text & "',"
sqladd &= "'" & txtEXTENUATE.Text & "',"
sqladd &= "'" & txtSURVEY_NO.Text & "',"
sqladd &= "'" & txtLAND_NO.Text & "',"
sqladd &= "'" & txtDOL_RAI.Text & "',"
sqladd &= "'" & txtDOL_NGAN.Text & "',"
sqladd &= "'" & txtDOL_WA.Text & "',"
sqladd &= "'" & txtTAXED_RAI.Text & "',"
sqladd &= "'" & txtTAXED_NGAN.Text & "',"
sqladd &= "'" & txtTAXED_WA.Text & "',"
sqladd &= "'" & txtOWNERID.Text & "',"
sqladd &= "'" & txtOCCUID.Text & "',"
sqladd &= "" & Val(txtDISTRICT_TH.Text) & ","
sqladd &= "" & Val(txtPROVINCE_TH.Text) & ")"
Try
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
land = New SqlCommand
With land
.CommandType = CommandType.Text
If add_state = True Then
.CommandText = sqladd
Else
.CommandText = sqlupdate
End If
.Connection = Conn
.ExecuteNonQuery()
End With
Catch ErrorToEdit As Exception
MessageBox.Show(ErrorToEdit.Message & sqladd, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
add_state = False
MessageBox.Show("บันทึกข้อมูลเสร็จแล้ว ", "แจ้งให้ทราบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call Show()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
addNewMode = True
currRecPos = bin.Position
bin.AddNew() 'เพิ่มเรคอร์ดใหม่ว่างๆ 1 เรคอร์ดใน datatable
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
bin.Position = 1
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
bin.Position -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
bin.Position += 1
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
bin.Position = bin.Count - 1
End Sub
Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
If dt.Rows.Count = 0 Then Exit Sub
If MessageBox.Show("ต้องการลบเรคอร์ดนี้ใช่หรือไม่?", "ยืนยันการลบ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
bin.RemoveCurrent()
currRecPos = bin.Position
End If
If dt.Rows.Count = 0 Then
End If
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
If dt.Rows.Count <= 0 Then
Exit Sub
Else
addNewMode = False
currRecPos = bin.Position
txtLANDID.Focus()
End If
End Sub
Private Sub btnprint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprint.Click
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
SetAutoCompleteMode()
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
SetAutoCompleteMode()
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
SetAutoCompleteMode()
End Sub
Private Sub SetAutoCompleteMode()
With cboLAND_USE
.AutoCompleteSource = AutoCompleteSource.CustomSource
.AutoCompleteCustomSource = acProductList
If RadioButton1.Checked Then
.AutoCompleteMode = AutoCompleteMode.Append
ElseIf RadioButton2.Checked Then
.AutoCompleteMode = AutoCompleteMode.Suggest
ElseIf RadioButton3.Checked Then
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End If
End With
End Sub
นี่ครับ ทั้งหมดของผมแก้ไขได้เเต่ยังเพิ่มไม่ ได้ บางครั้งก็ได้ บางครั้งก้อไม่ได้
With land
.CommandType = CommandType.Text
If add_state = True Then
.CommandText = sqladd
Else
.CommandText = sqlupdate
End If
.Connection = Conn
.ExecuteNonQuery()
End With
มันติดตรงนี้ด้วยครับ ถ้าเอา If ให้มันเหลือเเต่ การ add มันก็เพิ่มได้ เเต่ถ้าเอาไว้ทั้งสอง เพิ่มไม่ได้ ครับ อัพเดทได้อย่างเดียว
Date :
2010-11-24 09:32:38
By :
pisansri
เพี้ยงขอให้ผ่าน
Code (VB.NET)
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlupdate As String
Dim sqladd As String
Dim strConn As String
strConn = "Data Source=.\SQLEXPRESS;Initial Catalog=SSPEGIS;Integrated Security=True"
sqlupdate = "update land "
sqlupdate &= " set PARCEL_CODE = '" & txtPARCEL_CODE.Text & "',"
sqlupdate &= " TAMBON_TH= '" & txtTAMBON_TH.Text & "',"
sqlupdate &= " PICTURE_CODE= '" & txtPICTURE_CODE.Text & "',"
sqlupdate &= " UTM_CODE= '" & txtUTM_CODE.Text & "',"
sqlupdate &= " UTM_NO= '" & txtUTM_NO.Text & "',"
sqlupdate &= " UTM_NO_P= '" & txtUTM_NO_P.Text & "',"
sqlupdate &= " UTM_RATIO= '" & txtUTM_RATIO.Text & "',"
sqlupdate &= " LDOC_NO= '" & txtLDOC_NO.Text & "',"
sqlupdate &= " TAXRATE= '" & txtTAXRATE.Text & "',"
sqlupdate &= " EXTENUATE= '" & txtEXTENUATE.Text & "',"
sqlupdate &= " SURVEY_NO= '" & txtSURVEY_NO.Text & "',"
sqlupdate &= " LAND_NO= '" & txtLAND_NO.Text & "',"
sqlupdate &= " DOL_RAI= '" & txtDOL_RAI.Text & "',"
sqlupdate &= " DOL_NGAN= '" & txtDOL_NGAN.Text & "',"
sqlupdate &= " DOL_WA= '" & txtDOL_WA.Text & "',"
sqlupdate &= " TAXED_RAI= '" & txtTAXED_RAI.Text & "',"
sqlupdate &= " TAXED_NGAN= '" & txtTAXED_NGAN.Text & "',"
sqlupdate &= " TAXED_WA= '" & txtTAXED_WA.Text & "',"
sqlupdate &= " OWNERID= '" & txtOWNERID.Text & "',"
sqlupdate &= " OCCUID= '" & txtOCCUID.Text & "',"
'ส่วนของการบันทึก combobox
sqlupdate &= " LDOC_TYPEID= '" & CStr(cboLDOC_TYPE.SelectedValue) & "',"
sqlupdate &= " LAND_USEID= '" & CStr(cboLAND_USE.SelectedValue) & "',"
sqlupdate &= " AREAID= '" & CStr(cboAREATAX.SelectedValue) & "',"
sqlupdate &= " DISTRICT_TH= " & Val(txtDISTRICT_TH.Text) & ","
sqlupdate &= " PROVINCE_TH= " & Val(txtPROVINCE_TH.Text) & ""
sqlupdate &= " where LANDID = '" & txtLANDID.Text & "'"
sqladd = " insert into land (LANDID,PARCEL_CODE,TAMBON_TH,PICTURE_CODE,UTM_CODE,UTM_NO,UTM_RATIO,LDOC_NO,TAXRATE,EXTENUATE,SURVEY_NO,LAND_NO,DOL_RAI,DOL_NGAN,DOL_WA,TAXED_RAI,TAXED_NGAN,TAXED_WA,OWNERID,OCCUID,DISTRICT_TH,PROVINCE_TH)"
sqladd &= " values ('" & txtLANDID.Text & "',"
sqladd &= " '" & txtPARCEL_CODE.Text & "',"
sqladd &= "'" & txtTAMBON_TH.Text & "',"
sqladd &= "'" & txtPICTURE_CODE.Text & "',"
sqladd &= "'" & txtUTM_CODE.Text & "',"
sqladd &= "'" & txtUTM_NO.Text & "',"
sqladd &= "'" & txtUTM_NO_P.Text & "',"
sqladd &= "'" & txtUTM_RATIO.Text & "',"
sqladd &= "'" & txtLDOC_NO.Text & "',"
sqladd &= "'" & txtTAXRATE.Text & "',"
sqladd &= "'" & txtEXTENUATE.Text & "',"
sqladd &= "'" & txtSURVEY_NO.Text & "',"
sqladd &= "'" & txtLAND_NO.Text & "',"
sqladd &= "'" & txtDOL_RAI.Text & "',"
sqladd &= "'" & txtDOL_NGAN.Text & "',"
sqladd &= "'" & txtDOL_WA.Text & "',"
sqladd &= "'" & txtTAXED_RAI.Text & "',"
sqladd &= "'" & txtTAXED_NGAN.Text & "',"
sqladd &= "'" & txtTAXED_WA.Text & "',"
sqladd &= "'" & txtOWNERID.Text & "',"
sqladd &= "'" & txtOCCUID.Text & "',"
sqladd &= "" & Val(txtDISTRICT_TH.Text) & ","
sqladd &= "" & Val(txtPROVINCE_TH.Text) & ")"
Try
If Conn.State <> ConnectionState.Open Then Conn.Open() ' เปิดเมื่อจะใช้
Dim cmd As SqlCommand = Conn.CreateCommand()
cmd.CommandType = CommandType.Text
If addNewMode = True Then
.CommandText = sqladd ' ผมคาดหวังว่า sqladd ที่คุณทำไว้ถูกต้องนะ
Else
.CommandText = sqlupdate ' ผมคาดหวังว่า sqlupdate ที่คุณทำไว้ถูกต้องนะ
End If
cmd.ExecuteNonQuery()
Conn.Close() ' ปิดเมื่อเลิกใช้ ไม่ควรเปิด Connection ทิ้งไว้
addNewMode = False
MessageBox.Show("บันทึกข้อมูลเสร็จแล้ว ", "แจ้งให้ทราบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call Show()
Catch ErrorToEdit As Exception
MessageBox.Show(ErrorToEdit.Message & sqladd, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Date :
2010-11-24 10:53:05
By :
watcharop
ยังเหมือนเดิมครับ
Date :
2010-11-24 12:33:44
By :
pisansri
ผมเข้าใจว่า ฟอร์มของคุณ มีปุ่มอย่างน้อยเป็นอย่างนี้
btnAdd: ใช้เมื่อต้องการจะสร้างรายการ LAND ใหม่
btnEdit: ใช้เมื่อต้องการจะแก้ไขรายการ LAND ที่มีอยู่
btnSave: เมื่อต้องการบันทึกข้อมูล
นั่นคือ
1. เมื่อต้องการเพิ่มรายการใหม่ User ต้องกดปุ่ม btnAdd จากนั้นก็ใส่ข้อมูล แล้วกดปุ่ม btnSave (addNewMode เป็น True)
2. เมื่อต้องการแก้ไขรายการ User ต้องกดปุ่ม btnEdit จากนั้นก็ใส่ข้อมูล แล้วกดปุ่ม btnSave (addNewMode เป็น False)
ถ้าเป็นอย่างที่ว่า
ตัวแปร addNewMode จะเป็น True เมื่อ User กดปุ่ม btnAdd แล้วกดปุ่ม btnSave
ดังนั้นอยากให้ตั้ง Break Point ไว้ใน btnSave_Click
ที่บรรทัดคำสั่ง If addNewMode = True Then
แล้วเช็คว่า addNewMode เป็นอย่างที่คาดไว้หรือไม่
ถ้าไม่ใช่ คุณต้องหาสาเหตุว่าทำไม addNewMode มิได้เป็นเช่นนั้น
ประเด็นต่อมาคือ
ในการแก้ไข หรือเพิ่มข้อมูล แล้วกดปุ่มบันทึกนั้น โปรแกรมเกิด Error หรือไม่
แล้ว Show() คืออะไร
Date :
2010-11-24 12:54:03
By :
watcharop
น่าจะปล่อยให้นั่งดูสักสองสามวันแล้วค่อยตอบ(ดองให้ออกเปรี้ยว)
เขียนมายาวๆแล้วมาถามว่าผิดตรงไหนเนี่ย
Date :
2010-11-24 13:20:41
By :
เฮ้อ
เอาภาพฮาๆมาให้ดูครายเครียดครับ
Date :
2010-11-24 14:14:53
By :
nooknoname
ลองๆบอกยอมแพ้แล้วให้เขียนเองดิ
แรกๆจะมีเสียงโอดครวญว่าไม่มีน้ำใจมั่งละ ก็ผมไม่เก่งนี่ ฯลฯ
แต่สักพักพอทำใจได้จะเขียนได้ละเอียดถี่ถ้วนขึ้น
แล้วจะกลับมาถามใหม่ วัฎจักรจะเป็นแบบนี้แล
แล้วที่ต้องมาเขียน sql ยาวๆนี่ ไม่ได้ normalize อะไรมาเลย
นึกไรได้ก็จับใส่ๆ code เหมือนกับ copy นี่นิดนั่นหน่อย เลยลงเอยด้วย
ผมผิดตรงไหนไม่ทราบครับ ช่วยผมด้วย
เอ้าพยายามเข้า ตอนเราเริ่มเขียนไปถามรุ่นพี่เจอมาหนักกว่านี้นายเอ้ย
Date :
2010-11-24 14:15:37
By :
เฮ้อ เฮ้อ
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Call clear_text()
txtLANDID.Focus()
add_state = True
End Sub
Private Sub clear_text()
txtLANDID.Text = ""
txtPARCEL_CODE.Text = ""
txtTAMBON_TH.Text = ""
txtUTM_CODE.Text = ""
txtUTM_NO.Text = ""
txtUTM_NO_P.Text = ""
txtSURVEY_NO.Text = ""
txtLAND_NO.Text = ""
txtLDOC_NO.Text = ""
txtDOL_RAI.Text = ""
txtDOL_NGAN.Text = ""
txtDOL_WA.Text = ""
txtTAXED_RAI.Text = ""
txtTAXED_NGAN.Text = ""
txtTAXED_WA.Text = ""
txtEXTENUATE.Text = ""
txtTAXRATE.Text = ""
txtDISTRICT_TH.Text = ""
txtPROVINCE_TH.Text = ""
txtOWNERID.Text = ""
txtOCCUID.Text = ""
End Sub
เเก้ได้เเล้วครับ ผมเขียนปุ่ม Add ใหม่ ขอบคุณครับที่เอาภาพฮาๆ มา ให้ดู
Date :
2010-11-24 16:35:58
By :
pisansri
เมาละ เครียด
5555 พรุ่ง นี้ไปสู้ต่อกับโปรแกรม
Date :
2010-11-25 00:37:51
By :
pisansri
Load balance : Server 01