Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > ช่วยดูโค้ด ปุ่มเพิ่มข้อมูลลงใน DataGridView ทีละแถวให้หน่อย มัน Error และควรจะแก้อย่างไร


 

[.NET] ช่วยดูโค้ด ปุ่มเพิ่มข้อมูลลงใน DataGridView ทีละแถวให้หน่อย มัน Error และควรจะแก้อย่างไร

 
Topic : 117712



โพสกระทู้ ( 216 )
บทความ ( 0 )



สถานะออฟไลน์



Code (VB.NET)
01.Dim cmd As New OleDbCommand
02.        If Not con.State = ConnectionState.Open Then
03.            'open connection if it is not yet open
04.            con.Open()
05.        End If
06.        cmd.Connection = con
07.            'add data to table
08.            cmd.CommandText = "INSERT INTO tb_Out_Product(ID_Product,จำนวน,ชื่อคนยืม,ชื่อคนจ่าย,วันและเวลา)" & _
09.                      "VALUES (" & Me.ComboBox1.Text & ",'" & Me.TextBox3.Text & "','" & _
10.                        Me.TextBox4.Text & "','" & Me.TextBox5.Text & "','" & _
11.                        Me.DateTimePicker1.Text & "')"
12.            cmd.ExecuteNonQuery()
13. 
14.        'close connection
15.        con.Close()



มันErrorตรง cmd.ExecuteNonQuery() ว่า

Code
Syntax error (missing operator) in query expression '202ส้ม'.




Tag : .NET, Ms Access, VB.NET, VS 2012 (.NET 4.x)



ประวัติการแก้ไข
2015-07-10 22:14:13
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2015-07-10 22:12:25 By : peemes101 View : 952 Reply : 4
 

 

No. 1

Guest


รู้สึกว่าตรง VALUES ลืม ' นะVALUES ('" & Me.ComboBox1.Text & "','" &
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-07-10 22:44:03 By : โทรศัพท์พิมยาก
 

 

No. 2



โพสกระทู้ ( 216 )
บทความ ( 0 )



สถานะออฟไลน์


ยังerror ตรง cmd.ExecuteNonQuery() อยู่ดี
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-07-11 00:01:57 By : peemes101
 

 

No. 3



โพสกระทู้ ( 821 )
บทความ ( 0 )



สถานะออฟไลน์


ลองสั่ง
Msgbox(cmd.CommandText)
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-07-11 08:18:39 By : fonfire
 

 

No. 4



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ก่อนหน้านี้ได้ผ่านการเช็คยังครับว่า มี หรือ ไม่มี ข้อมูล ID_Product นี้อยู่

รูปแบบการบันทึก database

check ID ....

if have ID then
Update
else
insert
end if

ในการบึนทึกข้อมูลแต่ละครั้งควรจะมีรูปแบบประมาณนั้ครับ(แล้วแต่ว่าแต่ละคนจะประยุคต์แบบไหน)
ยกตัวอย่างจาก data wizard ครับ


Code (C#)
01.Private Sub InitAdapter()
02.            Me._adapter = New Global.System.Data.OleDb.OleDbDataAdapter()
03.            Dim tableMapping As Global.System.Data.Common.DataTableMapping = New Global.System.Data.Common.DataTableMapping()
04.            tableMapping.SourceTable = "Table"
05.            tableMapping.DataSetTable = "Table1"
06.            tableMapping.ColumnMappings.Add("ID", "ID")
07.            tableMapping.ColumnMappings.Add("type", "type")
08.            tableMapping.ColumnMappings.Add("count", "count")
09.            Me._adapter.TableMappings.Add(tableMapping)
10.            Me._adapter.DeleteCommand = New Global.System.Data.OleDb.OleDbCommand()
11.            Me._adapter.DeleteCommand.Connection = Me.Connection
12.            Me._adapter.DeleteCommand.CommandText = "DELETE FROM `Table1` WHERE ((`ID` = ?) AND ((? = 1 AND `type` IS NULL) OR (`type`" & _
13.                " = ?)) AND ((? = 1 AND `count` IS NULL) OR (`count` = ?)))"
14.            Me._adapter.DeleteCommand.CommandType = Global.System.Data.CommandType.Text
15.            Me._adapter.DeleteCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_ID", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "ID", Global.System.Data.DataRowVersion.Original, False, Nothing))
16.            Me._adapter.DeleteCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("IsNull_type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Original, True, Nothing))
17.            Me._adapter.DeleteCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Original, False, Nothing))
18.            Me._adapter.DeleteCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("IsNull_count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Original, True, Nothing))
19.            Me._adapter.DeleteCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Original, False, Nothing))
20.            Me._adapter.InsertCommand = New Global.System.Data.OleDb.OleDbCommand()
21.            Me._adapter.InsertCommand.Connection = Me.Connection
22.            Me._adapter.InsertCommand.CommandText = "INSERT INTO `Table1` (`type`, `count`) VALUES (?, ?)"
23.            Me._adapter.InsertCommand.CommandType = Global.System.Data.CommandType.Text
24.            Me._adapter.InsertCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Current, False, Nothing))
25.            Me._adapter.InsertCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Current, False, Nothing))
26.            Me._adapter.UpdateCommand = New Global.System.Data.OleDb.OleDbCommand()
27.            Me._adapter.UpdateCommand.Connection = Me.Connection
28.            Me._adapter.UpdateCommand.CommandText = "UPDATE `Table1` SET `type` = ?, `count` = ? WHERE ((`ID` = ?) AND ((? = 1 AND `ty" & _
29.                "pe` IS NULL) OR (`type` = ?)) AND ((? = 1 AND `count` IS NULL) OR (`count` = ?))" & _
30.                ")"
31.            Me._adapter.UpdateCommand.CommandType = Global.System.Data.CommandType.Text
32.            Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Current, False, Nothing))
33.            Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Current, False, Nothing))
34.            Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_ID", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "ID", Global.System.Data.DataRowVersion.Original, False, Nothing))
35.            Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("IsNull_type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Original, True, Nothing))
36.            Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Original, False, Nothing))
37.            Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("IsNull_count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Original, True, Nothing))
38.            Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Original, False, Nothing))
39.        End Sub



Code (C#)
001.Public Overridable Overloads Function GetData() As ฐานข้อมูล1DataSet.Table1DataTable
002.            Me.Adapter.SelectCommand = Me.CommandCollection(0)
003.            Dim dataTable As ฐานข้อมูล1DataSet.Table1DataTable = New ฐานข้อมูล1DataSet.Table1DataTable()
004.            Me.Adapter.Fill(dataTable)
005.            Return dataTable
006.        End Function
007.Public Overridable Overloads Function Delete(ByVal Original_ID As Integer, ByVal Original_type As Global.System.Nullable(Of Integer), ByVal Original_count As Global.System.Nullable(Of Integer)) As Integer
008.            Me.Adapter.DeleteCommand.Parameters(0).Value = CType(Original_ID, Integer)
009.            If (Original_type.HasValue = True) Then
010.                Me.Adapter.DeleteCommand.Parameters(1).Value = CType(0, Object)
011.                Me.Adapter.DeleteCommand.Parameters(2).Value = CType(Original_type.Value, Integer)
012.            Else
013.                Me.Adapter.DeleteCommand.Parameters(1).Value = CType(1, Object)
014.                Me.Adapter.DeleteCommand.Parameters(2).Value = Global.System.DBNull.Value
015.            End If
016.            If (Original_count.HasValue = True) Then
017.                Me.Adapter.DeleteCommand.Parameters(3).Value = CType(0, Object)
018.                Me.Adapter.DeleteCommand.Parameters(4).Value = CType(Original_count.Value, Integer)
019.            Else
020.                Me.Adapter.DeleteCommand.Parameters(3).Value = CType(1, Object)
021.                Me.Adapter.DeleteCommand.Parameters(4).Value = Global.System.DBNull.Value
022.            End If
023.            Dim previousConnectionState As Global.System.Data.ConnectionState = Me.Adapter.DeleteCommand.Connection.State
024.            If ((Me.Adapter.DeleteCommand.Connection.State And Global.System.Data.ConnectionState.Open) _
025.                        <> Global.System.Data.ConnectionState.Open) Then
026.                Me.Adapter.DeleteCommand.Connection.Open()
027.            End If
028.            Try
029.                Dim returnValue As Integer = Me.Adapter.DeleteCommand.ExecuteNonQuery
030.                Return returnValue
031.            Finally
032.                If (previousConnectionState = Global.System.Data.ConnectionState.Closed) Then
033.                    Me.Adapter.DeleteCommand.Connection.Close()
034.                End If
035.            End Try
036.        End Function
037.Public Overridable Overloads Function Insert(ByVal type As Global.System.Nullable(Of Integer), ByVal count As Global.System.Nullable(Of Integer)) As Integer
038.            If (type.HasValue = True) Then
039.                Me.Adapter.InsertCommand.Parameters(0).Value = CType(type.Value, Integer)
040.            Else
041.                Me.Adapter.InsertCommand.Parameters(0).Value = Global.System.DBNull.Value
042.            End If
043.            If (count.HasValue = True) Then
044.                Me.Adapter.InsertCommand.Parameters(1).Value = CType(count.Value, Integer)
045.            Else
046.                Me.Adapter.InsertCommand.Parameters(1).Value = Global.System.DBNull.Value
047.            End If
048.            Dim previousConnectionState As Global.System.Data.ConnectionState = Me.Adapter.InsertCommand.Connection.State
049.            If ((Me.Adapter.InsertCommand.Connection.State And Global.System.Data.ConnectionState.Open) _
050.                        <> Global.System.Data.ConnectionState.Open) Then
051.                Me.Adapter.InsertCommand.Connection.Open()
052.            End If
053.            Try
054.                Dim returnValue As Integer = Me.Adapter.InsertCommand.ExecuteNonQuery
055.                Return returnValue
056.            Finally
057.                If (previousConnectionState = Global.System.Data.ConnectionState.Closed) Then
058.                    Me.Adapter.InsertCommand.Connection.Close()
059.                End If
060.            End Try
061.        End Function
062.Public Overridable Overloads Function Update(ByVal type As Global.System.Nullable(Of Integer), ByVal count As Global.System.Nullable(Of Integer), ByVal Original_ID As Integer, ByVal Original_type As Global.System.Nullable(Of Integer), ByVal Original_count As Global.System.Nullable(Of Integer)) As Integer
063.            If (type.HasValue = True) Then
064.                Me.Adapter.UpdateCommand.Parameters(0).Value = CType(type.Value, Integer)
065.            Else
066.                Me.Adapter.UpdateCommand.Parameters(0).Value = Global.System.DBNull.Value
067.            End If
068.            If (count.HasValue = True) Then
069.                Me.Adapter.UpdateCommand.Parameters(1).Value = CType(count.Value, Integer)
070.            Else
071.                Me.Adapter.UpdateCommand.Parameters(1).Value = Global.System.DBNull.Value
072.            End If
073.            Me.Adapter.UpdateCommand.Parameters(2).Value = CType(Original_ID, Integer)
074.            If (Original_type.HasValue = True) Then
075.                Me.Adapter.UpdateCommand.Parameters(3).Value = CType(0, Object)
076.                Me.Adapter.UpdateCommand.Parameters(4).Value = CType(Original_type.Value, Integer)
077.            Else
078.                Me.Adapter.UpdateCommand.Parameters(3).Value = CType(1, Object)
079.                Me.Adapter.UpdateCommand.Parameters(4).Value = Global.System.DBNull.Value
080.            End If
081.            If (Original_count.HasValue = True) Then
082.                Me.Adapter.UpdateCommand.Parameters(5).Value = CType(0, Object)
083.                Me.Adapter.UpdateCommand.Parameters(6).Value = CType(Original_count.Value, Integer)
084.            Else
085.                Me.Adapter.UpdateCommand.Parameters(5).Value = CType(1, Object)
086.                Me.Adapter.UpdateCommand.Parameters(6).Value = Global.System.DBNull.Value
087.            End If
088.            Dim previousConnectionState As Global.System.Data.ConnectionState = Me.Adapter.UpdateCommand.Connection.State
089.            If ((Me.Adapter.UpdateCommand.Connection.State And Global.System.Data.ConnectionState.Open) _
090.                        <> Global.System.Data.ConnectionState.Open) Then
091.                Me.Adapter.UpdateCommand.Connection.Open()
092.            End If
093.            Try
094.                Dim returnValue As Integer = Me.Adapter.UpdateCommand.ExecuteNonQuery
095.                Return returnValue
096.            Finally
097.                If (previousConnectionState = Global.System.Data.ConnectionState.Closed) Then
098.                    Me.Adapter.UpdateCommand.Connection.Close()
099.                End If
100.            End Try
101.        End Function


ทำ Function ออกมาแบบนี้ก็ดูดีไปอีกแบบ
แต่ต้องออกตัวก่อนว่าข้อมูลข้างต้นมาจาก data wizard ล้วนๆผมไม่ได้คิดเอง 5555
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-07-11 08:31:27 By : lamaka.tor
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ช่วยดูโค้ด ปุ่มเพิ่มข้อมูลลงใน DataGridView ทีละแถวให้หน่อย มัน Error และควรจะแก้อย่างไร
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่