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

HOME > .NET Framework > Forum > Object reference not set to an instance of an object. ช่วยดูทีคับมันขึ้นแบบนี้ต้องแก้ไขยังไง



 

Object reference not set to an instance of an object. ช่วยดูทีคับมันขึ้นแบบนี้ต้องแก้ไขยังไง

 



Topic : 093913



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



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




Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Public Class frmJew
    Private conection As SqlConnection
    Private command As SqlCommand
    Private dataSt As DataSet
    Private adapter As SqlDataAdapter
    Private bindingSrc As BindingSource
    Private sql As String
    Private Sub UpdateBinding()
        ComboBox11.DataBindings.Clear()
        ComboBox21.DataBindings.Clear()
        PictureBox11.DataBindings.Clear()

        sql = "SELECT* FROM Jewelry"
        command = New SqlCommand(sql, conection)
        adapter = New SqlDataAdapter(command)
        dataSt = New DataSet()
        adapter.Fill(dataSt, "Jewelry")

        bindingSrc = New BindingSource()
        bindingSrc.DataSource = dataSt.Tables("Jewelry")

        ComboBox11.DataBindings.Add("Text", bindingSrc, "Jew_ID")
        ComboBox21.DataBindings.Add("Text", bindingSrc, "TypeJew_ID")
        
        PictureBox11.DataBindings.Add("Image", bindingSrc, "Picture", True)

    End Sub
    Private Sub frmJew_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim conStr As String = "Data Source=MINI-PC;Initial Catalog=MEO;Integrated Security=True"
        conection = New SqlConnection(conStr)
        conection.Open()

        ButtonSave.Enabled = False
        ComboBox11.Items.AddRange(jew)
        ComboBox11.SelectedIndex = 0
    End Sub
    Public Sub frmjew_FormClosing() Handles MyBase.FormClosing
        conection.Close()
    End Sub
    Private jew() As String = {"ต่างหู", "จี่", "แหวน"}

    Private ry()() As String = {New String() {"ER"},
                                   New String() {"P"},
                                   New String() {"R"}}

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox11.SelectedIndexChanged
        Dim idy As Integer = ComboBox11.SelectedIndex
        ComboBox21.Items.Clear()
        ComboBox21.Items.AddRange(ry(idy))
        ComboBox21.SelectedIndex = 0

    End Sub

    Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
        ComboBox11.Text = ""
        ComboBox21.Text = ""
        'TextMatID.Text = ""
        'TextJewID.Text = ""
        PictureBox11.Image = Nothing

        If ButtonAdd.Text = "เพิ่มข้อมูล" Then
            ButtonAdd.Text = "ยกเลิก"
            ButtonSave.Enabled = True


        ElseIf ButtonAdd.Text = "ยกเลิก" Then

            ButtonAdd.Text = "เพิ่มข้อมูล"
            ButtonSave.Enabled = False

            UpdateBinding()

        End If
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        OpenFileDialog1.Filter = "Image File(*.jpg,*.png,*.gif,*.bmp)|*.jpg;*.png;*.gif;*.bmp"

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PictureBox11.Image = Bitmap.FromFile(OpenFileDialog1.FileName)
        End If
    End Sub

    Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
        If IsDataComplete() = False Then
            Exit Sub
        End If
        sql = "INSERT INTO Jewelry "
        If Not (PictureBox11.Image Is Nothing) Then
            sql &= "(Jew_ID,TypeJew_ID,Picture)" &
            "VALUES(@jew,@type,@pic)"
        Else
            sql &= "(Jew_ID,TypeJew_ID)" &
            "VALUES(@jew,@type)"
        End If


        command.Parameters.Clear()
        command.CommandText = sql
        command.Parameters.AddWithValue("@jew", ComboBox11.Items)
        command.Parameters.AddWithValue("@type", ComboBox21.Items)




        If Not (PictureBox11.Image Is Nothing) Then
            Dim pic() As Byte = ReadImage()
            command.Parameters.AddWithValue("pic", pic)
        End If

        Dim result As Integer = command.ExecuteNonQuery()
        If result = -1 Then
            MessageBox.Show("เกิดข้อผิดพลาดไม่สามารถเพิ่มข้อมูลได้")
        Else
            MessageBox.Show("บันทึกข้อมูลแล้ว")

            ButtonAdd.PerformClick()

        End If
    End Sub
    Private Function ReadImage() As Byte()
        Dim memStream As New IO.MemoryStream()
        PictureBox11.Image.Save(memStream, Drawing.Imaging.ImageFormat.Bmp)
        Return memStream.ToArray()
    End Function
    Private Function IsDataComplete()
        ComboBox11.Text.Trim()
        ComboBox21.Text.Trim()


        If ComboBox11.Text = "" OrElse ComboBox21.Text = "" Then

            MessageBox.Show("กรุณาใส่ข้อมูลให้ครบ")
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub Btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnupdate.Click
        If ComboBox11.Text = "" Or IsDataComplete() = False Then
            Exit Sub
        End If
        Dim s As String
        If Not (PictureBox11.Image Is Nothing) Then
            s = ",Picture = @pic "
        Else
            s = " "
        End If

        sql = "UPDATE Jewelry SET Jew_ID = @jew, TypeJew_ID = @type " & s &
             " WHERE Jew_ID = @jew "



        command.Parameters.Clear()
        command.CommandText = sql
        command.Parameters.AddWithValue("@Type", ComboBox11.Items)
        command.Parameters.AddWithValue("@Jew", ComboBox21.Items)
        command.Parameters.AddWithValue("@Type", ComboBox11.Items)



        If Not (PictureBox11.Image Is Nothing) Then
            Dim byteArr() As Byte = ReadImage()
            command.Parameters.AddWithValue("@pic", byteArr)
        End If

        If command.ExecuteNonQuery() = 1 Then
            MessageBox.Show("บันทึกการเปลี่ยนแปลงแล้ว")
        Else
            MessageBox.Show("เกิดข้อผิดพลาด")
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If ComboBox11.Text = "" Then
            Exit Sub
        End If

        Dim result As DialogResult =
        MessageBox.Show("ท่านต้องการลบข้อมูลแถวนี้จริงหรือไม่", "ยืนยัน",
                          MessageBoxButtons.OKCancel)
        If result = DialogResult.Cancel Then
            Exit Sub
        End If
        sql = "DELETE FROM Jewelry WHERE TypeJew_ID =" & ComboBox11.Text
        command.Parameters.Clear()
        command.CommandText = sql
        command.ExecuteNonQuery()
    End Sub

End Class

'คือผมต้องการที่จะบันทึกข้อมูลในComboboxในฐานข้อมูล แต่มันขึ้นObject reference not set to an instance of an object.
ไม่รู้ว่าผิดพลาดตรงไหนรบกวนผู้รู้ช่วยทีคับ



Tag : .NET, VB.NET







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-04-19 10:15:40 By : tumsonicsnk View : 1105 Reply : 2
 

 

No. 1

Guest


Code
command.Parameters.AddWithValue("@Type", ComboBox11.Items.toString())







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-04-19 10:36:35 By : dekkuza
 


 

No. 2



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



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


Code (VB.NET)
 Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
        If IsDataComplete() = False Then
            Exit Sub
        End If
        sql = "INSERT INTO Jewelry "
        If Not (PictureBox11.Image Is Nothing) Then
            sql &= "(Jew_ID,TypeJew_ID,Picture)" &
            "VALUES(@jew,@type,@pic)"
        Else
            sql &= "(Jew_ID,TypeJew_ID)" &
            "VALUES(@jew,@type)"
        End If


        command.Parameters.Clear()
        command.CommandText = sql
        command.Parameters.AddWithValue("@jew", ComboBox11.Items.ToString())
        command.Parameters.AddWithValue("@Type", ComboBox21.Items.ToString())


'ผมแก้ตามที่บอกแล้วนะครับแต่ก็ยังเหมือนเดิมคับ ช่วยดูอีกทีนะคับ


        If Not (PictureBox11.Image Is Nothing) Then
            Dim pic() As Byte = ReadImage()
            command.Parameters.AddWithValue("pic", pic)
        End If

        Dim result As Integer = command.ExecuteNonQuery()
        If result = -1 Then
            MessageBox.Show("เกิดข้อผิดพลาดไม่สามารถเพิ่มข้อมูลได้")
        Else
            MessageBox.Show("บันทึกข้อมูลแล้ว")

            ButtonAdd.PerformClick()

        End If
    End Sub

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-04-19 10:40:33 By : tumsonicsnk
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Object reference not set to an instance of an object. ช่วยดูทีคับมันขึ้นแบบนี้ต้องแก้ไขยังไง
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 04
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 อัตราราคา คลิกที่นี่