 |
|
VB.NET อยากได้โค๊ดที่บันทึกข้อมูลใม่ให้ซ้ำกับข้อมูลเดิมที่บันทึกไปแล้ว |
|
 |
|
|
 |
 |
|
Unique Keys
|
 |
 |
 |
 |
Date :
2011-07-27 18:57:27 |
By :
ตะกอน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Sub btnSave_Click(sender As Object, e As EventArgs)
Dim intNumRows As Integer
strSQL = "SELECT COUNT(*) FROM customer WHERE CustomerID = '"& Me.txtCustomerID.Text &"' "
objCmd = New SqlCommand(strSQL, objConn)
intNumRows = objCmd.ExecuteScalar()
IF intNumRows > 0 Then
Me.pnlAdd.Visible = False
Me.lblStatus.Visible = True
Me.lblStatus.Text = "CustomerID already exist."
Else
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
" VALUES " & _
" ('" & Me.txtCustomerID.Text & "','" & Me.txtName.Text & "','" & Me.txtEmail.Text & "', " & _
" '" & Me.txtCountryCode.Text & "','" & Me.txtBudget.Text & "','" & Me.txtUsed.Text & "')"
objCmd = New SqlCommand
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Me.pnlAdd.Visible = False
Try
objCmd.ExecuteNonQuery()
Me.lblStatus.Text = "Record Inserted"
Me.lblStatus.Visible = True
Catch ex As Exception
Me.lblStatus.Visible = True
Me.lblStatus.Text = "Record can not insert Error ("& ex.Message &")"
End Try
End IF
End Sub
อยากเช็คอะไรเพิ่มก็ WHERE ตรงนี้ะครับ
Code (VB.NET)
strSQL = "SELECT COUNT(*) FROM customer WHERE CustomerID = '"& Me.txtCustomerID.Text &"' "
|
 |
 |
 |
 |
Date :
2011-07-27 21:13:55 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ฟิลด์ไอดีแรกของผมไม่ซ้ำครับ แต่จะมาซ้ำฟิลด์ไอดีที่ 2 (ขออภัยครับมือใหม่)
|
 |
 |
 |
 |
Date :
2011-07-29 10:46:53 |
By :
sunfulic |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
strSQL = "SELECT COUNT(*) FROM customer WHERE CustomerID = '"& Me.txtCustomerID.Text &"' AND .... "
เพิ่ม AND เข้าไปครับ
|
 |
 |
 |
 |
Date :
2011-07-29 11:55:26 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ ทำแบบ Unique Key เวลาป้อนรหัสลงมันก็ฟ้องว่ารหัสซ้ำที่หน้าจอเหมือนกัน
ขอบคุณมากๆครับ
|
 |
 |
 |
 |
Date :
2011-07-29 12:55:53 |
By :
sunfulic |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เขียนโปรแกรมยืม-คืน อุปกรณ์แต่ฟอร์มที่ทำอยู่ยังไม่สามารถบันทึกข้อมูลการยืมได้
ช่วยดูให้ด้วยนะค่ะว่าผิดตรงไหน
**** โค้ดบันทึึก ที่ต้องบันทึกข้อมูลจาก Tableข้อมูลเจ้าหน้าที่ และ ข้อมูลอุปกรณ์ที่ได้ Add ใส่ใน ListView *******
Code (VB.NET)
Private Sub btnSaveList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If txtID_E.Text = "" Then
MessageBox.Show("กรุณาระบุรหัสเจ้าหน้าที่ก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtID_E.Focus()
Exit Sub
End If
If lsvProductsList.Items.Count = 0 Then
MessageBox.Show("กรุณาป้อนรายการยืมอุปกรณ์ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtID_P.Focus()
Exit Sub
End If
Dim sqlBring As String = ""
Dim comBring As SqlCommand = New SqlCommand
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConnection
.Open()
End With
Try
If MessageBox.Show("คุณต้องการบันทึกรายการการยืมอุปกรณ์ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
AutoGenerateNo_W()
**** คิดว่าผิดตรงนี้ แต่แก้ไม่ถูกว่าต้องแก้ยังไง *****
sqlBring = "INSERT INTO I_Withdraw (ID_W,Date_W,ID_E,DataCheck_W) "
sqlBring &= " VALUES('" & LastID_W & "',"
sqlBring &= "'" & dtpDateWithdraw.Value & "',"
sqlBring &= "'" & txtID_E.Text & "')"
With comBring
.CommandType = CommandType.Text
.CommandText = sqlBring
.Connection = Conn
.ExecuteNonQuery()
End With
Dim i As Integer = 0
Dim tmpID_P As String = ""
Dim sqlProducts As String = ""
Dim tmpRemain_P As Integer = 0
Dim tmpNumber_W As Integer = 0
Dim tmpNumber_approval As Integer = 0
Dim tmpDateWithdraw As Date
tmpDateWithdraw = Date.Now
For i = 0 To lsvProductsList.Items.Count - 1
sqlBring = "INSERT INTO I_WithdrawDetails(ID_W,ID_P,Number_W,"
sqlBring &= " Number_approval)"
sqlBring &= " VALUES('" & LastID_W & "',"
tmpID_P = lsvProductsList.Items(i).SubItems(0).Text
tmpNumber_W = CInt(lsvProductsList.Items(i).SubItems(2).Text)
tmpNumber_approval = CInt(lsvProductsList.Items(i).SubItems(3).Text)
sqlBring &= "'" & tmpID_P & "',"
sqlBring &= tmpNumber_approval & ","
sqlBring &= tmpNumber_W & ")"
With comBring
.CommandText = sqlBring
.ExecuteNonQuery()
End With
sqlProducts = "SELECT ID_P,Remain_P FROM I_Equipment"
sqlProducts &= " WHERE (ID_P='" & tmpID_P & "')"
da.SelectCommand.CommandText = sqlProducts
da.Fill(ds, "I_Equipment")
tmpRemain_P = CInt(ds.Tables("I_Equipment").Rows(0).Item("Remain_P"))
ds.Tables("I_Equipment").Clear()
sqlBring = "UPDATE I_Equipment"
sqlBring &= " SET Remain_P =" & tmpRemain_P - tmpNumber_W
sqlBring &= " WHERE (ID_P='" & tmpID_P & "')"
With comBring
.CommandText = sqlBring
.ExecuteNonQuery()
End With
Next
'frmMain.tslStatus.Text = "กำลังบันทึกข้อมูล..."
MessageBox.Show("บันทึกรายการการยืมอุปกรณ์เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
'frmMain.tslStatus.Text = ""
If IsInstallPrinter() = True Then
If prDlg.ShowDialog = DialogResult.OK Then
prDoc.Print()
End If
Else
MessageBox.Show("กรุณาติดตั้งเครื่อง Printer ก่อนสั่งพิมพ์เอกสาร", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
ClearAllEmployeesData()
ClearAllProductsData()
lsvProductsList.Items.Clear()
txtID_E.Enabled = True
txtID_E.Focus()
End If
Catch ErrProcess As Exception
MessageBox.Show("ไม่สามารถบันทึกรายการการยืมได้ เนื่องจาก " & ErrProcess.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
End Sub
|
 |
 |
 |
 |
Date :
2011-10-16 15:22:03 |
By :
kuuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
sqlBring = "INSERT INTO I_Withdraw (ID_W,Date_W,ID_E,DataCheck_W) "
sqlBring &= " VALUES('" & LastID_W & "',"
sqlBring &= "'" & dtpDateWithdraw.Value & "',"
sqlBring &= "'" & txtID_E.Text & "','" & เหมือนคุณจะลืม Values ไปอีก 1 ค่าน่ะครับ & "')"
|
 |
 |
 |
 |
Date :
2011-10-20 10:56:24 |
By :
unlum |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|