locked
SqlDataAdapter RRS feed

  • Question

  • User-1499457942 posted

    Hi

       I have below code . If there is no record , what should be the value in dt

    string strQuery = "Select * from Period where IsClosed = 'N'";
    SqlDataAdapter sdaPeriod = new SqlDataAdapter(strQuery, con);
    DataTable dt = new DataTable();
    sdaPeriod.Fill(dt);

    Thanks

    Monday, October 8, 2018 11:21 AM

Answers

  • User-821857111 posted

    The DataTable will never be null because you used the new keyword to create an instance of it. It will just have no rows of data in it. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 8, 2018 12:50 PM
  • User-1171043462 posted

    The check which I gave will work for both conditions.

    First will check NULL then Count.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 8, 2018 1:52 PM

All replies

  • User-1171043462 posted

    It will be empty or null you can check as follows

    if (dt != null && dt.Rows.Count > 0)
    {
       //Has Data
    }

    Monday, October 8, 2018 11:44 AM
  • User-1499457942 posted

    Hi

       If there is no record satisfying condition will it return 0 or null

    Thanks

    Monday, October 8, 2018 11:47 AM
  • User-821857111 posted

    The DataTable will never be null because you used the new keyword to create an instance of it. It will just have no rows of data in it. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 8, 2018 12:50 PM
  • User-1171043462 posted

    The check which I gave will work for both conditions.

    First will check NULL then Count.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 8, 2018 1:52 PM