Make sure aat least one record is returned

G

Guest

I am using the following code on a web form

public DataView CreateEquipmentDataSource(

string equipConnect = "Provider=\"MSDAORA.1\";" + GetConnectionString() + ""
string equipSelect = "Select Description, Isrunning, to_char(Warrantyexpdate, 'mm/dd/yyyy')Expdate"
" from Equipment where Eqnum = " + "'" + equipLabel.Text.ToUpper() + "'"

oda1 = new OleDbDataAdapter(equipSelect, equipConnect)
DataSet ds1 = new DataSet()

oda1.Fill(ds1, "Equip")
DataView Equip = ds1.Tables["Equip"].DefaultView
return Equip


public void FillEquipment(

DataView eq = CreateEquipmentDataSource()
equipDescLabel.Text = eq[0].Row["Description"].ToString()
eqUpLabel.Text = eq[0].Row["Isrunning"].ToString()


When running this code, I get the following error

Index 0 is not non-negative and below total rows count

How do I amend my code to make sure that at least one record is returned

Thanks

Dave
 
C

Chris R. Timmons

I am using the following code on a web form:

public DataView CreateEquipmentDataSource()
{
string equipConnect = "Provider=\"MSDAORA.1\";" +
GetConnectionString() + ""; string equipSelect =
"Select Description, Isrunning,
to_char(Warrantyexpdate, 'mm/dd/yyyy')Expdate" +
" from Equipment where Eqnum = " + "'" +
equipLabel.Text.ToUpper() + "'";

oda1 = new OleDbDataAdapter(equipSelect,
equipConnect); DataSet ds1 = new DataSet();

oda1.Fill(ds1, "Equip");
DataView Equip = ds1.Tables["Equip"].DefaultView;
return Equip;
}

public void FillEquipment()
{
DataView eq = CreateEquipmentDataSource();
equipDescLabel.Text =
eq[0].Row["Description"].ToString();
eqUpLabel.Text =
eq[0].Row["Isrunning"].ToString();
}

When running this code, I get the following error:

Index 0 is not non-negative and below total rows count.

How do I amend my code to make sure that at least one record is
returned?

Dave,

Depending on your data, their may be no way to guarantee that the
DataView will have any rows.

A better approach is to handle the case where their are no rows in
the result set:


public void FillEquipment()
{
DataView eq = CreateEquipmentDataSource();

if (eq.Count == 0)
{
equipDescLabel.Text = "No rows in result set.";
eqUpLabel.Text = "No rows in result set.";
}
else
{
equipDescLabel.Text = eq[0].Row["Description"].ToString();
eqUpLabel.Text = eq[0].Row["Isrunning"].ToString();
}
}


Hope this helps.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Index 0 Error 1

Top