Results 1 to 8 of 8

Thread: Object reference not set to an instance of an objec

  1. #1

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Question Object reference not set to an instance of an objec

    I created a subroutine that inserts data from controls into a sql database. I've done this a million times in the past, and never had this problem.

    Now i'm getting a 'Object reference not set to an instance of an object' error when I call the subroutine to a button click on my webform.

    Its something in the sub, because when i take the sub out and throw a label1.text="Anything" it works.

    I'm kinda noobish I can post code or my stack trace if needed

    Thanks for your help!!!

  2. #2

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    STACK TRACE
    [NullReferenceException: Object reference not set to an instance of an object.]
    TransouthPOP.addadmins.saveAdmin() +49
    TransouthPOP.addadmins.btnAdminSubmit_Click(Object sender, EventArgs e) +7
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
    System.Web.UI.Page.ProcessRequestMain() +1277


    SUBROUTINE
    Private Sub saveAdmin()
    Try
    sqlconn.Open()
    With sqlcomm
    .CommandType = CommandType.StoredProcedure
    .CommandText = "sp_AddAdmin"
    .Connection = sqlconn
    .Parameters.Add("@username", txtAdminUser.Text)
    .Parameters.Add("@pw", txtAdminPass1.Text)
    .Parameters.Add("@fullname", txtAdminName.Text)
    .Parameters.Add("@email", txtAdminEmail.Text)
    .ExecuteNonQuery()
    End With
    sqlconn.Close()
    Finally
    sqlconn = Nothing : sqlcomm = Nothing
    End Try

    End Sub

  3. #3
    Member
    Join Date
    Apr 2004
    Posts
    44
    did you declare sqlcomm as a new command first?

    Dim sqlcomm As New SqlClient.SqlCommand

  4. #4

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Thumbs up

    Dude you are the man,


    I had it like this:

    dim sqlcomm as SqlClient.SqlCommand

    I put NEW in it and it worked.

    Like i said I'm a NOOB So whats the difference between putting new and not?


    THANK YOU BY THE WAY!!!

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Using New creates the object. Not using it only tells the compiler to get ready for an object at some point, i.e. save some memory for it.
    You should probably add a catch to your try block, that's the reason to use it - to catch an error and deal with it somehow.

  6. #6

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    Awsome,

    now i noticed catches are used to put errors in the debugging console, correct? or am I way off?

    I'm making a webform, and would have to use a little javascript to accomplish an alert window.

    where would i catch these errors to when building a webform?

    Thanks again!


    <-- when will i ever use this?????

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I don't build webforms, but catches help with debugging, yes, but also allow you to deal with the error. If there's an error, you can run some different code, give the user the option to try again, etc.

  8. #8

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591
    Thanks guys!
    Thats awsome!

    Expect to see me around alot more

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width