what's wrong, Object reference not set to an instance of an object.
-
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
-
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
-
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
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
-
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)
hi, yes database values are null(gridview is populated with database values), i need to input the values through gridview in edit mode.
-
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
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.
-
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
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 ofGridView2.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 giveGridView2
some appropriate Name, instead of using auto generated name. -
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
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[^]
-
hi, yes database values are null(gridview is populated with database values), i need to input the values through gridview in edit mode.