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. Object reference not set to an instance of an object.

Object reference not set to an instance of an object.

Scheduled Pinned Locked Moved C#
databasehelp
18 Posts 6 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.
  • B Offline
    B Offline
    balanjingot
    wrote on last edited by
    #1

    hi, what would be the probable cause why i received this kind of error message... this is my code: try { foreach(DataGridViewRow row in dataGridView4.Rows) { long itemcode = 0; string description = ""; int quantity = 0; string unit = ""; itemcode = Convert.ToInt64(row.Cells[0].Value); description = row.Cells[1].Value.ToString(); quantity = Convert.ToInt32(row.Cells[2].Value); unit = row.Cells[3].Value.ToString(); String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (itemcode,description,quantity,unit)"; MySqlCommand command = new MySqlCommand(query, connection); command.ExecuteNonQuery(); } } catch (MySqlException mse) { MessageBox.Show(mse.Message); } }

    jing

    L P R A 4 Replies Last reply
    0
    • B balanjingot

      hi, what would be the probable cause why i received this kind of error message... this is my code: try { foreach(DataGridViewRow row in dataGridView4.Rows) { long itemcode = 0; string description = ""; int quantity = 0; string unit = ""; itemcode = Convert.ToInt64(row.Cells[0].Value); description = row.Cells[1].Value.ToString(); quantity = Convert.ToInt32(row.Cells[2].Value); unit = row.Cells[3].Value.ToString(); String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (itemcode,description,quantity,unit)"; MySqlCommand command = new MySqlCommand(query, connection); command.ExecuteNonQuery(); } } catch (MySqlException mse) { MessageBox.Show(mse.Message); } }

      jing

      L Offline
      L Offline
      lost in transition
      wrote on last edited by
      #2

      Where is the debugger popping the error out. What line?


      God Bless, Jason

      DavidCrow wrote:

      It would not affect me or my family one iota. My wife and I are in charge of when the tv is on, and what it displays. I do not need any external input for that.

      B 1 Reply Last reply
      0
      • B balanjingot

        hi, what would be the probable cause why i received this kind of error message... this is my code: try { foreach(DataGridViewRow row in dataGridView4.Rows) { long itemcode = 0; string description = ""; int quantity = 0; string unit = ""; itemcode = Convert.ToInt64(row.Cells[0].Value); description = row.Cells[1].Value.ToString(); quantity = Convert.ToInt32(row.Cells[2].Value); unit = row.Cells[3].Value.ToString(); String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (itemcode,description,quantity,unit)"; MySqlCommand command = new MySqlCommand(query, connection); command.ExecuteNonQuery(); } } catch (MySqlException mse) { MessageBox.Show(mse.Message); } }

        jing

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Some value is null. Try debugging to see what line throws the exception. If you still can't figure it out, then post more information.

        B 2 Replies Last reply
        0
        • L lost in transition

          Where is the debugger popping the error out. What line?


          God Bless, Jason

          DavidCrow wrote:

          It would not affect me or my family one iota. My wife and I are in charge of when the tv is on, and what it displays. I do not need any external input for that.

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

          hi, thanks for the reply... the following lines: description = row.Cells[1].Value.ToString(); unit = row.Cells[3].Value.ToString(); these two lines of code are string, i wonder why it has an error. pls. help:) thanks,

          jing

          L 1 Reply Last reply
          0
          • P PIEBALDconsult

            Some value is null. Try debugging to see what line throws the exception. If you still can't figure it out, then post more information.

            B Offline
            B Offline
            balanjingot
            wrote on last edited by
            #5

            hi, thanks for the reply.. the following 2 lines received the error message.. description = row.Cells[1].Value.ToString(); unit = row.Cells[3].Value.ToString(); regard:-D

            jing

            L 1 Reply Last reply
            0
            • B balanjingot

              hi, thanks for the reply... the following lines: description = row.Cells[1].Value.ToString(); unit = row.Cells[3].Value.ToString(); these two lines of code are string, i wonder why it has an error. pls. help:) thanks,

              jing

              L Offline
              L Offline
              lost in transition
              wrote on last edited by
              #6

              Like PIEBALDconsult said below, You need to use the debugger to find out what line.


              God Bless, Jason

              DavidCrow wrote:

              It would not affect me or my family one iota. My wife and I are in charge of when the tv is on, and what it displays. I do not need any external input for that.

              B 1 Reply Last reply
              0
              • L lost in transition

                Like PIEBALDconsult said below, You need to use the debugger to find out what line.


                God Bless, Jason

                DavidCrow wrote:

                It would not affect me or my family one iota. My wife and I are in charge of when the tv is on, and what it displays. I do not need any external input for that.

                B Offline
                B Offline
                balanjingot
                wrote on last edited by
                #7

                hi, ok thanks a lot... God Bless:)

                jing

                1 Reply Last reply
                0
                • B balanjingot

                  hi, thanks for the reply.. the following 2 lines received the error message.. description = row.Cells[1].Value.ToString(); unit = row.Cells[3].Value.ToString(); regard:-D

                  jing

                  L Offline
                  L Offline
                  lmoelleb
                  wrote on last edited by
                  #8

                  Then either row, row.Cells, row.Cells[1], or row.Cells[1].Value is null. The debugger will show the value of each of them so it should be easy to find.

                  B 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Some value is null. Try debugging to see what line throws the exception. If you still can't figure it out, then post more information.

                    B Offline
                    B Offline
                    balanjingot
                    wrote on last edited by
                    #9

                    hi, by the way will you please help me on how to insert values from the datagridview to the database table? waiting for your reply:)

                    jing

                    P 2 Replies Last reply
                    0
                    • L lmoelleb

                      Then either row, row.Cells, row.Cells[1], or row.Cells[1].Value is null. The debugger will show the value of each of them so it should be easy to find.

                      B Offline
                      B Offline
                      balanjingot
                      wrote on last edited by
                      #10

                      hi, thanks.. yap,through the debugger,i've found out that row.Cell[1].Value is null.i'm trying to fix it right now... :)

                      jing

                      1 Reply Last reply
                      0
                      • B balanjingot

                        hi, by the way will you please help me on how to insert values from the datagridview to the database table? waiting for your reply:)

                        jing

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        In your String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (itemcode,description,quantity,unit)"; You'll want to use parameters for itemcode,description,quantity,unit I'm not as familiar with MySql, but in Sql Server I'd prefix each with an @ @itemcode,@description,@quantity,@unit Then instantiate a MySqlParameter for each and add them to the command's parameter collection. Roughly: command.Parameters.Add ( new MySqlParameter ( "@itemcode" , itemcode ) ; But if you're going to be inserting a bunch of data in a loop, you'll want something like: command.Parameters.Add ( new MySqlParameter ( "@itemcode" , typeof(itemcode) ) ; then in the loop: command.Parameters [ "@itemcode" ].Value = itemcode ; (will that work?)

                        1 Reply Last reply
                        0
                        • B balanjingot

                          hi, by the way will you please help me on how to insert values from the datagridview to the database table? waiting for your reply:)

                          jing

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #12

                          This is untested, and I don't usually use DataGridViews or MySql:

                          try
                          {
                          connection.Open() ;

                          MySqlCommand command = new MySqlCommand
                          (
                              "INSERT INTO test(itemcode,description,quantity,unit) VALUES (@itemcode,@description,@quantity,@unit)"
                          , 
                              connection
                          ) ;
                          
                          foreach ( DataGridViewColumn col in dataGridView4.Columns )
                          {
                              command.Parameters.Add ( new MySqlParameter ( "@" + col.DataPropertyName , col.ValueType ) ;
                          }
                          
                          command.Transaction = connection.BeginTransaction() ;
                          
                          foreach(DataGridViewRow row in dataGridView4.Rows)
                          {
                              foreach ( DataGridViewCell cel in row.Cells )
                              {
                                  command.Parameters \[ "@" + cel.DataPropertyName \].Value = cel.Value ;
                              }
                          
                              command.ExecuteNonQuery() ;
                          }
                          
                          command.Transaction.Commit ;
                          

                          }
                          catch ( MySqlException mse )
                          {
                          command.Transaction.Rollback ;
                          MessageBox.Show ( mse.Message ) ;
                          }
                          finally
                          {
                          connection.Close() ;
                          }

                          P 1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            This is untested, and I don't usually use DataGridViews or MySql:

                            try
                            {
                            connection.Open() ;

                            MySqlCommand command = new MySqlCommand
                            (
                                "INSERT INTO test(itemcode,description,quantity,unit) VALUES (@itemcode,@description,@quantity,@unit)"
                            , 
                                connection
                            ) ;
                            
                            foreach ( DataGridViewColumn col in dataGridView4.Columns )
                            {
                                command.Parameters.Add ( new MySqlParameter ( "@" + col.DataPropertyName , col.ValueType ) ;
                            }
                            
                            command.Transaction = connection.BeginTransaction() ;
                            
                            foreach(DataGridViewRow row in dataGridView4.Rows)
                            {
                                foreach ( DataGridViewCell cel in row.Cells )
                                {
                                    command.Parameters \[ "@" + cel.DataPropertyName \].Value = cel.Value ;
                                }
                            
                                command.ExecuteNonQuery() ;
                            }
                            
                            command.Transaction.Commit ;
                            

                            }
                            catch ( MySqlException mse )
                            {
                            command.Transaction.Rollback ;
                            MessageBox.Show ( mse.Message ) ;
                            }
                            finally
                            {
                            connection.Close() ;
                            }

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #13

                            P.S. Using the technique above you can write a library routine that will even create the SQL statement, and you could simply pass it a reference to any DataGridView and the name of the table into which to sert the data, and never (hardly ever) write an INSERT statement again: InsertData ( "test" , dataGridView4 ) ;

                            B 1 Reply Last reply
                            0
                            • B balanjingot

                              hi, what would be the probable cause why i received this kind of error message... this is my code: try { foreach(DataGridViewRow row in dataGridView4.Rows) { long itemcode = 0; string description = ""; int quantity = 0; string unit = ""; itemcode = Convert.ToInt64(row.Cells[0].Value); description = row.Cells[1].Value.ToString(); quantity = Convert.ToInt32(row.Cells[2].Value); unit = row.Cells[3].Value.ToString(); String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (itemcode,description,quantity,unit)"; MySqlCommand command = new MySqlCommand(query, connection); command.ExecuteNonQuery(); } } catch (MySqlException mse) { MessageBox.Show(mse.Message); } }

                              jing

                              R Offline
                              R Offline
                              Rob Graham
                              wrote on last edited by
                              #14

                              balanjingot wrote:

                              String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (itemcode,description,quantity,unit)";

                              Won't work. C# isn't going to magically convert the variable names in th VALUES clause to the appropriate things. Best approach would be to make the list in the VALUES clause use parameters. "INSERT INTO test(itemcode,description,quantity,unit) VALUES ( @itemcode,@description,@quantity,@unit)" //What is a MySqlCommand? System.Data.SqlCommand is a sealed class, so you can't derive from it... MySqlCommand command = new MySqlCommand(query, connection); command.Parameters.Add( new SqlParameter("@itemcode",itemcode); ... the rest, in order... command.ExecuteNonQuery(); I would suppose that one other likely cause is that your MySqlCommand Class is flawed, and the constructor silently fails...

                              B 1 Reply Last reply
                              0
                              • P PIEBALDconsult

                                P.S. Using the technique above you can write a library routine that will even create the SQL statement, and you could simply pass it a reference to any DataGridView and the name of the table into which to sert the data, and never (hardly ever) write an INSERT statement again: InsertData ( "test" , dataGridView4 ) ;

                                B Offline
                                B Offline
                                balanjingot
                                wrote on last edited by
                                #15

                                hi, ok, it try to get the logic of your program so I could migrate it to MySql... regards:)

                                jing

                                1 Reply Last reply
                                0
                                • R Rob Graham

                                  balanjingot wrote:

                                  String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (itemcode,description,quantity,unit)";

                                  Won't work. C# isn't going to magically convert the variable names in th VALUES clause to the appropriate things. Best approach would be to make the list in the VALUES clause use parameters. "INSERT INTO test(itemcode,description,quantity,unit) VALUES ( @itemcode,@description,@quantity,@unit)" //What is a MySqlCommand? System.Data.SqlCommand is a sealed class, so you can't derive from it... MySqlCommand command = new MySqlCommand(query, connection); command.Parameters.Add( new SqlParameter("@itemcode",itemcode); ... the rest, in order... command.ExecuteNonQuery(); I would suppose that one other likely cause is that your MySqlCommand Class is flawed, and the constructor silently fails...

                                  B Offline
                                  B Offline
                                  balanjingot
                                  wrote on last edited by
                                  #16

                                  hi everybody, thanks for your reply and suggestions, finally i got the solution to my problem... my revised code: private void button13_Click(object sender, EventArgs e) { try { for (int i = 0; i < dataGridView4.Rows.Count -1; i++) { DataGridViewRow row = dataGridView4.Rows[i]; long itemcode = 0; string description = ""; int quantity = 0; string unit = ""; itemcode = Convert.ToInt64(row.Cells[0].Value); if (description != null) { description = row.Cells[1].Value.ToString() + ""; } quantity = Convert.ToInt32(row.Cells[2].Value); unit = row.Cells[3].Value.ToString(); String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (" + " " + itemcode + " ,'" + description + "'" + " , " + quantity + " ,'" + unit + "'" + " )"; MySqlCommand command = new MySqlCommand(query, connection); MessageBox.Show(command.ExecuteNonQuery().ToString()); } } thanks guys regards:)

                                  jing

                                  1 Reply Last reply
                                  0
                                  • B balanjingot

                                    hi, what would be the probable cause why i received this kind of error message... this is my code: try { foreach(DataGridViewRow row in dataGridView4.Rows) { long itemcode = 0; string description = ""; int quantity = 0; string unit = ""; itemcode = Convert.ToInt64(row.Cells[0].Value); description = row.Cells[1].Value.ToString(); quantity = Convert.ToInt32(row.Cells[2].Value); unit = row.Cells[3].Value.ToString(); String query = "INSERT INTO test(itemcode,description,quantity,unit) VALUES (itemcode,description,quantity,unit)"; MySqlCommand command = new MySqlCommand(query, connection); command.ExecuteNonQuery(); } } catch (MySqlException mse) { MessageBox.Show(mse.Message); } }

                                    jing

                                    A Offline
                                    A Offline
                                    AFSEKI
                                    wrote on last edited by
                                    #17
                                    1. Check if row.Cells[X].Value is null or DBnull before converting or ToString()ing them 2) Catch also (Exception ex) and System.Diagnostics.Debug.WriteLine("Error! Source: ", ex.Source + ", Message: " + ex.Message); to see what happened. You can also do this: if(ex.InnerException != null) System.Diagnostics.Debug.WriteLine(ex.InnerException.Source + ", " + ex.innerException.Message); Hope this helps...
                                    B 1 Reply Last reply
                                    0
                                    • A AFSEKI
                                      1. Check if row.Cells[X].Value is null or DBnull before converting or ToString()ing them 2) Catch also (Exception ex) and System.Diagnostics.Debug.WriteLine("Error! Source: ", ex.Source + ", Message: " + ex.Message); to see what happened. You can also do this: if(ex.InnerException != null) System.Diagnostics.Debug.WriteLine(ex.InnerException.Source + ", " + ex.innerException.Message); Hope this helps...
                                      B Offline
                                      B Offline
                                      balanjingot
                                      wrote on last edited by
                                      #18

                                      hi, thanks, my program already worked out!! regards:)

                                      jing

                                      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