Questions tagged [datareader]

A DataReader reads a forward-only stream of rows from a data source.

A DataReader reads a forward-only stream of rows from a data source. This tag is for questions about creating or using a datareader in code.

747 questions
123
votes
9 answers

How can I easily convert DataReader to List?

I have data in a DataReader which I want to be converted to a List. What is a possible simple solution for this? For e.g. in CustomerEntity class, I have CustomerId and CustomerName properties.If my DataReader returns these two columns as data,…
Laxman
  • 1,291
  • 2
  • 10
  • 11
52
votes
3 answers

Multiples Table in DataReader

I normally use DataSet because It is very flexible. Recently I am assigned code optimization task , To reduce hits to the database I am changing two queries in a procedure. one Query returns the count and the other returns the actual data. That is…
muhammad kashif
  • 2,466
  • 3
  • 22
  • 47
41
votes
13 answers

Handle DBNull in C#

Is there a better/cleaner way to do this? int stockvalue = 0; if (!Convert.IsDBNull(reader["StockValue"])) stockvalue = (int)reader["StockValue"];
Andreas
  • 451
  • 1
  • 4
  • 3
34
votes
8 answers

Reading a date using DataReader

I read a string using this format with a data reader. How can I read in a date using similar format? while (MyReader.Read()) { TextBox1.Text = (string)MyReader["Note"]; }
TeaDrinkingGeek
  • 1,837
  • 5
  • 30
  • 51
32
votes
10 answers

Convert rows from a data reader into typed results

I'm using a third party library which returns a data reader. I would like a simple way and as generic as possible to convert it into a List of objects. For example, say I have a class 'Employee' with 2 properties EmployeeId and Name, I would like…
Anthony
  • 6,880
  • 12
  • 57
  • 70
24
votes
2 answers

How to get a DataRow out the current row of a DataReader?

Ok, I would like to extract a DataRow out a DataReader. I have been looking around for quite some time and it doesn't look like there is a simple way to do this. I understand a DataReader is more a collection of rows but it only read one row at the…
Rémi
  • 3,406
  • 5
  • 23
  • 43
22
votes
14 answers

.NET DataTable skips rows on Load(DataReader)

I'm trying to populate a DataTable, to build a LocalReport, using the following: MySqlCommand cmd = new MySqlCommand(); cmd.Connection = new MySqlConnection(Properties.Settings.Default.dbConnectionString); cmd.CommandType =…
Tom
  • 6,301
  • 12
  • 52
  • 75
21
votes
3 answers

How to print all columns in a datareader

Using c# how do I print all columns in a datareader.
svon
  • 315
  • 1
  • 6
  • 14
20
votes
9 answers

How to fill Dataset with multiple tables?

I'm trying to fill DataSet which contains 2 tables with one to many relationship. I'm using DataReader to achieve this : public DataSet SelectOne(int id) { DataSet result = new DataSet(); using (DbCommand command =…
Andriy Zakharko
  • 1,531
  • 1
  • 13
  • 36
17
votes
3 answers

Enforce only single row returned from DataReader

I seem to write this quite a lot in my code: using (var reader = cmd.ExecuteReader()) { if (reader.Read()) { result = new User((int)reader["UserId"], reader["UserName"].ToString()); } if (reader.Read()) { throw…
fearofawhackplanet
  • 47,230
  • 49
  • 149
  • 249
17
votes
6 answers

DataReader.GetFieldType returned null

In my db table Layout, there's one column whose type is hierarchyid (column index=4). When trying to set-up new environment (a virtual web-server, created from XEN server), then running the site, I've met with this issue: Exception message:…
Undefined Identity
  • 395
  • 3
  • 5
  • 13
16
votes
3 answers

I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object

Here is my sample code that I am using to fetch data from database: on DAO layer: public IEnumerable GetDATA(ICommonSearchCriteriaDto commonSearchCriteriaDto) { using(DbContext) { DbDataReader reader =…
Sanjeev Rai
  • 6,019
  • 4
  • 20
  • 33
15
votes
6 answers

DataReader - hardcode ordinals?

When returning data from a DataReader I would typically use the ordinal reference on the DataReader to grab the relevant column: if (dr.HasRows) Console.WriteLine(dr[0].ToString()); or if (dr.HasRows) …
David Neale
  • 15,240
  • 5
  • 54
  • 82
15
votes
1 answer

DataReader: Specified cast is not valid (Int32)

Why does SqlDataReader throw an exception when converting 0 to integer? ?dataReader(3) 0 {Short} Short: 0 ?dataReader.GetInt16(3) 0 ?dataReader.GetInt32(3) {"Specified cast is not valid."} _HResult: -2147467262 _message: "Specified cast…
moldovanu
  • 702
  • 2
  • 8
  • 20
13
votes
1 answer

How to handle multiple ResultSets, each with multiple Rows? IDataReader.NextResult() ending Read()

How to handle multiple ResultSets, each with multiple Rows? The call to NextResult() breaks the while loop. Some of my SPs return multiple ResultSets. I'm handling these with NextResult() but when I do and my SP only has a single ResultSet, I see…
learnerplates
  • 3,769
  • 5
  • 23
  • 39
1
2 3
49 50