Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public static int getLatestCustomerID()
        {
            int latestCustomerID = 0;

            //query to get the latest customer ID
            const string getLatestCustomerIDQuery = @"select max(CustomerID) from Customers as 'latest'";

            try
            {
                //connection to get the latest customer ID
                using (SQLiteConnection getLatestCustomerIDConnection = new SQLiteConnection(conString))
                {
                    getLatestCustomerIDConnection.Open();

                    //command to get the latest customer ID
                    using (SQLiteCommand getLatestCustomerIDCommand = getLatestCustomerIDConnection.CreateCommand())
                    {
                        object obj_latestCustomerID = getLatestCustomerIDCommand.ExecuteScalar();

                        string str_latestCustomerID = obj_latestCustomerID.ToString();
                        if (str_latestCustomerID.Length == 0)
                            latestCustomerID = 0;
                        else
                            latestCustomerID = int.Parse(str_latestCustomerID);
                    }
                }
            }
            catch (Exception ex) { throw ex; }

            return latestCustomerID;
        }


Here's my code and it stops and shows this error...
http://tinypic.com/r/28t821y/8[^]

what's the meaning of this ?
Im haven't even created a null exception..
It's that exception what throws sqlite library Im throwing again..
How come this happens ?
What's the wrong with my code ?

Thanks in advance..
Posted
Comments
[no name] 18-Oct-14 9:58am    
What do you mean? The error message is pretty obvious is it not? You tried to call a method using a object that is null and the method you tried to call does not accept null arguments.
George Jonsson 18-Oct-14 10:00am    
Use your debugger and check the value of 'conString' or 'str_latestCustomerID'
M­­ar­­­­k 18-Oct-14 10:04am    
conString is okay and latestCustomerID is 0..

btw what do you mean by check those variables with debugger ? Im using visual studio.. I use watch. And it says "The name 'str_latestCustomerID' does not exist in the current context"
M­­ar­­­­k 18-Oct-14 10:00am    
So shouldn't it throws the exception again instead of showing the error in the class ?
[no name] 18-Oct-14 10:17am    
Hi Mark, you really should read the documentation before writing or using copied code from the web. Where are you defining your reader? You cannot execute something you have not defined.

Put a break point on your code and debug. Then press F11 when the breakpoint is reached, hover your mouse over each object/method until you find one which is null and keep pressing F11 until the error exception is reached.

I would strongly advise you to start again and read the link I provided you.

"So shouldn't it throws the exception again instead of showing the error in the class ?"

You have thrown the exception in the catch handler - but there is no handler further up the "tree" to handle it - so the system stops your application and lets you know there is a problem. In production, it would just crash.

So it's up to you to work out exactly what is causing the error in the first place. Place a breakpoint at the start of the method, and run you app again. When you hit the breakpoint, step through looking obvious problems. If you get to teh catch, then look very closely at the line you were previously on!

But my guess is that you haven't created an actual SQL command: You call CreateCommand on the connection, but you don't do anything else with it before you call ExecuteScalar. You should probably consider setting the CommandText property...
 
Share this answer
 
Hi..!

As I think

there should be problem
C#
object obj_latestCustomerID = getLatestCustomerIDCommand.ExecuteScalar();

string str_latestCustomerID = obj_latestCustomerID.ToString();


Due to ToString() Method it not handled null value. So before using it check the object is null or not.


C#
object obj_latestCustomerID = getLatestCustomerIDCommand.ExecuteScalar();

string str_latestCustomerID = ""; // Any default value when null value occure
if(obj_latestCustomerID != null)
{
  str_latestCustomerID = obj_latestCustomerID.ToString()
}




:-)
 
Share this answer
 
v3
Comments
M­­ar­­­­k 18-Oct-14 10:23am    
Actually I hasn't assigned the command text..
Hemant Singh Rautela 18-Oct-14 10:28am    
ok..
But I suggust you check dbnull value before assigning it, because if you have not any value in that table it return dbnull value....
Hello,

Going through your code posted I don't see the SQL Query ( getLatestCustomerIDQuery) passed to the command object. Please add code to pass the SQL Query to the command object ;

eg : getLatestCustomerIDCommand.CommandText = getLatestCustomerIDQuery;


C#
public static int getLatestCustomerID()
        {
            int latestCustomerID = 0;

            //query to get the latest customer ID
            const string getLatestCustomerIDQuery = @"select max(CustomerID) from Customers as 'latest'";

            try
            {
                //connection to get the latest customer ID
                using (SQLiteConnection getLatestCustomerIDConnection = new SQLiteConnection(conString))
                {
                    getLatestCustomerIDConnection.Open();

                    //command to get the latest customer ID
                    using (SQLiteCommand getLatestCustomerIDCommand = getLatestCustomerIDConnection.CreateCommand())
                    {
                        //pass the SQL Query as command text
                        getLatestCustomerIDCommand.CommandText = getLatestCustomerIDQuery;
                        object obj_latestCustomerID = getLatestCustomerIDCommand.ExecuteScalar();

                        string str_latestCustomerID = obj_latestCustomerID.ToString();
                        if (str_latestCustomerID.Length == 0)
                            latestCustomerID = 0;
                        else
                            latestCustomerID = int.Parse(str_latestCustomerID);
                    }
                }
            }
            catch (Exception ex) { throw ex; }

            return latestCustomerID;
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900