Click here to Skip to main content
15,921,694 members
Home / Discussions / C#
   

C#

 
QuestionObject reference not set to an instance of an object Pin
Ice_Freez0520-Apr-10 21:22
Ice_Freez0520-Apr-10 21:22 
AnswerRe: Object reference not set to an instance of an object Pin
OriginalGriff20-Apr-10 21:47
mveOriginalGriff20-Apr-10 21:47 
GeneralRe: Object reference not set to an instance of an object Pin
Ice_Freez0520-Apr-10 22:10
Ice_Freez0520-Apr-10 22:10 
GeneralRe: Object reference not set to an instance of an object Pin
OriginalGriff20-Apr-10 22:22
mveOriginalGriff20-Apr-10 22:22 
GeneralRe: Object reference not set to an instance of an object [modified] Pin
Ice_Freez0520-Apr-10 22:48
Ice_Freez0520-Apr-10 22:48 
GeneralRe: Object reference not set to an instance of an object Pin
OriginalGriff20-Apr-10 22:59
mveOriginalGriff20-Apr-10 22:59 
GeneralRe: Object reference not set to an instance of an object Pin
Ice_Freez0520-Apr-10 23:04
Ice_Freez0520-Apr-10 23:04 
GeneralRe: Object reference not set to an instance of an object Pin
OriginalGriff20-Apr-10 23:33
mveOriginalGriff20-Apr-10 23:33 
Before we get to the problem, a couple of things!

Firstly, when you post a code fragment, use the "code block" wdiget - it preserves teh formatting and makes it easier to read, like this:
public int getRequestedFunctionID(string strFunctCode)
    {
    SQLiteCommand SQLComm;
    SQLiteDataReader sqlDR = null;

    int intFuncNameID = 0;

    string strSQL = "SELECT FuncNameID " +
    "FROM tblFuncName WHERE FunctCode = '" + strFunctCode + "'";

    try
        {
        cnn = new SQLiteConnection(strConn);
        cnn.Open();

        SQLComm = new SQLiteCommand(strSQL, cnn);
        sqlDR = SQLComm.ExecuteReader();


        sqlDR.Read();
        if (sqlDR.HasRows)
            {
            intFuncNameID = Convert.ToInt32(sqlDR["FuncNameID"]);
            }

        }
    catch
        {
        intFuncNameID = 100;

        }
    finally
        {
        sqlDR.Dispose();
        cnn.Close();
        }
    return intFuncNameID;
    }

Secondly, don't do DB access like that - always use parameterised queries. They help to make the code more readable, and remove the chances for a SQL Injection Attack.
cnn = new SQLiteConnection(strConn);
cnn.Open();
string strSQL = "SELECT FuncNameID FROM tblFuncName WHERE FunctCode = @FC";
SQLComm = new SQLiteCommand(strSQL, cnn);
SQLComm.Parameters.AddWithValue("@FC", strFunctCode);
sqlDR = SQLComm.ExecuteReader();

Thirdly, don't use anonymous exceptions unless you document well why it shoudl be anonymous! Why not? Because when an exception occurs, it is really handy to know why it happened, so that a problem can be averted... At the least, explain why you can ignore the error, if any.

OK, your problem:
There are three ways that the method can return zero:
1) If strFunctCode does not exist in the DB. The reader will then return with no rows, and the value given when intFuncNameID was declared will be returned.
2) If strFunctCode exists in the DB, but has an FuncNameID of zero.
3) If strFunctCode is not what you think it is...

Add some logging: If necessary put
if (intFuncNameID == 0)
    {
    LogToSomewhereTheDBIfNecessary(strFunctCode, sqlDR["FuncNameID"], "It returns zero!");
    }
Immediately before the return statement. (Obviously, that code won't work since sqlDR is out of scope, but you get the general idea...)
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace

C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

GeneralRe: Object reference not set to an instance of an object Pin
Ice_Freez0520-Apr-10 23:47
Ice_Freez0520-Apr-10 23:47 
Questionfreeing a variable Pin
AnirbanChak 20-Apr-10 20:47
AnirbanChak 20-Apr-10 20:47 
AnswerRe: freeing a variable Pin
Stanciu Vlad20-Apr-10 21:04
Stanciu Vlad20-Apr-10 21:04 
GeneralRe: freeing a variable Pin
AnirbanChak 20-Apr-10 23:06
AnirbanChak 20-Apr-10 23:06 
GeneralRe: freeing a variable Pin
Stanciu Vlad20-Apr-10 23:11
Stanciu Vlad20-Apr-10 23:11 
AnswerRe: freeing a variable Pin
Ice_Freez0520-Apr-10 21:06
Ice_Freez0520-Apr-10 21:06 
AnswerRe: freeing a variable Pin
OriginalGriff20-Apr-10 21:08
mveOriginalGriff20-Apr-10 21:08 
QuestionSend multiple SMS problem using Skype API Pin
Tridip Bhattacharjee20-Apr-10 20:13
professionalTridip Bhattacharjee20-Apr-10 20:13 
Questionauto update Pin
Member 59031020-Apr-10 19:53
Member 59031020-Apr-10 19:53 
AnswerRe: auto update Pin
Calla20-Apr-10 20:18
Calla20-Apr-10 20:18 
QuestionHow get USP device serial no Pin
anishkannan20-Apr-10 19:50
anishkannan20-Apr-10 19:50 
AnswerRe: How get USP device serial no Pin
sanforjackass20-Apr-10 21:52
sanforjackass20-Apr-10 21:52 
AnswerRe: How get USP device serial no Pin
Kythen21-Apr-10 6:18
Kythen21-Apr-10 6:18 
QuestionWaitOne won't wakeup Pin
doubleclick2d20-Apr-10 14:58
doubleclick2d20-Apr-10 14:58 
AnswerRe: WaitOne won't wakeup Pin
Luc Pattyn20-Apr-10 15:41
sitebuilderLuc Pattyn20-Apr-10 15:41 
Questionunresolved hashtable problem [modified] Pin
Jassim Rahma20-Apr-10 12:55
Jassim Rahma20-Apr-10 12:55 
AnswerRe: unresolved hashtable problem Pin
Luc Pattyn20-Apr-10 14:10
sitebuilderLuc Pattyn20-Apr-10 14:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.