How to find no of rows returns by SqlDataAdapter

  • Thread starter Thread starter Imran Aziz
  • Start date Start date
I

Imran Aziz

Hello All,
I am using the following code

DataSet ds = new DataSet();

try

{

strQuery = "SELECT sChannelTitle, sChannelDescription, nChannelID,
dDateAdded, sChannelType, nUserID" +

" FROM tblFeedChannels ";

if (strSearch != "" || sFeedType != "")

{

strQuery = strQuery + "where tblFeedChannels.sChannelTitle like '%" +
strSearch + "%' and sFeedType='" + sFeedType + "'";

}

SqlDataAdapter myCommand = new SqlDataAdapter(strQuery, conn);

// Create and fill a DataSet.

myCommand.Fill(ds);


}

catch (SqlException ex)

{

strError = "Error is " + ex.Message;

}

return ds;

but on some situations based on search the SqlDataAdapter does not return
any results, how do I check the number of rows returned from the command so
that I dont fill the dataset accordingly and just set it to null.

Thanks a lot.

Imran.
 
Imran,

The action of executing

myDataAdapter.Fill(myDataSet)

returns the number of records returned by the SELECT statement.

myDataSet.Tables(tableNumber).Rows.Count

If you want to know how many records you have before filling the
dataset you might try using a datareader first and step through the
returned data. If this returns no records then don't fill the
dataset. Otherwise, I don't think that there is a way of doing what
you ask.

Jason.
 
I would rely on peering into properties of the datatable that you are
filling. Instead, the fill method of the dataadapter returns the number of
records affected.

int iResult = (int)myCommand.Fill(ds);

I think this way is a better practice. Additionally, you could do something
like this:

datatable dt;
dt = new datatable();

.... search code

int iResult = (int)myCommand.Fill(dt);

if (iResult >0)
{
ds.merge(dt, true);
}
 
Thanks a lot for the tip.
Imran.
AMDRIT said:
I would rely on peering into properties of the datatable that you are
filling. Instead, the fill method of the dataadapter returns the number of
records affected.

int iResult = (int)myCommand.Fill(ds);

I think this way is a better practice. Additionally, you could do
something like this:

datatable dt;
dt = new datatable();

... search code

int iResult = (int)myCommand.Fill(dt);

if (iResult >0)
{
ds.merge(dt, true);
}
 
Back
Top