Skip to content

C#.net

ranjan sahoo

Asked Apr 1, 2013, 9:56 PM EDT2 answers
Share
LinkedInX
what is the meaning of
1. ds.Tables[0].Rows.Count > 0
2.ds.Tables[0].Rows[0]["column_name"].ToString();
3.dt.rows.count>0
 with example

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Vishal Gilbile
    0
    Hello Friend,
                    The following lines conclude the following things.

    1. ds.Tables[0].Rows.Count>0

    Lets say that you wrote a code for filling data from database to dataset using an SqlDataAdapter. Sometimes it might happen that there is no records in the database. So for such scenario we always make a habbit to check whether there is any data or not. This could be done by the above code.

    Code
    if(ds.Tables[0].Rows.Count>0)
    gridview1.DataSource=ds.Tables[0].DefaultView;
    else
    gridview1.EmptyDataText="No Records Found";
    gridview1.DataBind();
    
    
    2.ds.Tables[0].Rows[0]["column_name"].ToString();

    This is done for assigning a particular column data to let's say a listbox or dropdownlist. For instance to assign the employeeid of employee table to dropdownlist the following line of code will do it.

    Code
    if(ds.Tables[0].Rows.Count>0)
    {
    Dropdownlist1.DataSource=ds.Tables[0].DefaultView;
    Dropdownlist1.DataValueField=ds.Tables[0].Rows[0]["empid"].ToString();
    Dropdownlist1.Databind();
    }


    3.dt.rows.count>0
    This is same as that of first one. Only here we are checking with dataTable.


    Hope that Solves your problem.

    With Regards,
    Vishal Gilbile.
    0
  • 0


    1. ds.Tables[0].Rows.Count > 0

    Ans : Data set can contains number of tables, here ds is data set and table [0] is pointing to 1st table index in data set. Rows.Count is a property in data table which will return you the number of rows in data table. The above statement ds.Tables[0].Rows.Count is returning you the number of rows in first table of data set ds. This is the conditional statement, here one is checking total number of rows with predefined value in this case it 0.

    2.ds.Tables[0].Rows[0]["column_name"].ToString();

     Ans : This statement will return you the first rows value of ds.Tables[0] table, and for which column you want value u can specify instead of "column_name"

    3.dt.rows.count>0

    Ans : the first statement and this statment has no diffrence. In 1 data table was from data set object and here we are directly using data tables object.
    0