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 > เพิ่ม ลบ แก้ไข ข้อมูลในฐานข้อมูลไม่ได้ แต่ว่าดูข้อมูลได้ดึงข้อมูลมาใช้ฐานข้อมูลSQL



 

เพิ่ม ลบ แก้ไข ข้อมูลในฐานข้อมูลไม่ได้ แต่ว่าดูข้อมูลได้ดึงข้อมูลมาใช้ฐานข้อมูลSQL

 



Topic : 057189



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



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




ก็คือผมทำจัดการข้อมูลแล้วที่นี้ก็เลยเอาฐานข้อมูลมาใส่ที่โปรเจ็คและก็datagridview ซึ่งมันมองเห็นข้อมูลในฐานข้อมูลแต่ว่าพอทำการเพิ่มลบแก้ไขข้อมูลมันทำไม่ได้ โดยมันไม่มีErrorหรือruntime errrorแต่มันแสดงว่าบันทึกได้แต่ข้อมูลไม่ขยับเลยครับ



Tag : .NET, Ms SQL Server 2008, VB.NET, VS 2010 (.NET 4.x)







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-03-12 21:08:41 By : เด็กน้อยขี้ดื้อ View : 1481 Reply : 2
 

 

No. 1



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



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


โค๊ดครับ
Code (VB.NET)
Public Class Drug
    Dim rsg As MsgBoxResult
    Dim id As String
    Dim strConn = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data\DrugStore.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
    'ปุ่มออก
    Private Sub btn_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exit.Click
        'ปิดฟอร์ม
        Me.Close()
    End Sub
    'ค้นหาจาDataGrid
    Private Sub drug2_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles drug2.CellDoubleClick
        Try
            If e.RowIndex = drug2.Rows.Count Then
                Return
            End If
            If e.RowIndex = -1 Then
                Return
            End If
            'นำข้อมูลใน Cell จาก DataGrid มาโชว์ใน Textbox
            id = drug2.Rows(e.RowIndex).Cells(0).Value.ToString()
            txt_id.Text = drug2.Rows(e.RowIndex).Cells(0).Value.ToString()
            txt_name.Text = drug2.Rows(e.RowIndex).Cells(1).Value.ToString()
            com_type.Text = drug2.Rows(e.RowIndex).Cells(2).Value.ToString()
            date_exp.Text = drug2.Rows(e.RowIndex).Cells(3).Value.ToString()
            txt_total.Text = drug2.Rows(e.RowIndex).Cells(4).Value
            txt_unit.Text = drug2.Rows(e.RowIndex).Cells(5).Value.ToString()
            txt_dprice.Text = drug2.Rows(e.RowIndex).Cells(6).Value.ToString()
            txt_low.Text = drug2.Rows(e.RowIndex).Cells(7).Value.ToString()
            txt_exp.Text = drug2.Rows(e.RowIndex).Cells(8).Value.ToString()
            txt_id.ReadOnly = True
            txt_name.Focus()
        Catch ex As Exception
            MessageBox.Show("ผิดพลาด")
        End Try
    End Sub
    'เพิ่มข้อมูลยา
    Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
        'ตรวจสอบว่ามีใน Textbox มีการป้อนค่าว่างหรือไม่
        If txt_id.Text = "" Then
            MessageBox.Show("กรุณาป้อนรหัสยา")
        ElseIf txt_name.Text = "" Then
            MessageBox.Show("กรุณาป้อนชื่อยา")
        ElseIf txt_total.Text = "" Then
            MessageBox.Show("กรุณาป้อนจำนวนยา")
            ElseIf txt_dprice.Text = "" Then
                MessageBox.Show("กรุณาป้อนราคายา")
            ElseIf txt_unit.Text = "" Then
                MessageBox.Show("กรุณาป้อนหน่วยของยา")
            ElseIf com_type.Text = "" Then
                MessageBox.Show("กรุณาป้อนประเภทของยา")
            ElseIf date_exp.Text = "" Then
                MessageBox.Show("กรุณาป้อนวันหมดอายุของยา")
            Else
                'เชื่อมต่อฐานข้อมูล
                Dim conn As New SqlConnection(strConn)
                conn.Open()
                'ค้นหาข้อมูลเจ้าหน้าที่จากรหัสยา
            Dim strSearch As String = "SELECT * FROM DRUG WHERE DRUG_ID = '" & txt_id.Text & "'"
                Try
                    'ถ้ามีรหัสยาในฐานข้อมูลแล้ว
                    rsg = MessageBox.Show("ยืนยันการเพิ่มยารหัส " & txt_id.Text & " " & vbCrLf & "จำนวน : " & txt_total.Text & "" & " " & txt_unit.Text & "หรือไม่", "ยืนยันการเพิ่มจำนวน", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                    If rsg = MsgBoxResult.Yes Then
                        Dim sqlup As New SqlCommand(strSearch, conn)
                        Dim view As New SqlDataAdapter(sqlup)
                        Dim data As New DataSet
                        view.Fill(data, "DRUG")
                        'ดึงข้อมูลยาขึ้นมาเก็บไว้ในตัวแปรเพื่อบวกกับจำนวนยาที่เพิ่มเข้ามา
                        Dim total, old, num As Integer
                        old = data.Tables("DRUG").Rows(0)(8)
                    num = Int(txt_total.Text)
                        total = old + num
                        'นำจำนวนยาที่คำนวณเสร็จแล้วไปแก้ไขลงฐานข้อมูล
                    Dim strUp As String = "UPDATE DRUG SET DRUG_TOTAL=@total WHERE DRUG.DRUG_ID ='" & txt_id.Text & "'"
                        Dim sqlcomm As New SqlCommand(strUp, conn)
                        sqlcomm.Parameters.AddWithValue("total", SqlDbType.Int).Value = total
                        Dim result = sqlcomm.ExecuteNonQuery()
                        If result < 1 Then
                            MessageBox.Show("บันทึกข้อมูลผิดพลาด", "ล้มเหลว", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        Else
                            MessageBox.Show("บันทึกเรียบร้อย", "เรียบร้อย", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            'Clear ค่าใน Textbox และอัพเดทค่าใน DataGrid
                            txt_id.Text = ""
                            txt_dprice.Text = ""
                            txt_exp.Text = ""
                            txt_low.Text = ""
                            txt_name.Text = ""
                            date_exp.Value = Date.Now
                            txt_total.Text = ""
                            txt_unit.Text = ""
                            com_type.Text = ""
                        Me.DRUGTableAdapter.Fill(Me.DrugStoreDataSet.DRUG)
                            txt_id.ReadOnly = False
                            txt_id.Focus()
                        End If
                    End If
                Catch ex As Exception
                    'ถ้ารหัสยาที่รับมาไม่มีในฐานข้อมูล
                    rsg = MessageBox.Show("ยืนยันการบันทึกข้อมูลยารหัส" & txt_id.Text & "หรือไม่", "ยืนยันการบันทึก", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                    If rsg = MsgBoxResult.Yes Then
                        'เพิ่มข้อมูลยาลงในฐานข้อมูล
                    Dim strIn = "INSERT INTO DRUG(DRUG_ID,DRUG_NAME,DRUG_PRICE,DRUG_LOW,DRUG_EXP,DRUG_BEXP,DRUG_UNIT,DRUG_TYPE,DRUG_TOTAL)VALUES(@id,@name,@price,@low,@exp,@bexp,@unit,@type,@total)"
                        Dim sqlIn As New SqlCommand(strIn, conn)
                        With sqlIn
                            .Parameters.Clear()
                            .CommandType = CommandType.Text
                            .CommandText = strIn
                            .Parameters.Add("id", SqlDbType.NChar).Value = txt_id.Text
                            .Parameters.Add("name", SqlDbType.NVarChar).Value = txt_name.Text
                            .Parameters.Add("price", SqlDbType.Money).Value = txt_dprice.Text
                            .Parameters.Add("low", SqlDbType.Int).Value = txt_low.Text
                            .Parameters.Add("exp", SqlDbType.Date).Value = date_exp.Text
                            .Parameters.Add("bexp", SqlDbType.Int).Value = txt_exp.Text
                            .Parameters.Add("unit", SqlDbType.NVarChar).Value = txt_unit.Text
                            .Parameters.Add("type", SqlDbType.NVarChar).Value = com_type.Text
                            .Parameters.Add("total", SqlDbType.Int).Value = txt_total.Text
                            Dim result = sqlIn.ExecuteNonQuery()
                            If result < 1 Then
                                MessageBox.Show("บันทึกข้อมูลผิดพลาด", "ล้มเหลว", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            Else
                                MessageBox.Show("บันทึกเรียบร้อย", "เรียบร้อย", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                ' Clear ค่าใน Textbox และ อัพเดทข้อมูลใน Datagrid
                                txt_id.Text = ""
                                txt_dprice.Text = ""
                                txt_exp.Text = ""
                                txt_low.Text = ""
                                txt_name.Text = ""
                                date_exp.Value = Date.Now
                                txt_total.Text = ""
                                txt_unit.Text = ""
                                com_type.Text = ""
                            Me.DRUGTableAdapter.Fill(Me.DrugStoreDataSet.DRUG)
                                txt_id.ReadOnly = False
                                txt_id.Focus()
                            End If
                        End With
                    End If
                End Try
                conn.Close()
            End If
    End Sub
    'ลบข้อมูลยา
    Private Sub add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add.Click
        'ตรวจสอบว่าค่าที่ป้อนใน Textbox ว่ามีค่าว่างหรือไม่
        If txt_id.Text = "" Then
            MessageBox.Show("กรุณาป้อนรหัสยา")
        Else
            'ยืนยันการลบข้อมูล
            rsg = MessageBox.Show("ต้องการลบยารหัส " & txt_id.Text & "", "ยืนยันการลบข้อมูล", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
            If rsg = MsgBoxResult.Ok Then
                Dim strData As String
                Dim conn As New SqlConnection(strConn)
                conn.Open()
                'ลบข้อมูลยาจากรหัสยา
                strData = "DELETE FROM DRUG WHERE DRUG.DRUG_ID =" & "'" & txt_id.Text & "'"
                Dim strcomm As New SqlCommand(strData, conn)
                Dim result = strcomm.ExecuteNonQuery()
                If result < 1 Then
                    MessageBox.Show("ลบข้อมูลผิดพลาด", "ล้มเหลว", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Else
                    MessageBox.Show("ลบข้อมูลเรียบร้อย", "เรียบร้อย", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    'Clear ค่าใน Textbox และ อัพเดตค่าใน DataGrid
                    txt_id.Text = ""
                    txt_dprice.Text = ""
                    txt_exp.Text = ""
                    txt_low.Text = ""
                    txt_name.Text = ""
                    date_exp.Value = Date.Today
                    txt_total.Text = ""
                    txt_unit.Text = ""
                    com_type.Text = ""
                    txt_id.ReadOnly = False
                    Me.DRUGTableAdapter.Fill(Me.DrugStoreDataSet.DRUG)
                End If
            End If
        End If
    End Sub
    'แก้ไขข้อมูลยา
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' ตรวจสอบว่าค่าใน TextBox ว่ามีค่าว่างหรือไม่
        txt_id.ReadOnly = False
        If txt_id.Text = "" Then
            MessageBox.Show("กรุณาป้อนรหัสยา")
        ElseIf txt_name.Text = "" Then
            MessageBox.Show("กรุณาป้อนชื่อยา")
        ElseIf txt_total.Text = "" Then
            MessageBox.Show("กรุณาป้อนจำนวนยา")
        ElseIf txt_dprice.Text = "" Then
            MessageBox.Show("กรุณาป้อนราคายา")
        ElseIf txt_unit.Text = "" Then
            MessageBox.Show("กรุณาป้อนหน่วยของยา")
        ElseIf com_type.Text = "" Then
            MessageBox.Show("กรุณาป้อนประเภทของยา")
        ElseIf date_exp.Text = "" Then
            MessageBox.Show("กรุณาป้อนวันหมดอายุของยา")
        Else
            'ยืนยันว่าต้องการแก้ไขข้อมูลหรือไม่
            Dim strData As String
            rsg = MessageBox.Show("ยืนยันการแก้ไขข้อมูลยารหัส " & txt_id.Text & "", "ยืนยันการเพิ่มจำนวน", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
            If rsg = MsgBoxResult.Yes Then
                Dim conn As New SqlConnection(strConn)
                conn.Open()
                'แก้ไขข้อมูลยาตามรหัสยา
                strData = "UPDATE DRUG SET DRUG_ID=@id,DRUG_NAME=@name,DRUG_PRICE=@price,DRUG_LOW=@low,DRUG_EXP=@exp,DRUG_BEXP=@bexp,DRUG_UNIT=@unit,DRUG_TYPE=@type,DRUG_TOTAL=@total WHERE DRUG.DRUG_ID ='" & txt_id.Text & "'"
                Dim strcomm As New SqlCommand(strData, conn)
                With strcomm
                    .Parameters.Clear()
                    .CommandType = CommandType.Text
                    .CommandText = strData
                    .Connection = conn
                    .Parameters.AddWithValue("id", SqlDbType.NChar).Value = txt_id.Text
                    .Parameters.AddWithValue("name", SqlDbType.NVarChar).Value = txt_name.Text
                    .Parameters.AddWithValue("price", SqlDbType.Money).Value = txt_dprice.Text
                    .Parameters.AddWithValue("low", SqlDbType.Int).Value = txt_low.Text
                    .Parameters.AddWithValue("exp", SqlDbType.Date).Value = date_exp.Value
                    .Parameters.AddWithValue("bexp", SqlDbType.Int).Value = txt_exp.Text
                    .Parameters.AddWithValue("unit", SqlDbType.NVarChar).Value = txt_unit.Text
                    .Parameters.AddWithValue("type", SqlDbType.NVarChar).Value = com_type.Text
                    .Parameters.AddWithValue("total", SqlDbType.Int).Value = txt_total.Text
                    Dim result = strcomm.ExecuteNonQuery()
                    If result < 1 Then
                        MessageBox.Show("บันทึกข้อมูลผิดพลาด", "ล้มเหลว", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Else
                        MessageBox.Show("บันทึกเรียบร้อย", "เรียบร้อย", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        'Clear ค่าใน Textbox และอัพเดตค่าในDatagrid
                        txt_id.Text = ""
                        txt_dprice.Text = ""
                        txt_exp.Text = ""
                        txt_low.Text = ""
                        txt_name.Text = ""
                        date_exp.Value = Date.Now
                        txt_total.Text = ""
                        txt_unit.Text = ""
                        com_type.Text = ""
                        Me.DRUGTableAdapter.Fill(Me.DrugStoreDataSet.DRUG)
                        txt_id.Focus()
                    End If
                End With
                conn.Close()
            End If
        End If
    End Sub
    Private Sub AddDrug_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DrugStoreDataSet1.DRUG' table. You can move, or remove it, as needed.
        'แสดงข้อมูลจากฐานข้อมูลในDataGrid
        Me.DRUGTableAdapter.Fill(Me.DrugStoreDataSet.DRUG)

    End Sub
End Class







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-03-12 21:49:02 By : เด็กน้อยขี้ดื้อ
 


 

No. 2



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

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

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

การ Debug จะช่วยได้น่ะครับ

.NET Windows Form Application เขียนโปรแกรมบน Windows Form Application ด้วย .NET Framework
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-03-13 09:00:09 By : webmaster
 

   

ค้นหาข้อมูล


   
 

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