CrBruno
CrBruno

Reputation: 1003

Read specific column in dataset

I have a question, how to read a specific column form data set....

I have this code:

cmd_line.CommandText = "SELECT IDOC_NUM, SEG_NUM FROM AGR3PL_LINE WHERE IDOC_NUM = '" + Lidoc_num + "'";

OracleDataAdapter ora_adapter_li = new OracleDataAdapter(cmd_line);
DataSet ds_idoc_li = new DataSet("AGR3PL_LINE");
ora_adapter_li.Fill(ds_idoc_li, "AGR3PL_LINE");

if (ds_idoc_li.Tables[0].Rows.Count == 0)
{
   SEG_NUM_L = 1;
}
else
{
   int var = Convert.ToInt32(ds_idoc_li.Tables["AGR3PL_LINE"]["SEG_NUM"]);
   SEG_NUM_L = var;
   SEG_NUM_L++;
}

First I make a select to extract SEG_NUM variable, then I check if is there any row in dataset, if is not set SEG_NUM = 1, else if there is any data extract SEG_NUM, read it and increment it for 1.

I have problem with this part:

int var = Convert.ToInt32(ds_idoc_li.Tables["AGR3PL_LINE"].Rows[0]["SEG_NUM"]);

If I have 2 entries it works fine, but if there are more than 2 entries I get an error for 3. entry: There is no line at position 0, the counter always increments for 2.

This part is not right

 Convert.ToInt32(ds_idoc_li.Tables["AGR3PL_LINE"].Rows[0]["SEG_NUM"]);

Upvotes: 0

Views: 16184

Answers (1)

Niranjan Singh
Niranjan Singh

Reputation: 18290

you can access column value from the Rows collection of the DataTable as:

Convert.ToInt32(ds_idoc_li.Tables["AGR3PL_LINE"].Rows[indexofRow][columnName or Index]);

Try this:

if (ds_idoc_li.Tables[0].Rows.Count == 0)
{
   SEG_NUM_L = 1;
}
else
{
  foreach(DataRow row in ds_idoc_li.Tables["AGR3PL_LINE"].Rows)
  {
   int var = Convert.ToInt32(row["SEG_NUM"]);
   SEG_NUM_L = var;
   }
   SEG_NUM_L++;  
}

Upvotes: 2

Related Questions