i have a scenario where some column
name of a DataTable
may not be present. Because i am creating a dynamic DataTable
.
DataTable tbl = new DataTable();
tbl.Columns.Add("Roll");
tbl.Columns.Add("Name");
DataRow dr = tbl.NewRow();
dr["Name"] = "Arshad";
dr["Roll"] = 1;
tbl.Rows.Add(dr);
Console.WriteLine(dr["Address"]);// exception, or
Console.WriteLine(Convert.ToString(dr["Address"]));
I want to check whether this DataTable
contains a column
called Address or not. Is it possible like we have in Dictionary
like:
if (objDictionary.ContainsKey("Address"))
{
}
Solution 1Add(new DataColumn("@Id", typeof(int))); DataRow dr = dt. NewRow(); dr["Id"] = 666; You will get the same error. DataTable dt = new DataTable(); dt.
So Table, tr, td belongs to table tags in HTML and form does not belong.
You can use DataColumnCollection.Contains Method method as
if(dt.Columns.Contains("Address"))
//column exists
DataColumnCollection.Contains Method
Checks whether the collection contains a column with the specified name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With