如果没有可用计数,如何计数显示零? [英] How to have count to display zero if no count is available?

查看:70
本文介绍了如果没有可用计数,如何计数显示零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gridview,可以显示输入数据库的内容以及数据状态的更改时间。我有一个Count代码,告诉我有多少遗留在不同的状态点。当状态一状态变为零时,我想在计数中显示0并仍然显示我在表单上的所有其他网格视图。现在我收到这个错误:



I have a Gridview that shows me what is being entered into a database and when the status of the data is changed. I have a Count code in place to show me how many are left in different status points. When the status one status gets to zero, I want to display 0 in the count and still show all the other Gridviews I have on the form. Right now I get this error:

 Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.





哪个我知道它是什么,但是当Gridview没有任何显示时,试图让计数显示为零。有没有办法做这个代码?



这是我的计数代码:





Which I know what it is but trying to get the count to display zero when the Gridview has nothing to show. Is there a way to do this code?

Here is my Count code:

SqlCommand com2 = new SqlCommand("Select count(AutoID) from TableFIN where SUBMITTED = 'False' and FINYR = '" + TextBoxFINYR.Text + "' GROUP BY SUBMITTED, FINYR", con2);
            object count2 = com2.ExecuteScalar();
            lblCount3.Text = count2.ToString();





错误在这里:





The error is here:

lblCount3.Text = count2.ToString();





< b>我尝试了什么:



我尝试了一个If Else声明,但没有运气。



What I have tried:

I have tried an If Else Statement but no luck.

推荐答案

我已经解决了我的问题。这是我使用的代码。



I have solved my issue. Here is the code I used.

DataTable dt = new DataTable();

            SqlDataReader sqlDataReader = com3.ExecuteReader();


            dt.Load(sqlDataReader);
            sqlDataReader.Close();

            GridView dataGridView4 = new GridView();
            GridView4.DataSource = dt;
            GridView4.DataBind();
            lblCount4.Text = GridView4.Rows.Count.ToString();


这篇关于如果没有可用计数,如何计数显示零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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