-3

'System.Data.SqlClient.SqlDataReader' has no constructors defined, Please check the code I can't fixed this problem

When I am run this code I am faceing thisproblem in SqlDataReader dr = new SqlDataReader(); The type 'System.Data.SqlClient.SqlDataReader' has no constructors defined, Please check the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace PhotoGellaryy.Models
{
    public class PhotoGellary
    {

        public string strcn = 
ConfigurationManager.ConnectionStrings["UploadImagesEntities"].ToString();

        public IList<UploadImagesViewModel>GetImages()
        {
            SqlConnection con = new SqlConnection(strcn);
            List<UploadImagesViewModel> photogellary = new 
List<UploadImagesViewModel>();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select ImageID,ImageName from 
ImageUploadTbl";
                con.Open();
                SqlDataReader dr = new SqlDataReader();
                while(dr.Read())
                {
                    UploadImagesViewModel objuploadimagesviewmodel = new 
UploadImagesViewModel();
                     //objuploadimagesviewmodel.ImageID = 
dr["ImageID"].ToString();
                     objuploadimagesviewmodel.ImageName = 
dr["ImageName"].ToString();
                    photogellary.Add(objuploadimagesviewmodel);
                }
                if(dr!= null)
                {


                    dr.Dispose();
                    dr.Close();
                }
                con.Close();
                return photogellary.ToList();
            }

        }


    }
}
4
  • 2
    Please consider pasting your code here instead of the image of the code!!! Thanks!! Commented Jul 19, 2018 at 4:21
  • SqlDataReader: "To create a SqlDataReader, you must call the ExecuteReader method of the SqlCommand object, instead of directly using a constructor." Commented Jul 19, 2018 at 6:02
  • 1
    Unrelated tips: SqlConnection, SqlCommand and SqlDataReader are all IDisposable and should each be in using blocks, then you won't need either of the Closes or the Dispose because the implicit Dispose will handle that for you. Commented Jul 19, 2018 at 6:03
  • Possible duplicate of The type 'System.Data.SqlClient.SqlDataReader' has no constructors defined Commented Jul 20, 2018 at 5:11

1 Answer 1

0

Replace the line with:

SqlDataReader dr = cmd.ExecuteReader();

And why we are not allowed to create an object of SqlDataReader for that have a look at Answer

Sign up to request clarification or add additional context in comments.

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.