Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi guys,
I am new to C#. I am trying to fill a datareader using a function with return type as 'SqlDataReader' but unfortunately its giving some error. Any advice would be really appreciated.
Thanks,
Akky


C#
public static void Main(string[] args)
       {

           SqlDataReader dr = Program.hello("select top 10 firstName,lastName from DimCustomer");
           while (dr.Read())
           {
               Console.WriteLine(dr.GetValue(0));
           }
           Console.ReadLine();

       }
       public static SqlDataReader hello(string query)
       {
           SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=adventure_works;Integrated Security=true;");
           SqlCommand cmd = new SqlCommand(query, con);
           con.Open();
           SqlDataReader dr = cmd.ExecuteReader();
           con.Close();
           return dr;
       }
Posted
Comments
Abhinav S 4-Feb-14 13:51pm    
What error?
King Fisher 4-Feb-14 23:45pm    
Error?

Just remove one line from code that is con.Close() because SqlDataReader works in connected mode and it read forward only mode.

C#
using System;
using System.Data.SqlClient;

namespace RestApp
{
    class Program
    {
        public static void Main(string[] args)
        {

            SqlDataReader dr = Program.hello("select top 10 firstName,lastName from DimCustomer");
            while (dr.Read())
            {
                Console.WriteLine(dr.GetValue(0));
            }
            Console.ReadLine();

        }
        public static SqlDataReader hello(string query)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=adventure_works;Integrated Security=true;");
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            con.Close();
            return dr;
        }
    }
    
  
}
 
Share this answer
 
v2
C#
class Program
{
    static SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=adventure_works;Integrated Security=true;");
    public static void Main(string[] args)
    {

        SqlDataReader dr = Program.hello("select top 10 firstName,lastName from DimCustomer");
        while (dr.Read())
        {
            Console.WriteLine(dr.GetValue(0));
        }
        con.Close();
        Console.ReadLine();

    }
    public static SqlDataReader hello(string query)
    {
        
        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader(); 
  con.Close();
        return dr;

    }
}
 
Share this answer
 
You should add some code to process rows as follows
C#
while (reader.Read())
{
 
      // Do something with rows
     for( int i = 0; i < reader.FieldCount; i++ ){
     }
//... or
     string field = (string)reader["column1"];
}


Please refer these links for details

SqlCommand.ExecuteReader Method[^]

SqlDataReader.Read[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900