Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Error in OleDBConnection : Object reference not set to an instance of an object

Error in OleDBConnection : Object reference not set to an instance of an object

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sksandz
    wrote on last edited by
    #1

    I am running the following code. It executes when the value of variable i is 1 and when the loop continues and value of i becomes 2 i am getting error "Object reference not set to an instance of an object" in the cmdSearch.CommandText line in the code. . There is no problem with connection string. It successfully retrieves when i = 1. Can anybody help me out what is the problem. Please post with corrected code if anybody can help. =========================================== for (int i=1; i <= objFolder.Items.Count; i++) { System.Data.OleDb.OleDbConnection odbSearch = new System.Data.OleDb.OleDbConnection(); System.Data.OleDb.OleDbCommand cmdSearch = new System.Data.OleDb.OleDbCommand(); odbSearch.ConnectionString = strConn; cmdSearch.Connection = odbSearch; item = (Outlook.ContactItem) objFolder.Items.Item(i); string em = item.Email1Address; cmdSearch.CommandText = "select * from Outlook_Contacts where FirstName='"+fn.ToString().Trim()+"' and LastName='"+ln.ToString().Trim()+"'"; odbSearch.Open(); OleDbDataReader rdrSearch = cmdSearch.ExecuteReader(); while( rdrSearch.Read()) { RecordFlag = true; } odbSearch.Close(); } ===========================================

    B M 2 Replies Last reply
    0
    • S sksandz

      I am running the following code. It executes when the value of variable i is 1 and when the loop continues and value of i becomes 2 i am getting error "Object reference not set to an instance of an object" in the cmdSearch.CommandText line in the code. . There is no problem with connection string. It successfully retrieves when i = 1. Can anybody help me out what is the problem. Please post with corrected code if anybody can help. =========================================== for (int i=1; i <= objFolder.Items.Count; i++) { System.Data.OleDb.OleDbConnection odbSearch = new System.Data.OleDb.OleDbConnection(); System.Data.OleDb.OleDbCommand cmdSearch = new System.Data.OleDb.OleDbCommand(); odbSearch.ConnectionString = strConn; cmdSearch.Connection = odbSearch; item = (Outlook.ContactItem) objFolder.Items.Item(i); string em = item.Email1Address; cmdSearch.CommandText = "select * from Outlook_Contacts where FirstName='"+fn.ToString().Trim()+"' and LastName='"+ln.ToString().Trim()+"'"; odbSearch.Open(); OleDbDataReader rdrSearch = cmdSearch.ExecuteReader(); while( rdrSearch.Read()) { RecordFlag = true; } odbSearch.Close(); } ===========================================

      B Offline
      B Offline
      big_trev
      wrote on last edited by
      #2

      What are fn and ln? Where are they defined?

      S 1 Reply Last reply
      0
      • B big_trev

        What are fn and ln? Where are they defined?

        S Offline
        S Offline
        sksandz
        wrote on last edited by
        #3

        fn and ln are declared outside and the values are properly getting passed to those variables. The code executes when i=1 and when the value of i becomes 2 the error occurs. There is no problem with connection string also. only when the code executes to the commandtext line it shows the error. that too i am getting one record successfully(when i = 1). hope this helps

        B 1 Reply Last reply
        0
        • S sksandz

          fn and ln are declared outside and the values are properly getting passed to those variables. The code executes when i=1 and when the value of i becomes 2 the error occurs. There is no problem with connection string also. only when the code executes to the commandtext line it shows the error. that too i am getting one record successfully(when i = 1). hope this helps

          B Offline
          B Offline
          big_trev
          wrote on last edited by
          #4

          well, some thing is null. It is either item, cmdSearch, fn or ln. can you step through it in VS and check?

          A 1 Reply Last reply
          0
          • S sksandz

            I am running the following code. It executes when the value of variable i is 1 and when the loop continues and value of i becomes 2 i am getting error "Object reference not set to an instance of an object" in the cmdSearch.CommandText line in the code. . There is no problem with connection string. It successfully retrieves when i = 1. Can anybody help me out what is the problem. Please post with corrected code if anybody can help. =========================================== for (int i=1; i <= objFolder.Items.Count; i++) { System.Data.OleDb.OleDbConnection odbSearch = new System.Data.OleDb.OleDbConnection(); System.Data.OleDb.OleDbCommand cmdSearch = new System.Data.OleDb.OleDbCommand(); odbSearch.ConnectionString = strConn; cmdSearch.Connection = odbSearch; item = (Outlook.ContactItem) objFolder.Items.Item(i); string em = item.Email1Address; cmdSearch.CommandText = "select * from Outlook_Contacts where FirstName='"+fn.ToString().Trim()+"' and LastName='"+ln.ToString().Trim()+"'"; odbSearch.Open(); OleDbDataReader rdrSearch = cmdSearch.ExecuteReader(); while( rdrSearch.Read()) { RecordFlag = true; } odbSearch.Close(); } ===========================================

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            Like bigtrev said, just step through and check fn and ln. Or perhaps you got the line number wrong and item is null. Anyway, just step through and check. Btw. you know that building SQL commands the way you do is highly vulnerable to SQL injection? You should use parameterized queries instead. Regards, mav

            1 Reply Last reply
            0
            • B big_trev

              well, some thing is null. It is either item, cmdSearch, fn or ln. can you step through it in VS and check?

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              I have done that. I am getting one record ie. when the value of i = 1. When the value of i increases to 2 then in the commandtext line this error occurs. And also fn and ln are fine. Can you please tell me how to solve it?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups