Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im having a exception throwing problem with my program which works with a access database through OleDB connection.
Here is my function which causes the error.

C++
public static string getNICByID(int ID)
        {
            //validation params
            if (ID <= 0)
                throw new Exception("Invalid ID");
            //if (!isRegNumExists(ID))
            //    throw new Exception("No records");


            const string query = @"select NICNumber from Student where StudentRegNo = @_regNum";

            try
            {
                using (OleDbConnection con = new OleDbConnection(conString))
                {
                    con.Open();

                    using (OleDbCommand cmd = new OleDbCommand(query, con))
                    {
                        cmd.Parameters.Add("@_regNum", OleDbType.Integer).Value = ID;

                        object obj = cmd.ExecuteScalar();
                        if (obj != null)
                            return obj.ToString();
                        else
                            throw new Exception("No records");
                    }
                }
            }
            catch { throw; }


This functions works fine and returns what I want if the data table contains a record with respect to the ID. But if the table doesnt contain a record it throws another exception like the picture below.

http://s7.postimg.org/w4j72ie8b/Untitled.png[^]

What's the wrong with my code ?

Thanks in advance.
Posted

That is because you, yourself, personally are throwing this exception in your code when there is no record found in the database.

C#
if (obj != null)
     return obj.ToString();
else
     throw new Exception("No records");


This line of code is intended to do what you're seeing. It will see, if there is any record, ortherwise throw the exception; with the parameter as the reason or Message.

To ignore the exception being raised, please remove this line,

C#
throw new Exception("No records");


Then it won't throw the exception, instead you can show a message pop up to the user with this value.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-Mar-15 10:21am    
Sure, a 5.
—SA
Afzaal Ahmad Zeeshan 6-Mar-15 10:31am    
Thanks a lot for your vote, Sergey sir. :-)
You're throwing the Excetion again in your catch() section.

try
{
    // your code
    throw New Exception ("no recors")
}
catch (throw)  
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
 
Share this answer
 
The other two answers are showing you what is going on. What I would do is

change your try catch block to

C#
try
{
   if (obj != null)
       return obj.ToString();
   else
       throw new Exception("No records");
}
catch(Exception ex)
{
  //put a break point on this line below
  String message = ex.message;
}


I would put a breakpoint on the string message line and then while debugging check the ex.message as it will show you that the error message should be "No Records"

I personally would also change your exception to something more appropriate such as SQLExcpetion rather than the catch all Exception
 
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