none
How to handle missing element when loading data from dataset to list RRS feed

  • Question

  • var myData = ds.Tables[0].AsEnumerable().Select(r => new Employee {
        Name = r.Field<string>("Name"),
        Age = r.Field<int>("Age")
    });
    var list = myData.ToList(); 

    suppose in my xml some records does not have Age element but in above code i am reading data from Age element and sure for that i will get exception. so tell me how to write code in such way that if records has age element then read data from age otherwise 0 will be assign to age variable if Age element is missing in records.

    https://stackoverflow.com/a/17107330/10975962

    Please answer with code example. thanks


    • Edited by Sudip_inn Monday, March 18, 2019 8:06 PM
    Monday, March 18, 2019 8:05 PM

Answers

  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, a simple way was convert null value to 0. I make a simple way for your reference.

    My Test database.

    My code.

    class Program { public static void Main() {

    //read data from database to datatable System.Data.DataTable dataTable = new System.Data.DataTable(); string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=Database.mdf;Integrated Security=True;Connect Timeout=30"; string query = "select * from Test"; DataSet ds = new DataSet(); using (SqlConnection conn = new SqlConnection(connString)) { using (SqlCommand cmd = new SqlCommand(query, conn)) { // create data adapter using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(ds); dataTable = ds.Tables[0]; } } }

    //convert null value to 0 for (int i = 0; i < dataTable.Rows.Count; i++) { for (int j = 0; j < dataTable.Columns.Count; j++) { if (string.IsNullOrEmpty(dataTable.Rows[i][j].ToString())) { dataTable.Rows[i][j] = "0"; } } }

    //read data from datatable var myData = dataTable.AsEnumerable().Select(r => new Employee { Name = r.Field<string>("Name"), Age = r.Field<int>("Age") }); var list = myData.ToList(); } } class Employee { public string Name { get; set; } public int Age { get; set; } }


    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, March 20, 2019 12:53 PM
    Tuesday, March 19, 2019 9:02 AM
    Moderator
  • var myData = ds.Tables[0].AsEnumerable().Select(r => new Employee {
        Name = r.Field<string>("Name"),
        Age = r.Field("Age") != null ? r.Field<int>("Age") : 0;
    });
    var list = myData.ToList(); 

    You can use the Ternary in the Linq projection. If it is not null then you use row field column value for Age, otherwise you set Age = 0.

    https://www.dotnetperls.com/ternary

    Tuesday, March 19, 2019 9:28 AM

All replies

  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, a simple way was convert null value to 0. I make a simple way for your reference.

    My Test database.

    My code.

    class Program { public static void Main() {

    //read data from database to datatable System.Data.DataTable dataTable = new System.Data.DataTable(); string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=Database.mdf;Integrated Security=True;Connect Timeout=30"; string query = "select * from Test"; DataSet ds = new DataSet(); using (SqlConnection conn = new SqlConnection(connString)) { using (SqlCommand cmd = new SqlCommand(query, conn)) { // create data adapter using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(ds); dataTable = ds.Tables[0]; } } }

    //convert null value to 0 for (int i = 0; i < dataTable.Rows.Count; i++) { for (int j = 0; j < dataTable.Columns.Count; j++) { if (string.IsNullOrEmpty(dataTable.Rows[i][j].ToString())) { dataTable.Rows[i][j] = "0"; } } }

    //read data from datatable var myData = dataTable.AsEnumerable().Select(r => new Employee { Name = r.Field<string>("Name"), Age = r.Field<int>("Age") }); var list = myData.ToList(); } } class Employee { public string Name { get; set; } public int Age { get; set; } }


    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, March 20, 2019 12:53 PM
    Tuesday, March 19, 2019 9:02 AM
    Moderator
  • var myData = ds.Tables[0].AsEnumerable().Select(r => new Employee {
        Name = r.Field<string>("Name"),
        Age = r.Field("Age") != null ? r.Field<int>("Age") : 0;
    });
    var list = myData.ToList(); 

    You can use the Ternary in the Linq projection. If it is not null then you use row field column value for Age, otherwise you set Age = 0.

    https://www.dotnetperls.com/ternary

    Tuesday, March 19, 2019 9:28 AM