help with datatable

  • Thread starter Thread starter Rodney
  • Start date Start date
R

Rodney

I am having trouble with getting at the last record in a datatable. I
need to find the id and increment it.

int x = logtbl.Rows.Count;
string x_id = logtbl.Rows[x]["fldid"].ToString();

//here it tells me that x_id has a null value because no row exists at
(x = 8)

int id = Convert.ToInt32(x_id);
MessageBox.Show(x_id);

Is there an easier way to accomplish this.
 
Try:

if (logtbl.Rows.Count > 0)
{
int x = logtbl.Rows.Count - 1;
string x_id = logtbl.Rows[x]["fldid"].ToString();
}

Arrays/collections are zero based. So if "Count" was 5 then the actual
indices would be 0 to 4.
 
Thanks for taking the time, I knew I should of known the answer.

rod


Tim Wilson said:
Try:

if (logtbl.Rows.Count > 0)
{
int x = logtbl.Rows.Count - 1;
string x_id = logtbl.Rows[x]["fldid"].ToString();
}

Arrays/collections are zero based. So if "Count" was 5 then the actual
indices would be 0 to 4.

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}
Rodney said:
I am having trouble with getting at the last record in a datatable. I
need to find the id and increment it.

int x = logtbl.Rows.Count;
string x_id = logtbl.Rows[x]["fldid"].ToString();

//here it tells me that x_id has a null value because no row exists at
(x = 8)

int id = Convert.ToInt32(x_id);
MessageBox.Show(x_id);

Is there an easier way to accomplish this.
 
Back
Top