在数据网格视图中获取null [英] Getting null on data grid view

查看:138
本文介绍了在数据网格视图中获取null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我遵循本指南,修改了自己的
的一些代码,用于检索datagridview的选择。



这是



这是代码:

  private void LoadDataGrid()
{
con.Open();

cmd = new SqlCommand(@SELECT quotationID,quo_product
FROM JobQuotations
WHERE quo_custname = @custname,con);
cmd.Parameters.AddWithValue(@ custname,lblLoginName.Text);
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}

这是我选择一行的类:

  private void LoadOrders()
{
con.Open();
cmd = new SqlCommand(@Select quo_product,quo_address
FROM JobQuotations
WHERE quotationId = @id
AND quo_product = @product,con);

cmd.Parameters.AddWithValue(@ id,GridView1.SelectedRow.Cells [1] .Text);
cmd.Parameters.AddWithValue(@ product,GridView1.SelectedRow.Cells [2] .Text);

if(rdr.HasRows)
{
while(rdr.Read())
{
lblProductName.Text = rdr [quo_product] .ToString();
lblAddress.Text = rdr [quo_Address]。ToString();
}
}
con.Close();
}

这行代码给我错误:

 `if(rdr.HasRows)`

* MejShop.dll中出现类型System.NullReferenceException的异常,但未在用户代码中处理



附加信息:对象引用未设置为一个对象的实例。*

解决方案

尝试添加 rdr = cmd.ExecuteReader(); while循环。


I followed this guide and modified some of its code for my own for retrieving a selectedrow for datagridview.

This is the guide.It's not that close to my problem thought as it doesn't use any sql query.

I'm using s Select query which needed a value from datagridview so when I triggered the button event,It gives me the value on my label.

here is the datagridview

Here is the code:

 private void LoadDataGrid()
    {
        con.Open();

        cmd = new SqlCommand(@"SELECT quotationID,quo_product
                             FROM JobQuotations
                             WHERE quo_custname = @custname", con);
        cmd.Parameters.AddWithValue("@custname",lblLoginName.Text);
        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        con.Close();
    }

and This is my class for selecting a row:

 private void LoadOrders()
    {
        con.Open();
        cmd = new SqlCommand(@"Select quo_product,quo_address
                            FROM JobQuotations
                            WHERE quotationId = @id
                            AND quo_product = @product",con);

        cmd.Parameters.AddWithValue("@id", GridView1.SelectedRow.Cells[1].Text);
        cmd.Parameters.AddWithValue("@product", GridView1.SelectedRow.Cells[2].Text);

        if(rdr.HasRows)
        {
            while(rdr.Read())
            {
                lblProductName.Text = rdr["quo_product"].ToString();
                lblAddress.Text = rdr["quo_Address"].ToString();
            }
        }
        con.Close();
    }

This line of code that is giving me error:

 `if(rdr.HasRows)`

*An exception of type 'System.NullReferenceException' occurred in MejShop.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.*

解决方案

Try and add the rdr = cmd.ExecuteReader(); above the while loop.

这篇关于在数据网格视图中获取null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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