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 > ช่วยดูcode vb.net ให้หน่อยค่ะ error Object reference not set to an instance of an object.



 

ช่วยดูcode vb.net ให้หน่อยค่ะ error Object reference not set to an instance of an object.

 



Topic : 051810

Guest




Object reference not set to an instance of an object.

Code (VB.NET)
  Sql = "UPDATE TblCheckEvent SET Ch_Id = '" & txtTextBox.Text.Replace("'", "''") & "' " & _
        " ,Ch_DateAccident='" & txtCheckDate.SelectedDate & "' " & _
        " ,Re_Time = '" & txtReTime.SelectedItem.Text & "' " & _
        " ,Ch_TimeAccident = '" & txtTimeAcc.Text & "' " & _
        " ,Ch_StyleEvents = '" & txtStyle.Text & "' " & _
        " ,Ch_Place = '" & txtPlace.Text & "' " & _
        " ,Ch_Circumstance = '" & txtCircum.Text & "' " & _
        " ,Ch_Flag = '" & txtFlag.Text & "' " & _
        " ,Ch_CheifInspector = '" & txtChief.SelectedItem.Text & "' " & _
           " ,Ch_Offer = '" & txtOffer.SelectedItem.Text & "' " & _
            " ,Ch_Receiver = '" & txtReceiver.SelectedItem.Text & "' " & _
        " WHERE Ch_Id = '" & strPeID & "'"




Tag : .NET, VB.NET







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-11-18 00:10:06 By : SherRy View : 11019 Reply : 7
 

 

No. 1

Guest


ข้างบนเป็น code ส่วนที่แจ้ง error

อันนี้เป็น code ทั้งหมดค่ะ

Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO

Partial Public Class WebForm17
    Inherits System.Web.UI.Page
    Dim Service As New WebService1
    Dim ConnStr As String = "server=(local);database=police;uid=police;password=police"
    Dim Conn As New SqlConnection(ConnStr)
    Dim strPeID As String = ""
    Dim Sql As String
    'Dim Sql As String = "SELECT * FROM TblPersonel"
    Dim Cmd As SqlCommand
    'Dim Idx As String = Request.QueryString("id")


    Dim MyDa As New SqlDataAdapter(Sql, Conn)
    Dim Da As New SqlDataAdapter
    Dim Ds As New DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GridView1.AllowPaging = True
        GridView1.PageSize = 5

        If Not Page.IsPostBack Then
            GridView1.DataSource = CreateDataTable()
            GridView1.DataBind()

            'ReadSIdToDdl()
        End If
    End Sub

    '*** DataTable ***'
    Protected Function CreateDataTable() As DataTable
        Dim strConnString As String = "server=(local);database=police;uid=police;password=police"
        Dim objConn As New SqlConnection(strConnString)

        Dim strCmdString As String = "SELECT * FROM [TblCheckEvent]"
        Dim objCmd As New SqlCommand(strCmdString, objConn)

        Dim Dt As New DataTable()
        Dim objAdapter As New SqlDataAdapter(objCmd)
        objAdapter.Fill(Dt)

        Return Dt '*** Return DataTable ***' 
    End Function

    Sub Search(ByVal SqlSearch As String)
        Dim Found As Boolean = False
        Dim DView As DataView
        'Sql = "Select * From Stock Where SId=" + "'" + Request.Form("TId") + "'" + " Order by SId "
        Da = New SqlDataAdapter(SqlSearch, Conn)
        Da.Fill(Ds, "TblCheckEvent")
        DView = Ds.Tables("TblCheckEvent").DefaultView
        If DView.Count <= 0 Then 'ถ้า DView.Count มีค่ามากกว่า 0 แสดงว่ามีข้อมูลรหัสนั้นๆ แล้ว
            Found = False
            MsgBox("ค้นหาข้อมูลไม่พบ")
            GridView1.Visible = False
            Exit Sub
        Else
            Found = True
            GridView1.Visible = True
            'LMsg.Text = "<Center>...มีข้อมูลรหัส :" & TId.Text & "จ้า...</Center>"
            GridView1.DataSource = Ds.Tables("TblCheckEvent").DefaultView
            'GridView1.DataKeyField = "Pe_Id"
            GridView1.DataBind()
            Conn.Close()
        End If
    End Sub


    Sub SearchField()
        If Len(Trim(TxtSearch.Text)) <= 0 Then
            Response.Write("<script language=javascript>alert('กรุณาใส่ข้อมูลสำหรับการค้นหา');</script")
        Else
            If UCase(DdlField.SelectedItem.Value) = UCase("Date") Then
                'Sql = "Select * From TblPersonel Where Pe_Id ="
                Sql = "Select * From TblCheckEvent Where Ch_DateAccident Like"
                Sql = Sql & "'%" & Trim(TxtSearch.Text) & "%'"
            ElseIf UCase(DdlField.SelectedItem.Value) = UCase("StyleEvents") Then
                Sql = "Select * From TblCheckEvent Where Ch_StyleEvents	 Like"  '//Likeเป็นการค้นหาแบบตัวใดตัวหนึ่งก็ได้แต่ต้องให้%ปิดหัวท้าย
                Sql = Sql & "'%" & Trim(TxtSearch.Text) & "%'"
            Else
                Sql = "Select * From TblCheckEvent Where Ch_CheifInspector	 Like"  '//Likeเป็นการค้นหาแบบตัวใดตัวหนึ่งก็ได้แต่ต้องให้%ปิดหัวท้าย
                Sql = Sql & "'%" & Trim(TxtSearch.Text) & "%'"
                'Else
                '    Sql = "Select * From T Where Zipcode ="
                '    Sql = Sql & "'" & Val(TxtSearch.Text) & "'"
            End If
        End If
        Search(Sql)
    End Sub

    Protected Sub DdlField_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DdlField.SelectedIndexChanged
        SearchField()
    End Sub

    Protected Sub BtnGo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnGo.Click
        SearchField()
    End Sub





    Protected Sub DetailsViewBindData()

        Sql = "SELECT * FROM TblCheckEvent WHERE Ch_Id = '" & strPeID & "' "
        Dim dtReader As SqlDataReader
        Cmd = New SqlCommand(Sql, Conn)
        Conn.Open()
        dtReader = Cmd.ExecuteReader()

        '*** BindData to DetailsView ***'
        DetailsView1.DataSource = dtReader
        DetailsView1.DataBind()


        dtReader.Close()
        dtReader = Nothing

    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Conn.Close()
        Conn = Nothing
    End Sub



    Private Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.DataSource = CreateDataTable()
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataBind()
    End Sub


    Private Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.Visible = False
        strPeID = GridView1.DataKeys(e.NewEditIndex).Value.ToString
        DetailsView1.Visible = True
        DetailsViewBindData()
    End Sub

    Private Sub DetailsView1_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs) Handles DetailsView1.ModeChanging
        Select Case e.NewMode
            Case DetailsViewMode.Edit
                Dim lblChId As Label = CType(DetailsView1.FindControl("lblChId"), Label)
                strPeID = lblChId.Text
                DetailsView1.ChangeMode(DetailsViewMode.Edit)
                DetailsViewBindData()

            Case DetailsViewMode.ReadOnly
                'DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
                DetailsView1.Visible = False
                GridView1.Visible = True
                Response.Redirect("CheckEdit.aspx")
        End Select




    End Sub

    Private Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
        Dim txtTextBox As TextBox = CType(DetailsView1.FindControl("TextBox1"), TextBox)
        '*** Old PeID ***'
        Dim lblDate0 As Label = CType(DetailsView1.FindControl("lblDate0"), Label)
        strPeID = lblDate0.Text

        '*** PersonelID ***'
       
        Dim txtCheckDate As eWorld.UI.CalendarPopup = CType(DetailsView1.FindControl("txtEditCheckDate"), eWorld.UI.CalendarPopup)

        '*** Position ***'
        Dim txtReTime As DropDownList = CType(DetailsView1.FindControl("txtEditTime"), DropDownList)
        Dim txtTimeAcc As TextBox = CType(DetailsView1.FindControl("txtEditTimeAcc"), TextBox)
        Dim txtStyle As TextBox = CType(DetailsView1.FindControl("txtEditStyle"), TextBox)
        Dim txtPlace As TextBox = CType(DetailsView1.FindControl("txtEditPlace"), TextBox)
        Dim txtCircum As TextBox = CType(DetailsView1.FindControl("txtEditCircum"), TextBox)
        Dim txtFlag As TextBox = CType(DetailsView1.FindControl("txtEditFlag"), TextBox)
        Dim txtChief As DropDownList = CType(DetailsView1.FindControl("txtEditChief"), DropDownList)
        Dim txtOffer As DropDownList = CType(DetailsView1.FindControl("txtEditOffer"), DropDownList)
        Dim txtReceiver As DropDownList = CType(DetailsView1.FindControl("txtEditReceiver"), DropDownList)
        

        'Dim myDate As Date
        'myDate = CalendarPopup1.SelectedDate
        'Dim strdate As String = myDate.ToString.Substring(0, 10)
        'txtCheckDate.Text = strdate
        Sql = "UPDATE TblCheckEvent SET Ch_Id = '" & txtTextBox.Text.Replace("'", "''") & "' " & _
        " ,Ch_DateAccident='" & txtCheckDate.SelectedDate & "' " & _
        " ,Re_Time = '" & txtReTime.SelectedItem.Text & "' " & _
        " ,Ch_TimeAccident = '" & txtTimeAcc.Text & "' " & _
        " ,Ch_StyleEvents = '" & txtStyle.Text & "' " & _
        " ,Ch_Place = '" & txtPlace.Text & "' " & _
        " ,Ch_Circumstance = '" & txtCircum.Text & "' " & _
        " ,Ch_Flag = '" & txtFlag.Text & "' " & _
        " ,Ch_CheifInspector = '" & txtChief.SelectedItem.Text & "' " & _
           " ,Ch_Offer = '" & txtOffer.SelectedItem.Text & "' " & _
            " ,Ch_Receiver = '" & txtReceiver.SelectedItem.Text & "' " & _
        " WHERE Ch_Id = '" & strPeID & "'"

        Cmd = New SqlCommand(Sql, Conn)

        Conn.Open()
        Cmd.ExecuteNonQuery()


        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
        DetailsView1.Visible = False
        GridView1.Visible = True
        'GridViewBindData()
        Response.Redirect("CheckEdit.aspx")




    End Sub

End Class







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2010-11-18 00:12:02 By : SherRy
 


 

No. 2



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

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

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

Debug ช่วยได้ครับ สามารถ Debug จนถึง Line ที่ Error ได้ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2010-11-18 06:33:19 By : webmaster
 

 

No. 3



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



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


ไม่เจอ object ที่อ้างถึง debug หาเอาครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2010-11-18 09:44:38 By : nooknoname
 


 

No. 4

Guest


ทำการedit ใน DetailView อ่ะค่ะ
แล้วอยากให้ calendarpopup สามารถeditได้ แต่ไมรู้ว่าต้องใช้ code อะไร
เพราะลองใช้แบบตอน add แล้วไม่ได้

นี่คือ code ที่ใช้ add
Dim myDate As Date
myDate = CalendarPopup1.SelectedDate
Dim strdate As String = myDate.ToString.Substring(0, 10)
txtDate.Text = strdate
Sql = Sql & "'" & Trim(txtDate.Text) & "'" & ","

ที่ให้ส่งค่าไปเก็บที่textbox จะได้ไม่มีปัญหาเรื่อง คศ พศ อ่ะค่ะ
แต่พอมา edit ลองทำคล้ายๆกันแล้ว แต่มันไม่รู้จัก carlendarpopup รบกวนพี่ๆช่วยดูให้หน่อยนนะคะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2010-11-18 12:14:21 By : SherRy
 


 

No. 5



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

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

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


จาก error message มันหมายถึงว่า มีการใช้ตัวแปรที่ไม่ได้ประกาศน่ะครับ

แล้วจากที่ลองไล่โค้ดที่แปะมาให้ดู เห็นว่า script ต้องที่คุณบอกว่า error น่ะมันอยู่ใน Private Sub DetailsView1_ItemUpdating

แล้วเห็นว่าตัวแปร Sql เนี่ย ยังไม่ได้ประกาศครับ ลองเปลี่ยนเป็นแบบข้างล้างนี้ดูครับ

Code (VB.NET)
dim Sql as string
Sql = "UPDATE TblCheckEvent SET Ch_Id = '" & txtTextBox.Text.Replace("'", "''") & "' " & _
      " ,Ch_DateAccident='" & txtCheckDate.SelectedDate & "' " & _
      " ,Re_Time = '" & txtReTime.SelectedItem.Text & "' " & _
      " ,Ch_TimeAccident = '" & txtTimeAcc.Text & "' " & _
      " ,Ch_StyleEvents = '" & txtStyle.Text & "' " & _
      " ,Ch_Place = '" & txtPlace.Text & "' " & _
      " ,Ch_Circumstance = '" & txtCircum.Text & "' " & _
      " ,Ch_Flag = '" & txtFlag.Text & "' " & _
      " ,Ch_CheifInspector = '" & txtChief.SelectedItem.Text & "' " & _
         " ,Ch_Offer = '" & txtOffer.SelectedItem.Text & "' " & _
          " ,Ch_Receiver = '" & txtReceiver.SelectedItem.Text & "' " & _
      " WHERE Ch_Id = '" & strPeID & "'"


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2010-11-18 13:59:04 By : salapao_codeman
 


 

No. 6

Guest


ตอนนี้ update ข้อมูลได้แล้ว
แต่จะทำยังไงให้เวลาอัพเดท ดูจาก Ch_Id เป็นหลัก ว่าจะupdate ตัวไหน
ณ ตอนนี้เวลาอัพเดทมันดูจากวันที่อ่ะค่ะ ถ้าวันที่ซ้ำกันมันก็จะเปลี่ยนข้อมูลให้เหมือนกันหมด
ซึ่งจริงๆแล้ว วันที่ซ้ำ แต่ข้อมูลในวันนั้นๆไม่ซ้ำกัน
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2010-11-18 15:04:31 By : SherRy
 


 

No. 7



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



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


ลองดูตรง Sub Pageload ดีๆน่ะค่ะ
ตรงนั้นรู้สึกจะแปลกๆน่ะค่ะ
น่าจะมีอะไรผิดสักอย่าง
ลองดูน่ะค่ะ[b][/b]
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-03-16 08:49:48 By : noname
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ช่วยดูcode vb.net ให้หน่อยค่ะ error 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 อัตราราคา คลิกที่นี่