5 Replies - 592 Views - Last Post: 17 February 2016 - 01:08 PM Rate Topic: -----

#1 cookit3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-February 16

I'm encountering this kind of error in my system.

Posted 17 February 2016 - 09:44 AM

Object reference not set to an instance of an object what is this mean? Pls Help me.
Is This A Good Question/Topic? 0
  • +

Replies To: I'm encountering this kind of error in my system.

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: I'm encountering this kind of error in my system.

Posted 17 February 2016 - 09:47 AM

It means you are using an object that has not been instantiated. (aka using 'new' or assigning a value)

Folks could be more helpful if you posted the relevant code where the error is being thrown.
Was This Post Helpful? 0
  • +
  • -

#3 cookit3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-February 16

Re: I'm encountering this kind of error in my system.

Posted 17 February 2016 - 09:53 AM

Imports System.Data.SqlClient
Public Class Form2
    Dim dbdataset As New DataTable

    Dim cmd As SqlCommand
    Dim myDS As DataSet
    Dim myDA As SqlDataAdapter
    Dim source1 As New BindingSource()
    Dim rd As SqlDataReader
    Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\user\Desktop\try\try\logindb.mdf;Integrated Security=True;User Instance=True")

    Private Sub btnlog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlog.Click


        If con.State = ConnectionState.Closed Then con.Open()
        Try

            Dim sql = "Select Username,Password From tblogin Where Username = '" & txtuname.Text & "' and Password = '" & txtpass.Text & "' "
            cmd = New SqlCommand(sql, con)

            rd = cmd.ExecuteReader
            If rd.Read = True Then
                Form4.Show()
                Me.Hide()

            Else
                MessageBox.Show("Username/Password Invalid")
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub


When i hit my login button,it did not continue to my another form because of the error i encountering.

This post has been edited by IronRazer: 17 February 2016 - 11:07 AM
Reason for edit:: Added Code Tags - Please remember to use them

Was This Post Helpful? 0
  • +
  • -

#4 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 592
  • View blog
  • Posts: 1,587
  • Joined: 02-November 11

Re: I'm encountering this kind of error in my system.

Posted 17 February 2016 - 12:53 PM

What line is the error on?

May want to Dim sql As String rather than leaving it as an Object
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: I'm encountering this kind of error in my system.

Posted 17 February 2016 - 01:01 PM

If you disable the Catch block then you'll be able to press Break and discover what line caused the error.
Was This Post Helpful? 0
  • +
  • -

#6 dday9   User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 495
  • Joined: 17-April 13

Re: I'm encountering this kind of error in my system.

Posted 17 February 2016 - 01:08 PM

Take a look at this approach and see if it better suits you:
Private Sub btnlog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlog.Click
    If Me.Login(txtuname.Text, txtpass.Text) Then
        Form4.Show()
        Me.Hide()
    Else
        MessageBox.Show("Invalid login. Please check your username and/or password and try again.", "Invalid Login", MessageBoxButtons.OK)
    End If 
End Sub

Private Function Login(ByVal username As String, ByVal password As String) As Boolean
    Dim count As Integer = -1

    'Declare the connection object
    Dim con As SqlConnection

    'Wrap code in Try/Catch
    Try
        'Set the connection object to a new instance
        'TODO: Change "My Connection String Here" with a valid connection string
        con = New SqlConnection("My Connection String Here")

        'Create a new instance of the command object
        'The command will be to return the amount of rows where the username and password columns equal some value
        Using cmd As SqlCommand = New SqlCommand("SELECT Count([ID]) FROM [tblogin] WHERE [Username][email protected] AND [Password][email protected]", con)
            'Parameterize the query
            cmd.Parameters.AddWithValue("@user", username)
            cmd.Parameters.AddWithValue("@pass", password)

            'Open the connection
            con.Open()

            'Use ExecuteScalar to return the count
            count = Convert.ToInt32(cmd.ExecuteScalar())

            'Close the connection
            con.Close()
        End Using
    Catch ex As Exception
        'Display the error
        MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK)
    Finally
        'Check if the connection object was initialized
        If con IsNot Nothing Then
            If con.State = ConnectionState.Open Then
                'Close the connection if it was left open(exception thrown)
                con.Close()
            End If

            'Dispose of the connection object
            con.Dispose()
        End If
    End Try

    'Return if the row count is greater than 0
    Return count > 0
End Function

This post has been edited by dday9: 17 February 2016 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1