The following chock of C# code will throw an error, if the column name does not exist in the DataTable.
//Error Code:
string strFieldName = "****";
if (ds != null &&
ds.Tables[0] != null &&
ds.Tables[0].Rows.Count > 0 &&
ds.Tables[0].Rows[0][strFieldName] != null)
{
string strValue = ds.Tables[0].Rows[0][strFieldName].ToString().Trim();
}
//Correct Code:
string strFieldName = "****";
if (ds != null &&
ds.Tables[0] != null &&
ds.Tables[0].Rows.Count > 0 &&
ds.Tables[0].Columns.Contains(strFieldName) == true &&
ds.Tables[0].Rows[0][strFieldName] != null)
{
string strValue = ds.Tables[0].Rows[0][strFieldName].ToString().Trim();
}
//Error Code:
string strFieldName = "****";
if (ds != null &&
ds.Tables[0] != null &&
ds.Tables[0].Rows.Count > 0 &&
ds.Tables[0].Rows[0][strFieldName] != null)
{
string strValue = ds.Tables[0].Rows[0][strFieldName].ToString().Trim();
}
//Correct Code:
string strFieldName = "****";
if (ds != null &&
ds.Tables[0] != null &&
ds.Tables[0].Rows.Count > 0 &&
ds.Tables[0].Columns.Contains(strFieldName) == true &&
ds.Tables[0].Rows[0][strFieldName] != null)
{
string strValue = ds.Tables[0].Rows[0][strFieldName].ToString().Trim();
}