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 > เกิดข้อผิดพลาด vb 2010 + access เพิ่ม ลบ แก้ไข ค้นหา ในหน้าเดียวกัน



 

เกิดข้อผิดพลาด vb 2010 + access เพิ่ม ลบ แก้ไข ค้นหา ในหน้าเดียวกัน

 



Topic : 099717



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



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




ช่วยด้วยคับ เพิ่มได้/ลบแก้ไขค้นหาไม่ได้คับ ผิดพลาดอยู่ตรง (dt)
Code (VB.NET)
Imports System.Data.OleDb
Public Class frm_customer
    Dim cnn As New OleDb.OleDbConnection
    Private Sub frm_customer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private Sub RefreshData()
        If Not cnn.State = ConnectionState.Open Then
            'open connection
            cnn.Open()
        End If

        Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Customer ORDER BY ID_cus", cnn)
        Dim dt As New DataTable
        'fill data to datatable
        da.Fill(dt)

        'offer data in data table into datagridview
        Me.dgview.DataSource = dt

        'close connection
        cnn.Close()
    End Sub
    Private Sub frm_customer(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cnn = New OleDb.OleDbConnection
        cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\database.accdb"
        Me.RefreshData()
    End Sub
    Private Function checkdata()

        Dim result As Boolean  'ฟังก์ชั่นการเช็คข้อมูลก่อนบันทึก
        If txt_cus_id.Text = "" Then
            MessageBox.Show("กรุณากรอก รหัสลูกค้า")
            result = False
        ElseIf txt_cus_name.Text = "" Then
            MessageBox.Show("กรุณากรอก ชื่อ")
            result = False
        ElseIf txt_cus_lname.Text = "" Then
            MessageBox.Show("กรุณากรอก นามสกุล")
            result = False
        ElseIf txt_cus_no.Text = "" Then
            MessageBox.Show("กรุณากรอก รหัสประชาชน")
            result = False
        ElseIf date_bd.Text = "" Then
            MessageBox.Show("กรุณากรอก วันเกิด")
            result = False
        ElseIf txt_cus_sex.Text = "" Then
            MessageBox.Show("กรุณากรอก เพศ")
            result = False
        ElseIf txt_cus_nation.Text = "" Then
            MessageBox.Show("กรุณากรอก สัญชาติ")
            result = False
        ElseIf txt_cus_rac.Text = "" Then
            MessageBox.Show("กรุณากรอก เชื้อชาติ")
            result = False
        ElseIf txt_cus_add.Text = "" Then
            MessageBox.Show("กรุณากรอก ที่อยู่")
            result = False
        ElseIf txt_cus_home.Text = "" Then
            MessageBox.Show("กรุณากรอก บ้าน")
            result = False
        ElseIf txt_cus_road.Text = "" Then
            MessageBox.Show("กรุณากรอก ถนน")
            result = False
        ElseIf txt_cus_districe.Text = "" Then
            MessageBox.Show("กรุณากรอก ตำบล")
            result = False
        ElseIf txt_cus_canton.Text = "" Then
            MessageBox.Show("กรุณากรอก อำเภอ")
            result = False
        ElseIf txt_cus_province.Text = "" Then
            MessageBox.Show("กรุณากรอก จังหวัด")
            result = False
        ElseIf txt_cus_pcode.Text = "" Then
            MessageBox.Show("กรุณากรอก รหัสไปรษณีย์")
            result = False
        ElseIf txt_cus_phone.Text = "" Then
            MessageBox.Show("กรุณากรอก เบอร์โทรศัพท์")
            result = False
        Else
            result = True
        End If
        Return result

    End Function
    Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click

        Dim result As Boolean 'กำหนดตัวแปร  result ให้เป็นชนิด Boolean เพื่อใช้ในการเปรียบเทียบ
        result = checkdata() 'ทำการเปรียบเทียบ ตัวแปร result ให้เป็นฟังก์ชั่น checkdata
        If result = True Then 'ถ้า result เท่ากับ checkdata จริงให้โปรแกรทำการบันทึกข้อมูลลงฐานข้อมูล

            Dim cmd As New OleDb.OleDbCommand
            If Not cnn.State = ConnectionState.Open Then
                'open connection if it is not yet open
                cnn.Open()
            End If
            cmd.Connection = cnn
            'check whether add new or update
            If Me.txt_cus_id.Tag & "" = "" Then
                'add new 
                'add data to table
                cmd.CommandText = "INSERT INTO Customer(ID_cus, cus_name, cus_lname, cus_no, cus_bd, cus_sex, cus_nation, cus_race, cus_add, cus_home, cus_road, cus_district, cus_canton, cus_province, cus_pcode, cus_phone) " & _
                                " VALUES(" & Me.txt_cus_id.Text & ",'" & Me.txt_cus_name.Text & "','" & Me.txt_cus_lname.Text & "','" & Me.txt_cus_no.Text & "','" & Me.date_bd.Text & "','" & Me.txt_cus_sex.Text & "','" & Me.txt_cus_nation.Text & "','" & Me.txt_cus_rac.Text & "','" & Me.txt_cus_add.Text & "','" & Me.txt_cus_home.Text & "','" & Me.txt_cus_road.Text & "','" & Me.txt_cus_districe.Text & "','" & Me.txt_cus_canton.Text & "','" & Me.txt_cus_province.Text & "','" & Me.txt_cus_pcode.Text & "','" & Me.txt_cus_phone.Text & "')"
                cmd.ExecuteNonQuery()
            Else
                'update data in table
                cmd.CommandText = "UPDATE student " & _
                            " SET ID_cus=" & Me.txt_cus_id.Text & _
                            ", cus_name='" & Me.txt_cus_name.Text & "'" & _
                            ", cus_lname='" & Me.txt_cus_lname.Text & "'" & _
                            ", cus_no='" & Me.txt_cus_no.Text & "'" & _
                            ", cus_bd='" & Me.date_bd.Text & "'" & _
                            ", cus_sex='" & Me.txt_cus_sex.Text & "'" & _
                            ", cus_nation='" & Me.txt_cus_nation.Text & "'" & _
                            ", cus_race='" & Me.txt_cus_rac.Text & "'" & _
                            ", cus_add='" & Me.txt_cus_add.Text & "'" & _
                            ", cus_home='" & Me.txt_cus_home.Text & "'" & _
                            ", cus_road='" & Me.txt_cus_road.Text & "'" & _
                            ", cus_district='" & Me.txt_cus_districe.Text & "'" & _
                            ", cis_canton='" & Me.txt_cus_canton.Text & "'" & _
                            ", cus_province='" & Me.txt_cus_province.Text & "'" & _
                            ", cus_pcode='" & Me.txt_cus_pcode.Text & "'" & _
                            ", cus_phone='" & Me.txt_cus_phone.Text & "'" & _
                            " WHERE ID_cus=" & Me.txt_cus_id.Tag
                cmd.ExecuteNonQuery()

                'refresh data in list
                RefreshData()
                'close connection
                cnn.Close()
            End If
        End If
    End Sub
    Private Sub cmdclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclose.Click
        Me.Close()
    End Sub

    Private Sub cmdupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupdate.Click
        If Me.dgview.Rows.Count > 0 Then
            If Me.dgview.SelectedRows.Count > 0 Then
                Dim intID_cus As Integer = Me.dgview.SelectedRows(0).Cells("ID_cus").Value
                'get data from database followed by student id
                'open connection
                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
                End If
                'get data into datatable
                Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Customer " & _
                                                     " WHERE ID_cus=" & intID_cus, cnn)
                Dim dt As New DataTable
                da.Fill(dt)

                Me.txt_cus_id.Text = intID_cus
                Me.txt_cus_name.Text = dt.Rows(0).Item("cus_name")
                Me.txt_cus_lname.Text = dt.Rows(0).Item("cus_lname")
                Me.txt_cus_no.Text = dt.Rows(0).Item("cus_no")
                Me.date_bd.Text = dt.Rows(0).Item("cus_bd")
                Me.txt_cus_sex.Text = dt.Rows(0).Item("cus_sex")
                Me.txt_cus_nation.Text = dt.Rows(0).Item("cus_nation")
                Me.txt_cus_rac.Text = dt.Rows(0).Item("cus_race")
                Me.txt_cus_add.Text = dt.Rows(0).Item("cus_add")
                Me.txt_cus_home.Text = dt.Rows(0).Item("cus_home")
                Me.txt_cus_road.Text = dt.Rows(0).Item("cus_road")
                Me.txt_cus_districe.Text = dt.Rows(0).Item("cus_district")
                Me.txt_cus_canton.Text = dt.Rows(0).Item("cus_canton")
                Me.txt_cus_province.Text = dt.Rows(0).Item("cus_province")
                Me.txt_cus_pcode.Text = dt.Rows(0).Item("cus_pcode")
                Me.txt_cus_phone.Text = dt.Rows(0).Item("cus_phone")
                '
                'hide the id to be edited in TAG of txtstdid in case id is changed
                Me.txt_cus_id.Tag = intID_cus
                'change button add to update
                Me.cmdadd.Text = "Update"
                'disable button edit
                Me.cmdupdate.Enabled = False
                'close connection
                cnn.Close()
            End If
        End If
    End Sub

    Private Sub cmddel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmddel.Click

        If Me.dgview.Rows.Count > 0 Then
            If Me.dgview.SelectedRows.Count > 0 Then
                Dim intID_cus As Integer = Me.dgview.SelectedRows(0).Cells("ID_cus").Value
                'open connection
                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
                End If

                'delete data
                Dim cmd As New OleDb.OleDbCommand
                cmd.Connection = cnn
                cmd.CommandText = "DELETE FROM Customer WHERE stdid=" & intID_cus
                cmd.ExecuteNonQuery()
                'refresh data
                Me.RefreshData()

                'close connection
                cnn.Close()
            End If
        End If
    End Sub

    Private Sub cmdclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclear.Click
        Me.txt_cus_id.Text = ""
        Me.txt_cus_name.Text = ""
        Me.txt_cus_lname.Text = ""
        Me.txt_cus_no.Text = ""
        Me.date_bd.Text = ""
        Me.txt_cus_sex.Text = ""
        Me.txt_cus_nation.Text = ""
        Me.txt_cus_rac.Text = ""
        Me.txt_cus_add.Text = ""
        Me.txt_cus_home.Text = ""
        Me.txt_cus_road.Text = ""
        Me.txt_cus_districe.Text = ""
        Me.txt_cus_canton.Text = ""
        Me.txt_cus_province.Text = ""
        Me.txt_cus_pcode.Text = ""
        Me.txt_cus_phone.Text = ""
        'enable button edit
        Me.cmdupdate.Enabled = True
        'set button add to add label
        Me.cmdadd.Text = "Add"
        '
        Me.txt_cus_id.Focus()
    End Sub

    Private Sub cmdsearch_ID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsearch_ID.Click

        cnn.Open()
        Dim myMS As New IO.MemoryStream
        Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Customer " & _
                                            " WHERE stdid=" & Me.txt_cus_id.Text, cnn)
        Dim dt As New DataTable
        da.Fill(dt)

        If dt.Rows.Count > 0 Then
            Me.txt_cus_id.Text = dt.Rows(0).Item("ID_cus") & ""
            Me.cmdadd.Enabled = False
        Else
            MsgBox("Record not found!")
        End If

        cnn.Close()
    End Sub
End Class




Tag : .NET, Ms Access, VB.NET







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-08-28 13:52:02 By : vinai55 View : 8836 Reply : 11
 

 

No. 1



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



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


vb2010






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-28 13:54:50 By : vinai55
 


 

No. 2



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



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


เพิ่มข้อมูลได้ครับ
ส่วนเวลาค้นหา แก้ไข หรือ ลบ จะผิดพลาดตรงนี้ da.Fill(dt)
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-28 13:56:10 By : vinai55
 

 

No. 3



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



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


ขอคำแนะนำด้วยคับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-29 20:34:11 By : vinai55
 


 

No. 4



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



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


มัน Error ว่ายังไงหรอครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-30 10:13:55 By : kaimuk
 


 

No. 5



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



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


มัน Error ยังไงหรอครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-30 10:14:17 By : kaimuk
 


 

No. 6



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



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


ประมาณนี้ครับ
som
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-03 16:35:22 By : vinai55
 


 

No. 7



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



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


แนะนำด้วยคับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-04 11:34:01 By : vinai55
 


 

No. 8



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

สมาชิกที่ใส่เสื้อไทยครีเอท

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

มันบอก Data type mismatch หมายถึงชนิดในฐานข้อมูลกับข้อมูลที่ส่งไป รูปแบบไม่ตรงกันครับ

อีกอย่าง นั้นมันเป็น คำสั่ง select ไม่ใช่ update นิครับ ถ้าจะแก้ไขให้ใช้ update ครับ

Code (VB.NET)
update Custome set fild1='',fild2='' where ID_cus='" & intID_cus & "'


ประมาณนี้ครับ ลองดูครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-04 11:53:58 By : slurpee55555
 


 

No. 9



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



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


โอ้ลืมไปเลยเป็นเพราะ

Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Customer WHERE ID_cus=" & intID_cus, cnn)

ตัวหนาอ่ะครับ เนื่องด้วยใน Database ท่านกำหนดเป็นชนิด String แต่ท่านดันไปส่งข้อมูล เป็น Int มันก็เลย Error ครับ

จากตรงนี้ Dim intID_cus As Integer

ดังนั้นท่านต้องทำ intID_cus เป็น String ก่อนหรือ ใส่บรรทัดนี้ลงไปแทนบรรทัดเก่า

Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Customer WHERE ID_cus='" & intID_cus & "'", cnn)
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-04 16:05:14 By : kaimuk
 


 

No. 10



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



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


ขอบคุณคับ จะลองทำดู
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-04 22:10:53 By : vinai55
 


 

No. 11



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



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


ได้แล้วคับ ขอบคุณคับ มีปัญหาจะมาถามอีกคับ


ประวัติการแก้ไข
2013-09-11 23:40:17
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-11 23:22:53 By : vinai55
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : เกิดข้อผิดพลาด vb 2010 + access เพิ่ม ลบ แก้ไข ค้นหา ในหน้าเดียวกัน
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 00
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 อัตราราคา คลิกที่นี่