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. what's wrong, Object reference not set to an instance of an object.

what's wrong, Object reference not set to an instance of an object.

Scheduled Pinned Locked Moved C#
help
8 Posts 5 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
    suni_dotnet
    wrote on last edited by
    #1

    Hi, whats wrong in this code. iam getting this error at AddWithValue. what value i need to set for what. protected void GridView2_RowUpdating(Object sender, GridViewUpdateEventArgs e) { string connectionstring = WebConfigurationManager.ConnectionStrings["RoyalHotelManagement"].ConnectionString; SqlConnection con = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("InsertSuite", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@filleddate", GridView2.SelectedRow.Cells[3].Text); cmd.Parameters.AddWithValue("@freedate", GridView2.SelectedRow.Cells[4].Text); cmd.Parameters.AddWithValue("@nameofcustomer", GridView2.SelectedRow.Cells[7].Text); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); } thanks in advance

    J P R W 4 Replies Last reply
    0
    • S suni_dotnet

      Hi, whats wrong in this code. iam getting this error at AddWithValue. what value i need to set for what. protected void GridView2_RowUpdating(Object sender, GridViewUpdateEventArgs e) { string connectionstring = WebConfigurationManager.ConnectionStrings["RoyalHotelManagement"].ConnectionString; SqlConnection con = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("InsertSuite", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@filleddate", GridView2.SelectedRow.Cells[3].Text); cmd.Parameters.AddWithValue("@freedate", GridView2.SelectedRow.Cells[4].Text); cmd.Parameters.AddWithValue("@nameofcustomer", GridView2.SelectedRow.Cells[7].Text); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); } thanks in advance

      J Offline
      J Offline
      Jon Rista
      wrote on last edited by
      #2

      I am guessing that something in the GridView is null. It could be: 1. SelectedRow, if there is no selected row. 2. Any of the cells 3. The Text value of the cell (null string)

      S 1 Reply Last reply
      0
      • S suni_dotnet

        Hi, whats wrong in this code. iam getting this error at AddWithValue. what value i need to set for what. protected void GridView2_RowUpdating(Object sender, GridViewUpdateEventArgs e) { string connectionstring = WebConfigurationManager.ConnectionStrings["RoyalHotelManagement"].ConnectionString; SqlConnection con = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("InsertSuite", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@filleddate", GridView2.SelectedRow.Cells[3].Text); cmd.Parameters.AddWithValue("@freedate", GridView2.SelectedRow.Cells[4].Text); cmd.Parameters.AddWithValue("@nameofcustomer", GridView2.SelectedRow.Cells[7].Text); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); } thanks in advance

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        As has already been said, it could be a null SelectedRow, or the value in the Cells might be null. What you need to do is put a breakpoint on the line with the first AddWithValue (just click F9 to insert the breakpoint), and then run the application. When you hit this method, hover your mouse over the different properties until you see one that pops up the tooltip "null". There - now you know how to solve these issues for yourself.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        My blog | My articles | MoXAML PowerToys

        S 1 Reply Last reply
        0
        • J Jon Rista

          I am guessing that something in the GridView is null. It could be: 1. SelectedRow, if there is no selected row. 2. Any of the cells 3. The Text value of the cell (null string)

          S Offline
          S Offline
          suni_dotnet
          wrote on last edited by
          #4

          hi, yes database values are null(gridview is populated with database values), i need to input the values through gridview in edit mode.

          J 1 Reply Last reply
          0
          • P Pete OHanlon

            As has already been said, it could be a null SelectedRow, or the value in the Cells might be null. What you need to do is put a breakpoint on the line with the first AddWithValue (just click F9 to insert the breakpoint), and then run the application. When you hit this method, hover your mouse over the different properties until you see one that pops up the tooltip "null". There - now you know how to solve these issues for yourself.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            My blog | My articles | MoXAML PowerToys

            S Offline
            S Offline
            suni_dotnet
            wrote on last edited by
            #5

            hi, but i need to input values to the blank cells. i cannot populate them with some other values. they has to be null/blank.

            1 Reply Last reply
            0
            • S suni_dotnet

              Hi, whats wrong in this code. iam getting this error at AddWithValue. what value i need to set for what. protected void GridView2_RowUpdating(Object sender, GridViewUpdateEventArgs e) { string connectionstring = WebConfigurationManager.ConnectionStrings["RoyalHotelManagement"].ConnectionString; SqlConnection con = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("InsertSuite", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@filleddate", GridView2.SelectedRow.Cells[3].Text); cmd.Parameters.AddWithValue("@freedate", GridView2.SelectedRow.Cells[4].Text); cmd.Parameters.AddWithValue("@nameofcustomer", GridView2.SelectedRow.Cells[7].Text); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); } thanks in advance

              R Offline
              R Offline
              Rutvik Dave
              wrote on last edited by
              #6

              First of all try to explain what you want to do with the code. if you want to store the value (which is added directly in the gridview) in the database, then try to use GridView2.Rows[e.RowIndex].Cell[3].Text for all the cells, becuase it can be possible that no rows selected at that time. also assign the Values of the gridview to some variable and the check wether the value is null or not. i.e.

              string FilledDate = GridView2.Rows[e.RowIndex].Cell[3].Text;
              if (FilledDate != null)
              cmd.Parameters.AddWithValue("@filleddate", FilledDate);
              else
              cmd.Parameters.AddWithValue("@filleddate", ""); // or give appropriate error messege instead of inserting blank values.

              ----------------------- or you can use directly e.NewValues[3] Instead of GridView2.SelectedRow.Cells[3].Text in your original code. e.NewValue will give you all the new updated values. e.OldValue will give you all the old existing values. do this for all the fields. and one more thing give GridView2 some appropriate Name, instead of using auto generated name.

              1 Reply Last reply
              0
              • S suni_dotnet

                Hi, whats wrong in this code. iam getting this error at AddWithValue. what value i need to set for what. protected void GridView2_RowUpdating(Object sender, GridViewUpdateEventArgs e) { string connectionstring = WebConfigurationManager.ConnectionStrings["RoyalHotelManagement"].ConnectionString; SqlConnection con = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("InsertSuite", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@filleddate", GridView2.SelectedRow.Cells[3].Text); cmd.Parameters.AddWithValue("@freedate", GridView2.SelectedRow.Cells[4].Text); cmd.Parameters.AddWithValue("@nameofcustomer", GridView2.SelectedRow.Cells[7].Text); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); } thanks in advance

                W Offline
                W Offline
                Wendelius
                wrote on last edited by
                #7

                This is quite confusing. Your procedure name is InsertSuite, which sounds like you're inserting data into the database. However you're executing a reader so it seems like you're actually fetching data. This is also the impression from your replies. If you're trying to update the database I think you should use e.RowIndex to get the actual row, but if you're trying to fetch the data I think you should use different event since RowUpdating is raised when rows update button is clicked.

                The need to optimize rises from a bad design.My articles[^]

                1 Reply Last reply
                0
                • S suni_dotnet

                  hi, yes database values are null(gridview is populated with database values), i need to input the values through gridview in edit mode.

                  J Offline
                  J Offline
                  Jon Rista
                  wrote on last edited by
                  #8

                  I understand that the database values are null...I was talking about GridView values. Your NullReferenceException is most likely because the SelectedRow is null, or the Text of one or more Cells is null. Both will cause a NullReferenceException.

                  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