Object reference not set to an instance of an object what is this mean? Pls Help me.
5 Replies - 592 Views - Last Post: 17 February 2016 - 01:08 PM
#1
I'm encountering this kind of error in my system.
Posted 17 February 2016 - 09:44 AM
Replies To: I'm encountering this kind of error in my system.
#2
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.
Folks could be more helpful if you posted the relevant code where the error is being thrown.
#3
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
#4
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
May want to Dim sql As String rather than leaving it as an Object
#5
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.
#6
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
Page 1 of 1

New Topic/Question
Reply


MultiQuote





|