Click here to Skip to main content
15,908,906 members
Please Sign up or sign in to vote.
0.00/5 (No 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)
          {
              int.Parse((string)grdRpt.DataKeys[gvrow.RowIndex].Value);

              bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

              // Check in the Session
              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;
      }

In gridview as follows

selectdata  transacteeid  totalprice   Qty   Isactive

 checkbox                    25         1        1
 checkbox      2355          10         2        1
 checkbox      1234           8         1        1



in the above gridview first row transacteeid value is empty.

when i run the above code shows error as follows

unable to caste object of type system.DBNull to type System.String.

The error shows in below line as follows


how to validate empty value in gridview row.

int.Parse((string)grdRpt.DataKeys[gvrow.RowIndex].Value);

how to solve this error.

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)
          {
              int.Parse((string)grdRpt.DataKeys[gvrow.RowIndex].Value);

              bool result = ((CheckBox)gvrow.FindControl("chkselecdata")).Checked;

              // Check in the Session
              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;
      }

In gridview as follows

selectdata  transacteeid  totalprice   Qty   Isactive

 checkbox                    25         1        1
 checkbox      2355          10         2        1
 checkbox      1234           8         1        1



in the above gridview first row transacteeid value is empty.

when i run the above code shows error as follows

unable to caste object of type system.DBNull to type System.String.

The error shows in below line as follows


how to validate empty value in gridview row.

int.Parse((string)grdRpt.DataKeys[gvrow.RowIndex].Value);

how to solve this error.
Posted
Updated 18-Jun-18 2:17am
v2
Comments
Richard MacCutchan 18-Jun-18 11:51am    
Why are you casting a Value to a string; the two are not compatible. You seem to be just writing random lines of code in the hope that something will magically do what you want.

int.Parse() is used to assign the value of another variable after parse the value.

No use of below line, so remove it from your code: -
C#
int.Parse((string)grdRpt.DataKeys[gvrow.RowIndex].Value);

Your problem will be resolved after removing this line.
check this link also: Int32.Parse Method (String) (System)[^]
 
Share this answer
 
v2
I believe You might be storing

int.Parse((string)grdRpt.DataKeys[gvrow.RowIndex].Value)


into a custom object. If not then you can remove this line.

and if you want to use it as a check then

if (available.Where(x => x.id == gvForCheckBox.DataKeys[gvrow.RowIndex].Value.ToString()).Any())
 
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