Convert a List<string> to DataRow in c#

Dhivya Sadasivam picture Dhivya Sadasivam · Jan 24, 2014 · Viewed 29.3k times · Source

I have a list of strings and am in need of converting it to a Datarow. I tried

toReturn.Add("UserID");
toReturn.Add("UserName");
DataRow row = null;
row.ItemArray=toreturn.ToArray();

This is throwing me an exception

Object reference not set to an instance of an object.

So i tried with

DataRow Row=new DataRow();

That is also not allowed.Can somebody help me in this.

Answer

SLaks picture SLaks · Jan 24, 2014

DataRows can only exist with a DataTable.

Create a DataTable with appropriate columns, then call table.Rows.Add(list.ToArray()).

However, you probably shouldn't be using DataRow in the first place.