Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My code as follows

private void SaveCheckedValues()
{
System.Collections.ArrayList userdetails = new
System.Collections.ArrayList();
int index = -1;
foreach (GridViewRow gvrow in grdRpt.Rows)
{
index = Convert.ToInt32(grdRpt.DataKeys[gvrow.RowIndex].Value);
 bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

               
                if (Session["CHECKED_ITEMS"] != null)
                    userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
                if (result)
                {
                    if (!userdetails.Contains(index))
                        userdetails.Add(index);
                }
                else
                    userdetails.Remove(index);
            }
            if (userdetails != null && userdetails.Count > 0)
                Session["CHECKED_ITEMS"] = userdetails;
        }

when i run the above code shows error as follows

Object cannot be cast from DBNull to other types.

The error shows in below line as follows

 index = Convert.ToInt32(grdRpt.DataKeys[gvrow.RowIndex].Value);


 In the gridview as follows

 selectdata  transacteeid     Qty   Price Isactive

  checkbox                      6      25       1
  checkbox                      4      20       1
  checkbox  109453628727        1      8        1
  checkbox  109453628727        2      6        1
  checkbox  109453628727        3      7        1
  checkbox  109453628727        5      2        1
  checkbox  109453628727        1      5        1

how to fix this error.

 in the gridview first two rows values is empty. because of this shows error. i think.

 how to validate this null value in the gridview.

What I have tried:

My code as follows

  private void SaveCheckedValues()
        {
             System.Collections.ArrayList userdetails = new 
             System.Collections.ArrayList();
            int index = -1;
            foreach (GridViewRow gvrow in grdRpt.Rows)
            {
              index = Convert.ToInt32(grdRpt.DataKeys[gvrow.RowIndex].Value);
              <pre> bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

               
                if (Session["CHECKED_ITEMS"] != null)
                    userdetails = (System.Collections.ArrayList)Session["CHECKED_ITEMS"];
                if (result)
                {
                    if (!userdetails.Contains(index))
                        userdetails.Add(index);
                }
                else
                    userdetails.Remove(index);
            }
            if (userdetails != null && userdetails.Count > 0)
                Session["CHECKED_ITEMS"] = userdetails;
        }

when i run the above code shows error as follows

Object cannot be cast from DBNull to other types.

The error shows in below line as follows

 index = Convert.ToInt32(grdRpt.DataKeys[gvrow.RowIndex].Value);


 In the gridview as follows

 selectdata  transacteeid     Qty   Price Isactive

  checkbox                      6      25       1
  checkbox                      4      20       1
  checkbox  109453628727        1      8        1
  checkbox  109453628727        2      6        1
  checkbox  109453628727        3      7        1
  checkbox  109453628727        5      2        1
  checkbox  109453628727        1      5        1

how to fix this error.

 in the gridview first two rows values is empty. because of this shows error. i think.

 how to validate this null value in the gridview.
Posted
Updated 18-Jun-18 2:08am

Please stop reposting the same question over and over. Go to the original where people have given you suggestions and add further information or reply to the comments. You may also like to spend some more time studying the basics of C# classes and objects.
 
Share this answer
 
Convert.toInt function will faile to convert null value to integer. Instead you can make use of Tryparse which will try to convert your input string to integer and if failed will default result to 0. Try with below it should resolve your issue.

int result;
bool isIntValue = Int32.TryParse(grdRpt.DataKeys[gvrow.RowIndex].Value.ToString(), out result);
index = result;

//Output
// when grdRpt.DataKeys[gvrow.RowIndex].Value = DbNull.Value
//index = 0
// when grdRpt.DataKeys[gvrow.RowIndex].Value = "3"
// index = 3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900