Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access ArrayList Values from DbDataRecord type

Status
Not open for further replies.

s789654

Programmer
Nov 4, 2004
4
US
Hi All,
I have this great little snippet of code that I bind to a Datalist with no problem.
When I go to trace out individual values of the arraylist , I get all sorts of errors.
I am populating the arraylist with a sqldatareader result set. Unfortunately I can find very little helpful information on this class “System.Data.Common.DbDataRecord”. I just can’t seem to access individual values.
Any help would be greatly appreciated!
Thanks,
Nathan
(Notice my commented notes within the code)

public ArrayList GetSQLDataReaderAryLst(string sqlString)
{

// sqlString = “select mid, MediaTitle, LastName from Media”

SqlConnection conn = new SqlConnection(_connStr);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sqlString;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
ArrayList ar = new ArrayList();
foreach (System.Data.Common.DbDataRecord ddr in dr)
{
ar.Add(ddr);
}

// my problems are here below:
// I’m trying to grab the first row value where the column equals “MediaTitle”
string passMsg = ((ddr)ar[0]).MediaTitle.ToString();
Trace.Warn("one value:", passMsg);

conn.Dispose();

// the arraylist binds fine to the datalist
return ar;


//dataGridView1.DataSource = ar; // this is the end result
}

 
A datareader is forward-only stream of rows from a SQL Server database, like you use File object.
So here is a minimal code that you should use when using DataReader. In the while below you decide what to store in the arraylist.
Code:
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
ArrayList ar = new ArrayList();
// Always call Read before accessing data.

while (dr.Read())
{
      //here you access any field of the record
      string passMsg = dr.GetString(1); //MediaTitle
}
// always call Close when done reading.
dr.Close();
I think you should use a DataSet.Fill() if you need an array list.

obi

 
Thanks obi,
I have tried several different ways of getting the data but have never used Dataset.Fill with an ArrayList. I will give that a try, but first an foremost I would like to use this fast code above thus my primary question would be, how do I get data out of an arrayList if it is an object of some type. I have tried many examples with no avail.
Nathan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top