Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8755361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:50:28+00:00 2026-06-13T13:50:28+00:00

How do I check if a specified column allows null values or not? I’m

  • 0

How do I check if a specified column allows null values or not?

I’m using the following code to print all the columns, but I also want to print if the column allows null values:

cnn = new SqlConnection(connetionString);
cnn.Open();

SqlCommand myCommand = new SqlCommand("select * from " + tableName, cnn);
SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();

da.Fill(ds, tableName);

foreach (DataColumn dc in ds.Tables[0].Columns)
{
     // Print stuff here, dc.ColumnName is the column name
}

The DataColumn.allowDBnull property doesn’t seem to work when getting a predefined table, it’s always set to true, even in columns that doesn’t allow nulls.

Thanks for your time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:50:29+00:00Added an answer on June 13, 2026 at 1:50 pm

    If you are only after column data I would do this from the system views, rather than relying on the data adapter. e.g.

    DECLARE @TableName VARCHAR(50) = 'dbo.TableName'
    
    SELECT  Name, Column_ID, Is_Nullable
    FROM    SYS.COLUMNS
    WHERE   [Object_ID] = OBJECT_ID(@TableName)
    

    This also means you can use parameterised queries properly and avoid the risks of SQL Injection. So your final code would be something like this:

    using (var connection = new SqlConnection(connectionString))
    using (var command = new SqlCommand("SELECT Name, Is_Nullable FROM sys.columns WHERE [Object_ID] = OBJECT_ID(@TableName)", connection))
    {
        connection.Open();
        command.Parameters.Add("@TableName", SqlDbType.VarChar).Value = tableName;
        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine("Name: {0}; Allows Null: {1}", reader.GetString(0), reader.GetBoolean(1));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need my program check if specified column exists in MS Access 2000 database,
We would like to check if a specified process is currently running via PHP.
I want to check whether any Zip file is present on a specified path.
I want to check whether the loggedIn user has liked a specified page or
How to check if a specific Column has Integer on each cell, and if
I want to update a specific column for all rows in a table in
[Previous essay-title for question] Oracle SQL: update parent table column if all child table
Is it possible to have a wildcard in a column name specified in the
I am trying to update a column in already exists MySQL table. The values
I have the following code that generates a table: <table class=table table-bordered table-striped id=assignedvs>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.