บันทึกข้อมูลลงฐานข้อมูลไม่ได้ค่ะ ทำไงดีคะ ใส่โค๊ดเเล้วตอนนั้นไม่ฟ้องเออเร่อ พอ Run มันฟ้องบรรทัดนี้ค่ะ mycommand.ExecuteNonQuery()
บันทึกข้อมูลไม่ได้ค่ะ ใส่โค๊ดแล้วตอน Run ฟ้องว่า Incorrect syntax near '-'. ตามรูปนี้เลยค่ะCode (VB.NET)
Imports System.Data.SqlClient
Public Class frmMember
Dim conn As SqlConnection
Dim status As Integer
Dim myadapter As SqlDataAdapter
Dim mydata As SqlDataReader
Dim sqlquery As String
Dim mycommand As SqlCommand
Private Sub frmMember_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New SqlConnection()
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = Database.strConn.ToString
.Open()
End With
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
TextBox5.Enabled = False
ComboBox1.Enabled = False
ComboBox2.Enabled = False
ComboBox3.Enabled = False
DateTimePicker1.Enabled = False
Button2.Enabled = False
Button4.Enabled = False
Button3.Enabled = True
Button6.Enabled = False
Button5.Enabled = False
filllistview()
End Sub
Public Sub filllistview()
mycommand = New SqlCommand()
sqlquery = "SELECT * FROM member"
mycommand.CommandText = sqlquery
mycommand.Connection = conn
mydata = mycommand.ExecuteReader
ListView1.Items.Clear()
While (mydata.Read())
With ListView1.Items.Add(mydata("รหัสสมาชิก"))
.subitems.add(mydata("คำนำหน้าชื่อ"))
.subitems.add(mydata("ชื่อ-สกุล"))
.subitems.add(mydata("ที่อยู่"))
.subitems.add(mydata("ระดับชั้น"))
.subitems.add(mydata("วันที่สมัครสมาชิก"))
.subitems.add(mydata("จำนวนหุ้น"))
.subitems.add(mydata("ราคาหุ้นละ"))
.subitems.add(mydata("รวมราคาหุ้น"))
End With
End While
mycommand.Dispose()
mydata.Close()
conn.Close()
End Sub
Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
TextBox1.Text = ListView1.SelectedItems(0).SubItems(0).Text
ComboBox1.Text = ListView1.SelectedItems(0).SubItems(1).Text
TextBox2.Text = ListView1.SelectedItems(0).SubItems(2).Text
TextBox3.Text = ListView1.SelectedItems(0).SubItems(3).Text
ComboBox2.Text = ListView1.SelectedItems(0).SubItems(4).Text
DateTimePicker1.Text = ListView1.SelectedItems(0).SubItems(5).Text
ComboBox3.Text = ListView1.SelectedItems(0).SubItems(6).Text
TextBox4.Text = ListView1.SelectedItems(0).SubItems(7).Text
TextBox5.Text = ListView1.SelectedItems(0).SubItems(8).Text
Button6.Enabled = True
Button5.Enabled = True
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
DateTimePicker1.Text = ""
TextBox1.Focus()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
status = 1
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox3.Enabled = True
TextBox4.Enabled = True
TextBox5.Enabled = True
ComboBox1.Enabled = True
ComboBox2.Enabled = True
ComboBox3.Enabled = True
DateTimePicker1.Enabled = True
Button2.Enabled = True
Button4.Enabled = True
TextBox1.Focus()
conn.Open()
filllistview()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
status = 2
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox3.Enabled = True
TextBox4.Enabled = True
TextBox5.Enabled = True
ComboBox1.Enabled = True
ComboBox2.Enabled = True
ComboBox3.Enabled = True
DateTimePicker1.Enabled = True
Button2.Enabled = True
Button4.Enabled = True
Button3.Enabled = False
Button5.Enabled = False
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
status = 3
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox3.Enabled = True
TextBox4.Enabled = True
TextBox5.Enabled = True
ComboBox1.Enabled = True
ComboBox2.Enabled = True
ComboBox3.Enabled = True
DateTimePicker1.Enabled = True
Button2.Enabled = True
Button4.Enabled = True
Button6.Enabled = False
Button3.Enabled = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If status = 1 Then
conn.Open()
sqlquery = "INSERT INTO member(รหัสสมาชิก ,คำนำหน้าชื่อ ,ชื่อ-สกุล ,ที่อยู่ ,ระดับชั้น ,วันที่สมัครสมาชิก ,จำนวนหุ้น ,ราคาหุ้นละ ,รวมราคาหุ้น)VALUES ('" + TextBox1.Text + "','" + ComboBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + ComboBox2.Text + "','" + DateTimePicker1.Text + "','" + ComboBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "');"
mycommand.CommandText = sqlquery
mycommand.Connection = conn
mycommand.ExecuteNonQuery()
'If mydata.HasRows Then
' MsgBox("NO")
'Else
MsgBox("success")
mycommand.Dispose()
status = 0
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
DateTimePicker1.Text = ""
TextBox1.Focus()
Me.filllistview()
conn.Close()
End If
If status = 2 Then
conn.Open()
sqlquery = "UPDATE member SET รหัสสมาชิก= '" + TextBox1.Text + "',คำนำหน้าชื่อ= '" + ComboBox1.Text + "', ชื่อ-สกุล= '" + TextBox2.Text + "', ที่อยู่= '" + TextBox3.Text + "', ระดับชั้น= '" + ComboBox2.Text + "', วันที่สมัครสมาชิก= '" + DateTimePicker1.Text + "', จำนวนหุ้น= '" + ComboBox3.Text + "', ราคาหุ้น= '" + TextBox4.Text + "', รวมราคาหุ้น= '" + TextBox5.Text + "' WHERE รหัสสมาชิก= '" + TextBox1.Text + "';"
mycommand.CommandText = sqlquery
mycommand.Connection = conn
mycommand.ExecuteNonQuery()
'mydata = mycommand.ExecuteReader
'mydata.Read()
'Else
MsgBox("success")
mycommand.Dispose()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
DateTimePicker1.Text = ""
TextBox1.Focus()
Me.filllistview()
conn.Close()
End If
If status = 3 Then
conn.Open()
sqlquery = "DELETE FROM member WHERE รหัสสมาชิก= '" + TextBox1.Text + "';"
mycommand.CommandText = sqlquery
mycommand.Connection = conn
mycommand.ExecuteNonQuery()
'mydata = mycommand.ExecuteReader
'mydata.Read()
'Else
MsgBox("success")
mycommand.Dispose()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
DateTimePicker1.Text = ""
TextBox1.Focus()
Me.filllistview()
conn.Close()
End If
status = 0
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
TextBox5.Enabled = False
ComboBox1.Enabled = False
ComboBox2.Enabled = False
ComboBox3.Enabled = False
DateTimePicker1.Enabled = False
Button2.Enabled = False
Button4.Enabled = False
Button3.Enabled = True
Button6.Enabled = False
Button5.Enabled = False
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
frmMain.Show()
Me.Close()
End Sub
Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
End Sub
Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
Try
Dim i As Integer = CInt(TextBox4.Text.Trim)
If (ComboBox3.Text <> "") Then
TextBox5.Text = i * CInt(ComboBox3.Text)
End If
Catch ex As Exception
TextBox4.Text = "10"
End Try
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
End Sub
Private Function PaddingConverter() As Integer
Throw New NotImplementedException
End Function
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
End Sub
End Class
Tag : .NET, Ms SQL Server 2008
Date :
2013-02-04 01:10:47
By :
Aonaka
View :
961
Reply :
9
Date :
2013-02-04 01:16:10
By :
Aonaka
Date :
2013-02-04 01:18:27
By :
Aonaka
ค่ะจะลองดูค่ะ ขอบคุณนะคะ
Date :
2013-02-04 16:07:24
By :
Aonaka
เปลี่ยนเเล้วค่ะเป็นเเบบนี้ค่ะ
Date :
2013-02-04 16:32:08
By :
Aonaka
เปลี่ยนเป็นภาษาอังกฤษแล้วค่ะ เเต่ตอนรันเจอปัญหาเดิมค่ะ
Date :
2013-02-04 19:06:15
By :
Aonaka
ใครรู้ช่วยตอบหน่อยค่ะ ต้องเร่งทำโปรเจ็กใกล้ถึงกำหนดสอบเเล้วค่ะ
Date :
2013-02-04 22:21:35
By :
Aonaka
ผิดเรื่อง format วันที่ค่ะลองเช็ครูปแบบวันที่ ๆ insert ลงไป
Date :
2013-02-04 23:15:58
By :
bangbang111
แล้วต้องแก้ยังไงบ้างคะ ต้องเปลี่ยนเป็นอะไร
Date :
2013-02-05 01:19:54
By :
Aonaka
Load balance : Server 01