Fill an array (or arraylist) from SqlDataReader
It is possible. In .NET 2.0+, SqlDataReader
inherits from DbDataReader
, which implements IEnumerable
(non-generic one). This means that you can use LINQ:
List<string> list = (from IDataRecord r in dataReader
select (string)r["FieldName"]
).ToList();
That said, the loop is still there, it's just hidden in Enumerable.Select
, rather than being explicit in your code.
Using SqlDataReader to fill an ArrayList?
A couple of things I would check:
- Is the connection string ok
- Does the user in your connection string have access to the database/view
- Can you access that database from the pc your at
- Does the ViewEligable view exist
- Does the view contain a field1 and field2 column.
Here one way you could possibly clean up that code somewhat (assuming you have .net 2.0)
public List<EligibleClass> GetEligibles(string sConnectionString)
{
List<EligibleClass> alEligible = null;
try
{
using (SqlConnection sConn = new SqlConnection(sConnectionString))
{
sConn.Open();
using (SqlCommand sCmd = new SqlCommand())
{
sCmd.Connection = sConn;
sCmd.CommandText = "SELECT field1, field2 FROM ViewEligible";
using (SqlDataReader sqlReader = sCmd.ExecuteReader())
{
while (sqlReader.Read())
{
EligibleClass Person = new EligibleClass();
Person.field1 = sqlReader.GetString(0);
Person.field2 = sqlReader.GetString(1);
if (alEligible == null) alEligible = new List<EligibleClass>();
alEligible.Add(Person);
}
}
}
}
}
catch (Exception ex)
{
// do something.
}
return alEligible;
}
C# - Convert SqlDataReader to ArrayList
You could use a SqlDataReader to build a DataTable and pass this object
DataTable dt = new DataTable();
if (rdr.HasRows == true)
dt.Load(rdr);
A very complete example with various options available could be found on MSDN
Now you could pass the whole DataTable instance and use it to bind the GridView
Fill any ArrayList with results of an SQL statement
If you are using C# 2.0. or greater use Generics
rather than ArrayList
.
You could use Reflection
to the get the properties of the type you pass in:
public void FillArrayList<T>(DataGridView grid, SqlDataReader reader, List<T> list)
{
//Fill the list with the contents of the reader
while (reader.Read())
{
Object obj = new Object();
Type type = typeof(T); // get the type of T (The paramter you passed in, i.e. Organisations)
FieldInfo[] fields = type.GetFields(); // Get the fields of the assembly
int i = 0;
foreach(var field in fields) // Loop round the fields
{
field.setValue(obj, reader[i]); // set the fields of T to the readers value
// field.setValue(obj, reader[field.Name]); // You can also set the field value to the explicit reader name, i.e. reader["YourProperty"]
i++;
}
list.Add(obj);
}
}
To invoke it:
FillArrayList(grid, reader, list);
Where list is a List type of Organisations
List<Organisations> list = new List<Organisations>();
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx
Filling an arraylist from a datareader
I think you would be better off with a DataTable and an adapter:
SqlConnection Conn = new SqlConnection(MyConnectionString);
Conn.Open();
SqlDataAdapter Adapter = new SqlDataAdapter("Select * from Employees", Conn);
DataTable Employees = new DataTable();
Adapter.Fill(Employees);
GridView1.DataSource = Employees;
GridView1.DataBind();
Of course in your case, use the Oracle versions of the objects.
How to get data from SqlDataReader to Array of arrays or Array of strings
What you're missing here is that an array is a reference type. So when you say:
MainArray[j] = ActivityArray
You're setting the entry in MainArray to point to ActivityArray. You never create a new ActivityArray, so they all point to the same place.
Because you're over-writing the content of ActivityArray, you're effectively over-writing all entries in MainArray (because they are all the same entry).
So, you probably want
while (myReader1.Read())
{
ActivityArray = new int[52];
if (!myReader1.IsDBNull(0) && !myReader1.IsDBNull(1))
{
The reason I have put probably is because it's not really that clear to me what your code is trying to do.
Populate an Array of Object Based on DataReader Data
You should use an Arraylist or -better- a generic List(of Product).
Besides i would strongly recommend to set Option Strict On in your project's Compiler Settings.
Dim products As New List(Of Product)
While datareaderData.read()
Dim nextProduct As New Product
nextProduct.ProductID = CType(datareaderData("ID"), System.Int32)
nextProduct.Name = datareaderData("Name").ToString
products.add(nextProduct)
End While
Related Topics
Wcf Service Attribute to Log Method Calls and Exceptions
Soap Authentication Fails When Running a C# App on a Linux Box
How to Open the Physical File Operating System Error 32
Reading/Writing from Named Pipes Under Mono/Linux
ASP.NET MVC 4 + Ninject MVC 3 = No Parameterless Constructor Defined for This Object
Signalr Owin Self-Host on Linux/Mono Socketexception When Clients Lose Connection
C#: Referencing or Using .So Files in .Net Core on Linux
Visual Studio C# Intellisense Not Automatically Displaying
What Is Difference Between Regasm.Exe and Regsvr32? How to Generate a Tlb File Using Regsvr32
How to Write My C++ Function So I Can Call It from C#
Detecting Symbolic Links and Pipes in Mono
How to Pass Current User Information to All Layers in Ddd
Is a Finally Block Without a Catch Block a Java Anti-Pattern
How to Run Sonarqube Code Analysis for .Net Core (C#) on Linux