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

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

问题描述

我遵循这个指南,并修改了一些它的code的我自己
用于检索的datagridview一个selectedrow。

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

这是<一个href=\"http://www.aspsnippets.com/Articles/How-to-get-Selected-Row-cell-value-from-GridView-in-ASPNet.aspx\"相对=nofollow>指南。它不是那么贴近我的问题看作是它不使用任何SQL查询。

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

我使用S 选择查询,它需要从DataGridView的值,所以当我触发按钮的事件,它给了我,我的标签上的价值。

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.

这里是DataGridView的

here is the datagridview

在这里输入的形象描述

下面是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();
    }

和这是我选择行类:

 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();
    }

这行code,它是给我的错误:

This line of code that is giving me error:

 `if(rdr.HasRows)`

*类型'System.NullReferenceException'的例外发生在MejShop.dll但在用户code没有处理

*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.*

推荐答案

尝试并添加 RDR = cmd.ExecuteReader(); while循环以上。

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

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

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