[Solved] Cannot set Column ‘ID’ to be null. Please use DBNull instead in SharePoint Online

.net cannot set column to be null. please use dbnull instead

In this SharePoint csom example, I will explain how to solve error “Cannot set Column ‘ID’ to be null. Please use DBNull instead” which comes while working with client side object model (csom C#.Net) in SharePoint Online Office 365.


I have written some code to retrieve all the SharePoint list template and bind SharePoint list template to a dropdown list, and I got the error “Cannot set Column ‘ID’ to be null. Please use DBNull instead“. I have retrieved all the list template and stored to a data table and bind the list template to the dropdown list. I am trying to run the below code and got the error.

[code]

Web web = clientContext.Web;
ListTemplateCollection templateColl = web.ListTemplates;
clientContext.Load(templateColl);

clientContext.ExecuteQuery();

DataTable dt = new DataTable();
dt.Columns.Add(“ID”, typeof(Int32));
dt.Columns.Add(“Title”, typeof(string));

foreach (ListTemplate template in templateColl)
{
DataRow dr = dt.NewRow();
dr[“ID”] = template[“ID”];
dr[“Title”] = template.Name;
dt.Rows.Add(dr);
}

[/code]

Once I run the above code, it gave me the below error in the below line.

dr[“ID”] = template[“ID”];

.net cannot set column to be null. please use dbnull instead
.net cannot set column to be null. please use dbnull instead

Solution to error ‘Cannot set Column ‘ID’ to be null. Please use DBNull instead’:

Here the error is coming because I have set the ID value as integer “dt.Columns.Add(“ID”, typeof(Int32));”.

dr[“ID”] = template[“ID”]; //But template[“ID”] returns null. So I have got the error Cannot set Column ‘ID’ to be null. Please use DBNull instead”.

So I have added “ListTemplateTypeKind” property whose return type is “integer”

dr[“ID”] = template.ListTemplateTypeKind;

You may like following SharePoint Online CSOM tutorials:



In the above SharePoint article, we were discussed how to resolve the error Cannot set Column ‘ID’ to be null. Please use DBNull instead” while binding the SharePoint list template to the dropdown list in SharePoint online.

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →