---
title: "Discussion on \"Will a for loop or foreach loop throw an exception in C# if the DataTable is null or contains no rows?\" | Hashnode"
description: "Discussion on \"Will a for loop or foreach loop throw an exception in C# if the DataTable is null or contains no rows?\". In C#, when working with a DataTable, you won’t get an exception from a for loop or foreach loop if the DataTable itself is not null but has no rows. However, if the DataTable is null, then trying to loop through it will throw a NullReferenceExceptio..."
url: "https://hashnode.com/posts/will-a-for-loop-or-foreach-loop-throw-an-exception-in-c-if-the-datatable-is-null-or-contains-no-rows/67f8a70ceda8eb03ec3b4a69"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Will a for loop or foreach loop throw an exception in C# if the DataTable is null or contains no rows?](https://aamasum.hashnode.dev/will-a-for-loop-or-foreach-loop-throw-an-exception-in-c-if-the-datatable-is-null-or-contains-no-rows?utm_source=hashnode&utm_medium=feed)

In C#, when working with a DataTable, you won’t get an exception from a for loop or foreach loop if the DataTable itself is not null but has no rows. However, if the DataTable is null, then trying to loop through it will throw a NullReferenceExceptio...

Apr 11, 2025 · 3 min read · aamasum.hashnode.dev  
Read the article

-   [#datatable](https://hashnode.com/tag/datatable)

## Written by

[Abdullah Al Masum](https://hashnode.com/@aamasum)

@aamasum · Dhaka, Bangladesh

  
[View profile](https://hashnode.com/@aamasum)

Fullstack Developer

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
