I am looking to make a database call based on user input. The line I am using is:
general_read.CommandText = "SELECT * FROM general WHERE Business_Name = '" & business_name & "';"
business_name is dynamically loaded from:
Protected Sub btnLoad_Click(sender As Object, e As System.EventArgs) Handles btnLoad.Click
business_name = txtName.Text
dbConnect()
dbGet()
dbDisconnect()
End Sub
The error that is being shown is:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
I’m assuming it has something to do with the variable not being set before the database call is made, but I’m not entirely sure.
I’m guessing it’s failing on
general_read.CommandText = "SELECT * FROM general WHERE Business_Name = '" & business_name & "';"since this is the code you specified. If that’s the case, then you didn’t properly initialize the SQLCommand object. Below is the MSDN page that properly shows you how to use all of the overloaded constructors for the object.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx
If business_name is null, an exception will also be thrown when you try to concatenate your ComamndText string. Put a break-point on that line and see what is null. That’s where your problem lies.
Hope this helps. If it’s not erroring on that line, please include the code where it does.