Originally Posted by
GanglyLamb
I was only pointing out where your error might come from, the error you are getting would be the same as doing something like
Code:
public string a;
public void DoSomething() {
int lengtOfString = this.a.Length; //debugger will spit "object reference not set to an instance of an object"
}
You will get the same error as the one you are receiving.
Reason the stirng a is declared but not initialised , so it just a variable which points to no object at this time. Only by initialising it , it will point to the object ...
Since the error comes from the first place where you want to use the object aCommand , then it means that the object aCommand is not initialised, which means you should go and take a look at the initialisation of that object which happens at
Code:
OleDbCommand aCommand = new OleDbCommand(query, aConnection);
So how is it then possible that this object not correctly initialised ... probably by the parameters you use at the object's constructor.
heres how I declare aCommand
Code:
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\whatever");
I already tested the query manually it works so i don't see how the initialization of the object is the problem.
The function actually works fine if the query returns one record, it is when the query returns no records that the problem occuers. I thought it might just return a null string or something, but appearently not. I don't know how to process the "nothing"
I tried doing this, it also didn't work
Code:
public string getScalar (string query)
{
OleDbCommand aCommand = new OleDbCommand(query, aConnection);
aConnection.Open();
object num = aCommand.ExecuteScalar()/*.ToString()*/;
Console.WriteLine(num.GetType());
string none = "none";
if(num.GetType() != none.GetType())
{
return none;
}
aConnection.Close();
return num.ToString();
}