-2

How do I handle this type of error or exception?

  Try
    If log.Trim = txtUSN.Text Then
        MessageBox.Show("USN found: " & log)
    Else
        MessageBox.Show("USN not found: " & log)
    End If
  Catch ex as Exception
    MessageBox.Show(ex.Message)

The message was "Object reference not set to an instance of an object."

This is the rest of the code:

    Dim log As String
    Dim sql As New SqlCommand

    sql.Connection = MyConnection
    sql.CommandText = "SELECT * FROM dbo.tblAcc WHERE USN = '" & txtUSN.Text & "' "

    MyConnection.Open()
    log = sql.ExecuteScalar
    MyConnection.Close()
johnprima
  • 25
  • 1
  • 10
  • My best guess is "log" is nothing and your trying to use the .Trim function on it and use it as part of a string and it's nothing. Why don't you just check if it's nothing: If log IsNot Nothing Then... also set a breakpoint in your code, you'll learn a lot more. – Trevor Sep 17 '14 at 03:15
  • Also your missing an End Try, might have got cut off... – Trevor Sep 17 '14 at 03:18
  • What is log anyways? – Trevor Sep 17 '14 at 03:18
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Sep 17 '14 at 10:08

2 Answers2

0

The simple answer is your trying to use an object that is nothing. If it's nothing you can't use it, hence "Object reference not set to an instance of an object."

As already mentioned in my comments above, the culprit is: log. I'm not sure where you have declared this or when your using it and how your using it, for all I know it's nothing. If you have more code that would be greatly appreciated as I can point out where it's nothing, until then here's how to get around your issue.

 Try
  If log IsNot Nothing Then
   If log.Trim = txtUSN.Text Then
    MessageBox.Show("USN found: " & log)
   Else
    MessageBox.Show("USN not found: " & log)
   End If
  Else
     MessageBox.Show("Log is NOTHING!")
  End If 
 Catch ex as Exception
    MessageBox.Show(ex.Message)
 End Try

**EDIT**

After you posted more code in a comment (please post code in the area, not in comment's) it seem's there are a few issue's. You have log defined as a string; when you do this set it to something like: String.Empty instead of nothing. Also you want to sse the ExecuteScalar method to retrieve a single value (for example, an aggregate value) from a database which could be an integer, long, single etc data types. In your query your selecting everything, you can't call ExecuteScalar to return that data... I would recommend looking up information about building queries and executing them, it's to long for me to get in depth with it here.

Happy Coding!

Trevor
  • 7,777
  • 6
  • 31
  • 50
  • I edited my post so you could see the rest of the codes, I'll try the one above and thanks for the help. I somewhat did something similar to that but it didn't work XD – johnprima Sep 17 '14 at 04:34
  • It worked, but I want to know why the else statement didn't work. My objective is to try to get the USN from the database, if it isn't in the database then the else statement would run. – johnprima Sep 17 '14 at 04:46
  • That's because 'log' was NOTHING that's why. It will not continue to execute code if there's an exception in your case, it stops. My code checks to see if it is something, big difference. – Trevor Sep 17 '14 at 04:49
  • @johnprima your welcome glad I could help. Remember if this has helped you please don't forget to vote. Thanks! – Trevor Sep 17 '14 at 04:58
  • I'm new, I think I can't vote yet. It says "Vote up requires 15 reputation" – johnprima Sep 17 '14 at 05:00
  • Just to clarify, you _can_ call `ExecuteScalar` with the type of query that the OP used. It just returns the first column of the first row of the data returned, ignoring all other columns and rows. – Chris Dunaway Sep 17 '14 at 14:16
  • Correct but he wants everything according to his query...I guess I should have reworded that. – Trevor Sep 17 '14 at 18:39
  • I changed the * to a specific column. – johnprima Sep 18 '14 at 00:25
  • @ChrisDunaway, if I wasn't using ExecuteScalar, what is the correct syntax to call/return all columns/rows? – johnprima Sep 18 '14 at 00:28
  • ExecuteNonQuery is what you need... you'll also have to change your object type to use a datatable. .. – Trevor Sep 18 '14 at 01:41
  • @johnprima - If you wanted all rows and columns from your query, you would use `ExecuteReader` which would return an instance of a DataReader. – Chris Dunaway Sep 18 '14 at 13:33
0

Make sure that (log) is not an empty string.

if not String.IsNullorEmpty(log) then
end if
Ala
  • 1,505
  • 1
  • 20
  • 36