Click here to Skip to main content
15,884,978 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.


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:

C#
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:

C#
lblCount3.Text = count2.ToString();


What I have tried:

I have tried an If Else Statement but no luck.
Posted
Updated 5-Oct-17 5:41am
Comments
A_Griffin 3-Oct-17 10:38am    
SELECT COUNT will always return an integer, unless there is an error. Check your SQL and database connection.
Also, it is BAD idea to build your SQL from string concatenation like that - ou leave yourself open to attack and input errors. You should use a paramatized query.
Atlapure Ambrish 3-Oct-17 10:45am    
The ideal approach for such scenario should be:

Use SqlCommand.ExecuteScalar() and cast it to an int, something like below:

cmd.CommandText = "SELECT COUNT(*) FROM table_name";
Int32 count = (Int32) cmd.ExecuteScalar();

you may use Convert.Int32 for casting.
Computer Wiz99 3-Oct-17 10:49am    
Thanks. Will this also work if I use it in a If Else statement?
Atlapure Ambrish 3-Oct-17 10:52am    
Yep, definitely, I don't see any reason for this to not to work.
Richard Deeming 3-Oct-17 12:38pm    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

using (SqlCommand com2 = new SqlCommand("Select count(AutoID) from TableFIN where SUBMITTED = 'False' and FINYR = @FINYR GROUP BY SUBMITTED, FINYR", con2))
{
    com2.Parameters.AddWithValue("@FINYR", TextBoxFINYR.Text);
    
    object count2 = com2.ExecuteScalar();
    lblCount3.Text = Convert.ToString(count2);
}

1 solution

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

C#
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();
 
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