none
Error: Object reference not set to an instance of an object RRS feed

  • Question

  • The goal is: check existence of username. I have been dealing with the issue for multiple days. Spend more than 30 minutes each day, still have not figured out the code which works.

    It is difficult for me (non IT professional, not familiar with syntax), keep modifying the code, now the new error "Object reference not set to an instance of an object" comes up for below code.

    Thanks.

    GlobalVariables.Username = lblUsername.Text;
    //There is code: lblUsername.Text = Environment.UserName;
    GlobalVariables.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=" + @"C:\Users\JC\Desktop\Test.accdb;";
    
    
    bool SuccessfulConnection;
    
                
    OdbcConnection myConn = new OdbcConnection(GlobalVariables.ConnectionString);
    OdbcCommand cmd = new OdbcCommand { Connection = myConn };
    cmd.CommandText = "SELECT UserName FROM  User_access WHERE UserName = '" + GlobalVariables.Username + "'";
    
    myConn.Open();
    var result = cmd.ExecuteScalar(); //Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
    if (result!= null)
    {
        SuccessfulConnection = true;
    }
    else
    {
        SuccessfulConnection = false;
    }


    • Edited by VA_er Monday, November 25, 2019 4:39 AM
    Monday, November 25, 2019 4:38 AM

All replies

  • At which particular line you are facing this error. Can you please provide stack trace of exception ?

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, November 25, 2019 4:50 AM
  • You should open you connection first and check if it is open..

    myConn.Open()

    if (myConn.Connected) -->enjoy your data

    should be myCon.State == Open..

    If closed - then you have bad connectionString or ODBC settings.

    I'll give you a good suggestion: just drop MAccess into garbage bin and setup MS SQL express + MS SQL Management Studio.. that will accelerate your desires fulfillment..


    "I = I + 1" .. Isn't it boolshit?


    • Edited by RobbKirk Monday, November 25, 2019 5:00 AM
    Monday, November 25, 2019 4:51 AM
  • At which particular line you are facing this error. Can you please provide stack trace of exception ?

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    I am not a IT professional, actually not quite familiar with Visual Studio, not sure how to know which line causes the problem. It can pass when I build the solution (no error), the error comes up when running the executive file.

    But one thing is: I built the app on my personal computer (Visual Studio Community Edition, window 10, 64 bits), then move to my work computer (no Visual Studio installed, still window 7, 32 bits). Does it cause the issue?

    If the app runs on my personal computer, there is different error too: [ODBC Driver Manager] Data Source name not found and no default driver specific.


    • Edited by VA_er Monday, November 25, 2019 5:00 AM
    Monday, November 25, 2019 4:58 AM
  • Hi VA_er, 

    Thank you for posting here.

    According to your question, I make a test on my side, and you can use ‘OdbcConnection.State’ property to check whether application connect to the database.

    Try the following code:

            public bool judge(string userName)
            {
                bool SuccessfulConnection = false;
                string connString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + "Dbq=D:\\Db1.accdb;";
                using (OdbcConnection myConn = new OdbcConnection(connString))
                {
                    myConn.Open();
                    // use State property to check whether connect to the database.
                    if (myConn.State == ConnectionState.Open)
                    {
                        // if successfully connect to the database, show the message.
                        MessageBox.Show("successfully connect to database");
                    }
                    else
                    {
                        MessageBox.Show("cannot connect to database, please check your connection string");
                    }
                    string cmdText = "SELECT ...";
                    using (OdbcCommand cmd = new OdbcCommand(cmdText, myConn))
                    {
                        var reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            var result = reader[0].ToString();
                            if (result == userName)
                            {
                                SuccessfulConnection = true;
                            }
                        }
                    }
                }
                return SuccessfulConnection;
            }

    Result:

    Besides, if you want to run the application in different computer, you should make sure the file path in other computer is correct, and query can work well.

    Last but not least, you may not build the app on 64bits computer and run it on 32bits computer.

    You can refer to the following reference.

    [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, November 26, 2019 8:26 AM
    Moderator