11

I have a DataTable populated with samo data/values and I want to read data from DataTable and pass it to a string variable.

I have this code:

DataTable dr_art_line_2 = ds.Tables["QuantityInIssueUnit"];

I have a countert like this:

for (int i = 1; i <= broj_ds; i++ )
{                                    
    QuantityInIssueUnit_value => VALUE FROM DataTable
    QuantityInIssueUnit_uom  => VALUE FROM DataTable    
}

Is this possible or not? If yes then how to pass data from DataTable to those variables?

Thanks!

4
  • What is broj_ds (I mean its "origins")? Commented Jul 19, 2012 at 8:38
  • It's a int value, serves in a counter, no string to datatable, i use it to count elements in dataset int broj_ds = ds.Tables["Line"].Rows.Count; Commented Jul 19, 2012 at 8:41
  • Why would you want to overwrite the value of your variables on each iteration ? Commented Jul 19, 2012 at 8:59
  • I have a for-each loop and in it i have this for loop, for loop must execute as many times as for-each does, not more,not less, so i have to build some kind of counter to work all that Commented Jul 19, 2012 at 9:05

4 Answers 4

21
DataTable dr_art_line_2 = ds.Tables["QuantityInIssueUnit"];

for (int i = 0; i < dr_art_line_2.Rows.Count; i++)
{
    QuantityInIssueUnit_value = Convert.ToInt32(dr_art_line_2.Rows[i]["columnname"]);
    //Similarly for QuantityInIssueUnit_uom.
}
Sign up to request clarification or add additional context in comments.

Comments

8

You can do it using the foreach loop

DataTable dr_art_line_2 = ds.Tables["QuantityInIssueUnit"];

  foreach(DataRow row in dr_art_line_2.Rows)
  {
     QuantityInIssueUnit_value = Convert.ToInt32(row["columnname"]);
  }

Comments

0

I think it will work

for (int i = 1; i <= broj_ds; i++ ) 
  { 

     QuantityInIssueUnit_value = dr_art_line_2[i]["Column"];
     QuantityInIssueUnit_uom  = dr_art_line_2[i]["Column"];

  }

Comments

0

For VB.Net is

        Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "database path")
        Dim cmd As New OleDb.OleDbCommand
        Dim dt As New DataTable
        Dim da As New OleDb.OleDbDataAdapter
        con.Open()
        cmd.Connection = con
        cmd.CommandText = sql
        da.SelectCommand = cmd

        da.Fill(dt)

        For i As Integer = 0 To dt.Rows.Count
            someVar = dt.Rows(i)("fieldName")
        Next

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.