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,027

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



 

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

 



Topic : 117712



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



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




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

        'close connection
        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 : 861 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,436 )
บทความ ( 23 )



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

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

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

check ID ....

if have ID then
Update
else
insert
end if

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


Code (C#)
Private Sub InitAdapter()
            Me._adapter = New Global.System.Data.OleDb.OleDbDataAdapter()
            Dim tableMapping As Global.System.Data.Common.DataTableMapping = New Global.System.Data.Common.DataTableMapping()
            tableMapping.SourceTable = "Table"
            tableMapping.DataSetTable = "Table1"
            tableMapping.ColumnMappings.Add("ID", "ID")
            tableMapping.ColumnMappings.Add("type", "type")
            tableMapping.ColumnMappings.Add("count", "count")
            Me._adapter.TableMappings.Add(tableMapping)
            Me._adapter.DeleteCommand = New Global.System.Data.OleDb.OleDbCommand()
            Me._adapter.DeleteCommand.Connection = Me.Connection
            Me._adapter.DeleteCommand.CommandText = "DELETE FROM `Table1` WHERE ((`ID` = ?) AND ((? = 1 AND `type` IS NULL) OR (`type`" & _
                " = ?)) AND ((? = 1 AND `count` IS NULL) OR (`count` = ?)))"
            Me._adapter.DeleteCommand.CommandType = Global.System.Data.CommandType.Text
            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))
            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))
            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))
            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))
            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))
            Me._adapter.InsertCommand = New Global.System.Data.OleDb.OleDbCommand()
            Me._adapter.InsertCommand.Connection = Me.Connection
            Me._adapter.InsertCommand.CommandText = "INSERT INTO `Table1` (`type`, `count`) VALUES (?, ?)"
            Me._adapter.InsertCommand.CommandType = Global.System.Data.CommandType.Text
            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))
            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))
            Me._adapter.UpdateCommand = New Global.System.Data.OleDb.OleDbCommand()
            Me._adapter.UpdateCommand.Connection = Me.Connection
            Me._adapter.UpdateCommand.CommandText = "UPDATE `Table1` SET `type` = ?, `count` = ? WHERE ((`ID` = ?) AND ((? = 1 AND `ty" & _
                "pe` IS NULL) OR (`type` = ?)) AND ((? = 1 AND `count` IS NULL) OR (`count` = ?))" & _
                ")"
            Me._adapter.UpdateCommand.CommandType = Global.System.Data.CommandType.Text
            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))
            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))
            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))
            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))
            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))
            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))
            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))
        End Sub



Code (C#)
Public Overridable Overloads Function GetData() As ฐานข้อมูล1DataSet.Table1DataTable
            Me.Adapter.SelectCommand = Me.CommandCollection(0)
            Dim dataTable As ฐานข้อมูล1DataSet.Table1DataTable = New ฐานข้อมูล1DataSet.Table1DataTable()
            Me.Adapter.Fill(dataTable)
            Return dataTable
        End Function
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
            Me.Adapter.DeleteCommand.Parameters(0).Value = CType(Original_ID, Integer)
            If (Original_type.HasValue = True) Then
                Me.Adapter.DeleteCommand.Parameters(1).Value = CType(0, Object)
                Me.Adapter.DeleteCommand.Parameters(2).Value = CType(Original_type.Value, Integer)
            Else
                Me.Adapter.DeleteCommand.Parameters(1).Value = CType(1, Object)
                Me.Adapter.DeleteCommand.Parameters(2).Value = Global.System.DBNull.Value
            End If
            If (Original_count.HasValue = True) Then
                Me.Adapter.DeleteCommand.Parameters(3).Value = CType(0, Object)
                Me.Adapter.DeleteCommand.Parameters(4).Value = CType(Original_count.Value, Integer)
            Else
                Me.Adapter.DeleteCommand.Parameters(3).Value = CType(1, Object)
                Me.Adapter.DeleteCommand.Parameters(4).Value = Global.System.DBNull.Value
            End If
            Dim previousConnectionState As Global.System.Data.ConnectionState = Me.Adapter.DeleteCommand.Connection.State
            If ((Me.Adapter.DeleteCommand.Connection.State And Global.System.Data.ConnectionState.Open) _
                        <> Global.System.Data.ConnectionState.Open) Then
                Me.Adapter.DeleteCommand.Connection.Open()
            End If
            Try
                Dim returnValue As Integer = Me.Adapter.DeleteCommand.ExecuteNonQuery
                Return returnValue
            Finally
                If (previousConnectionState = Global.System.Data.ConnectionState.Closed) Then
                    Me.Adapter.DeleteCommand.Connection.Close()
                End If
            End Try
        End Function
Public Overridable Overloads Function Insert(ByVal type As Global.System.Nullable(Of Integer), ByVal count As Global.System.Nullable(Of Integer)) As Integer
            If (type.HasValue = True) Then
                Me.Adapter.InsertCommand.Parameters(0).Value = CType(type.Value, Integer)
            Else
                Me.Adapter.InsertCommand.Parameters(0).Value = Global.System.DBNull.Value
            End If
            If (count.HasValue = True) Then
                Me.Adapter.InsertCommand.Parameters(1).Value = CType(count.Value, Integer)
            Else
                Me.Adapter.InsertCommand.Parameters(1).Value = Global.System.DBNull.Value
            End If
            Dim previousConnectionState As Global.System.Data.ConnectionState = Me.Adapter.InsertCommand.Connection.State
            If ((Me.Adapter.InsertCommand.Connection.State And Global.System.Data.ConnectionState.Open) _
                        <> Global.System.Data.ConnectionState.Open) Then
                Me.Adapter.InsertCommand.Connection.Open()
            End If
            Try
                Dim returnValue As Integer = Me.Adapter.InsertCommand.ExecuteNonQuery
                Return returnValue
            Finally
                If (previousConnectionState = Global.System.Data.ConnectionState.Closed) Then
                    Me.Adapter.InsertCommand.Connection.Close()
                End If
            End Try
        End Function
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
            If (type.HasValue = True) Then
                Me.Adapter.UpdateCommand.Parameters(0).Value = CType(type.Value, Integer)
            Else
                Me.Adapter.UpdateCommand.Parameters(0).Value = Global.System.DBNull.Value
            End If
            If (count.HasValue = True) Then
                Me.Adapter.UpdateCommand.Parameters(1).Value = CType(count.Value, Integer)
            Else
                Me.Adapter.UpdateCommand.Parameters(1).Value = Global.System.DBNull.Value
            End If
            Me.Adapter.UpdateCommand.Parameters(2).Value = CType(Original_ID, Integer)
            If (Original_type.HasValue = True) Then
                Me.Adapter.UpdateCommand.Parameters(3).Value = CType(0, Object)
                Me.Adapter.UpdateCommand.Parameters(4).Value = CType(Original_type.Value, Integer)
            Else
                Me.Adapter.UpdateCommand.Parameters(3).Value = CType(1, Object)
                Me.Adapter.UpdateCommand.Parameters(4).Value = Global.System.DBNull.Value
            End If
            If (Original_count.HasValue = True) Then
                Me.Adapter.UpdateCommand.Parameters(5).Value = CType(0, Object)
                Me.Adapter.UpdateCommand.Parameters(6).Value = CType(Original_count.Value, Integer)
            Else
                Me.Adapter.UpdateCommand.Parameters(5).Value = CType(1, Object)
                Me.Adapter.UpdateCommand.Parameters(6).Value = Global.System.DBNull.Value
            End If
            Dim previousConnectionState As Global.System.Data.ConnectionState = Me.Adapter.UpdateCommand.Connection.State
            If ((Me.Adapter.UpdateCommand.Connection.State And Global.System.Data.ConnectionState.Open) _
                        <> Global.System.Data.ConnectionState.Open) Then
                Me.Adapter.UpdateCommand.Connection.Open()
            End If
            Try
                Dim returnValue As Integer = Me.Adapter.UpdateCommand.ExecuteNonQuery
                Return returnValue
            Finally
                If (previousConnectionState = Global.System.Data.ConnectionState.Closed) Then
                    Me.Adapter.UpdateCommand.Connection.Close()
                End If
            End Try
        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 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

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