Object reference not set to an instance of an object

Last post 11-06-2008 6:12 PM by Haissam. 2 replies.

Sort Posts:

  • Object reference not set to an instance of an object

    11-05-2008, 2:02 AM
    • Member
      5 point Member
    • burns89
    • Member since 10-24-2008, 7:46 AM
    • Posts 15

    Hi guys.. i need help please... im new and already pulling my hair out!

    error -

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 38: 
    Line 39:         Dim objAnswer As Object = comm.ExecuteScalar()
    Line 40:         TextBox1.Text = objAnswer.ToString()
    Line 41:         conn.Close()
    Line 42:     End Sub

    Source File: D:\Sean\JVX\welcomechange.aspx.vb    Line: 40

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
       welcomechange.TextBox1_Load(Object sender, EventArgs e) in D:\Sean\JVX\welcomechange.aspx.vb:40
       System.Web.UI.Control.OnLoad(EventArgs e) +99
       System.Web.UI.Control.LoadRecursive() +47
       System.Web.UI.Control.LoadRecursive() +131
       System.Web.UI.Control.LoadRecursive() +131
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436
    

     

    and here's my code -

    1    Imports System.Data.SqlClient
    2    Imports System.Configuration
    3    
    4    Partial Class welcomechange
    5        Inherits System.Web.UI.Page
    6    
    7    
    8        Dim welcJVR As String = ConfigurationManager.ConnectionStrings("JVRwelcome").ConnectionString
    9    
    10       Sub welcome()
    11           Dim conn As New SqlConnection(welcJVR)
    12           Dim comm As New SqlCommand("welcome", conn)
    13           conn = New SqlConnection(welcJVR)
    14           conn.Open()
    15           comm = New SqlCommand("update JVR_welcome set welcomenote=('" & TextBox1.Text & "') where id=1 ")
    16           comm.Connection = conn
    17           comm.ExecuteNonQuery()
    18           conn.Close()
    19   
    20   
    21   
    22       End Sub
    23   
    24       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    25           welcome()
    26           Response.Redirect("admin.asp")
    27       End Sub
    28   
    29       Protected Sub TextBox1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Load
    30   
    31           Dim conn As New SqlConnection(welcJVR)
    32           Dim comm As New SqlCommand("welcomefill", conn)
    33           conn = New SqlConnection(welcJVR)
    34           conn.Open()
    35           comm = New SqlCommand("select welcomenote from JVR_welcome")
    36           comm.Connection = conn
    37   
    38   
    39           Dim objAnswer As Object = comm.ExecuteScalar()
    40           TextBox1.Text = objAnswer.ToString()
    41           conn.Close()
    42       End Sub
    43   End Class
    44   
    
      

    thanks for any help:)

  • Re: Object reference not set to an instance of an object

    11-05-2008, 4:02 AM
    Answer
    • All-Star
      20,984 point All-Star
    • venkatu2005
    • Member since 07-01-2008, 10:48 AM
    • Posts 4,558

     hi

    36           comm.Connection = conn
    37
    38
    39 Dim objAnswer As Object = comm.ExecuteScalar()
    40 TextBox1.Text = objAnswer.ToString()
    41 conn.Close()
    try this, look the query, you should write something like this..
    strQuery="select welcomenote from jvr_welcome where id=2"
    dim dr as SqlDatareader
    dr=cmd.executereader()
     while dr.read()
    textbox1.text=dr("welcomenote").ToString()
     end while 

     

    I have Changed My Blog from (http://venkat-dotnetsamples.blogspot.com) to (http://venkat-dotnetsnippets.blogspot.com)

    Regards,
    Venkatesan.M

    Please Mark as Answered If its helpful and Un-Mark as Answered if it not help u.
  • Re: Object reference not set to an instance of an object

    11-06-2008, 6:12 PM
    Answer
    • All-Star
      37,391 point All-Star
    • Haissam
    • Member since 10-05-2006, 2:25 AM
    • Beirut - Lebanon
    • Posts 5,632

    objAnswer is null and when you are converting it to string you are getting this error. Try to check if objAnswer is not null before writing its value to the textbox

    if(objAnswer != null)

    TextBox.Text = objAnswer.ToString();

     

    Haissam Abdul Malak
    MCAD.NET
    | Blog |
Page 1 of 1 (3 items)