Thread: OleDbCommand.ExecuteScalar return problems

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    85

    OleDbCommand.ExecuteScalar return problems

    Code:
    			
    			public string getScalar (string query)
    			{
    					OleDbCommand aCommand = new OleDbCommand(query, aConnection);
    					aConnection.Open();
    					string s= aCommand.ExecuteScalar().ToString();
    					aConnection.Close();
    					return s;
    			}
    The problem is when the query returns nothing. I tried a s == NULL case , but it said null was not valid for a string or something. I also tried saving the return as an "object" type, not exactly sure how that works, then converting it to a string only if the object is not NULL, that didn't work either.

    any help appriciated. Thanks.

  2. #2
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Try checking the Length of the string to see if it has anything in it.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    What command are you running?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    the length of string thing does not work, the program is crashing on the executeScalar function.

    The debugger says "Object reference not set to an instance of an object." I'm not that familiar with c# so i don't know what the problem is.

    The query is a select from where, with the db i'm using it should only return 1 or 0 records.

    what I'm trying to do is get the index if the record exists, else insert the record.

    thanks for the help.

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Quote Originally Posted by qwertiop
    the length of string thing does not work, the program is crashing on the executeScalar function.

    The debugger says "Object reference not set to an instance of an object." I'm not that familiar with c# so i don't know what the problem is.
    According to the message
    Code:
    OleDbCommand aCommand = new OleDbCommand(query, aConnection);
    this is where the error comes from, it can have two causes, either the aConnection is nt initialised correctly or your query is not valid .

    First check your query manually ( like with mySQL you can execute the query in the mySQLConsole ).
    If it returns something then probably it has something to do with aConnection...

    And to be honest if you don't know what the meaning is of the error the debugger gave you then i'm wondering what you are doing with oleDB connections already ...

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    Quote Originally Posted by GanglyLamb
    According to the message
    Code:
    OleDbCommand aCommand = new OleDbCommand(query, aConnection);
    this is where the error comes from, it can have two causes, either the aConnection is nt initialised correctly or your query is not valid .

    First check your query manually ( like with mySQL you can execute the query in the mySQLConsole ).
    If it returns something then probably it has something to do with aConnection...

    And to be honest if you don't know what the meaning is of the error the debugger gave you then i'm wondering what you are doing with oleDB connections already ...
    I'm not sure you understand what I'm trying to do. The query works, I already tested it manually. I want it to run a query that may return nothing, and if it returns nothing i want to detect that and run another query to insert somewhere else.

    is this not allowed, to process the results if the query returns nothing? If I can't do it this way, whats the best way to do it?

    aConnection is a OleDbConnection, I'm pretty sure its fine, I'm already using it in other functions to run other queries.

    hope this makes sense...

  7. #7
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    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.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    Quote 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();
    			}

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    85

    found te problem

    in case your wondering I found the problem after wasting a few hours. the first way I tried worked, but NULL must be in all lowercase in c#.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM