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
  1. Home
  2. General Programming
  3. C#
  4. C# SQL Exception Error

C# SQL Exception Error

Scheduled Pinned Locked Moved C#
helpdatabasecsharpdesignworkspace
4 Posts 2 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.
  • H Offline
    H Offline
    hobbsieoz
    wrote on last edited by
    #1

    Hi Im having a problem with SQL and C#. Here is a copy of my code where the problem is occurring. private void button1_Click(object sender, EventArgs e) { oleDbConnection1.Open(); oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM NASDATA where account = 'test'"; oleDbDataAdapter1.UpdateCommand.ExecuteNonQuery(); oleDbConnection1.Close(); } I get an exception error on the ExecuteNonQuery line. NullReferenceException was unhandled. Object refernece not set to an instance of an object. On the design page i have the Oledb connector and dataadapter setup, and the test connection succeeds. Note: I had this same problem using an Access database to. I really hope somebody can help. Many thanks!

    J H 2 Replies Last reply
    0
    • H hobbsieoz

      Hi Im having a problem with SQL and C#. Here is a copy of my code where the problem is occurring. private void button1_Click(object sender, EventArgs e) { oleDbConnection1.Open(); oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM NASDATA where account = 'test'"; oleDbDataAdapter1.UpdateCommand.ExecuteNonQuery(); oleDbConnection1.Close(); } I get an exception error on the ExecuteNonQuery line. NullReferenceException was unhandled. Object refernece not set to an instance of an object. On the design page i have the Oledb connector and dataadapter setup, and the test connection succeeds. Note: I had this same problem using an Access database to. I really hope somebody can help. Many thanks!

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      Put a breakpoint on the ExecuteNonQuery line. When the program gets there is oleDbDataAdapter1.UpdateCommand null?

      1 Reply Last reply
      0
      • H hobbsieoz

        Hi Im having a problem with SQL and C#. Here is a copy of my code where the problem is occurring. private void button1_Click(object sender, EventArgs e) { oleDbConnection1.Open(); oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM NASDATA where account = 'test'"; oleDbDataAdapter1.UpdateCommand.ExecuteNonQuery(); oleDbConnection1.Close(); } I get an exception error on the ExecuteNonQuery line. NullReferenceException was unhandled. Object refernece not set to an instance of an object. On the design page i have the Oledb connector and dataadapter setup, and the test connection succeeds. Note: I had this same problem using an Access database to. I really hope somebody can help. Many thanks!

        H Offline
        H Offline
        hobbsieoz
        wrote on last edited by
        #3

        I apologise, i realise i put the update command instead of select. However, i get the same problem (nullreferenceexception) if i change the code to the following: oleDbConnection1.Open(); oleDbDataAdapter1.UpdateCommand.CommandText = "UPDATE NASDATA SET EMAIL = 'TESTEMAIL' WHERE ACCOUNT = 'TEST'"; oleDbDataAdapter1.UpdateCommand.ExecuteNonQuery(); oleDbConnection1.Close(); It breaks on the UpdateCommand.CommandText line, saying NullReferenceException was unhandled. Object refernece not set to an instance of an object.

        J 1 Reply Last reply
        0
        • H hobbsieoz

          I apologise, i realise i put the update command instead of select. However, i get the same problem (nullreferenceexception) if i change the code to the following: oleDbConnection1.Open(); oleDbDataAdapter1.UpdateCommand.CommandText = "UPDATE NASDATA SET EMAIL = 'TESTEMAIL' WHERE ACCOUNT = 'TEST'"; oleDbDataAdapter1.UpdateCommand.ExecuteNonQuery(); oleDbConnection1.Close(); It breaks on the UpdateCommand.CommandText line, saying NullReferenceException was unhandled. Object refernece not set to an instance of an object.

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #4

          it's the same problem. oleDbDataAdapter1.UpdateCommand is not set to a valid object (meaning that it's null) but you're trying to access its members. Add this:

          oleDbDataAdapter1.UpdateCommand = new OleDbCommand();

          before you try to use UpdateCommand.

          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