如何验证gridview的null值 [英] How to validate the null value of gridview

查看:78
本文介绍了如何验证gridview的null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下



private void SaveCheckedValues()

{

System.Collections.ArrayList userdetails = new

System.Collections.ArrayList();

int index = -1;

foreach(grdRpt.Rows中的GridViewRow gvrow)

{

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

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.

推荐答案

请停止重新发布相同的问题过度。转到人们给你建议的原件,并添加更多信息或回复评论。您可能还想花更多时间研究C#类和对象的基础知识。
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.


Convert.toInt函数将故障转换为null值为整数。相反,您可以使用Tryparse,它会尝试将您的输入字符串转换为整数,如果失败,则默认结果为0.尝试使用下面它可以解决您的问题。



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


这篇关于如何验证gridview的null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆