Solved

ExecuteReader: Connection property has not been initialized. asp.net c#

Posted on 2014-11-11
3
901 Views
Last Modified: 2014-11-11
I am getting: "ExecuteReader: Connection property has not been initialized." at line 19 (SqlDataReader rdr = cmd.ExecuteReader();).

Question: How can I handle this error?

This is a asp tutorial I am duplicating.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string cs = ConfigurationManager.ConnectionStrings["StudentDB"].ConnectionString;
        SqlConnection con = new SqlConnection(cs);
        SqlCommand cmd = new SqlCommand("Select ID, FirstName, LastName, Gender FROM Students WHERE Gender='Male'");
        List<Student> listStudents = new List<Student>();
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            Student student = new Student();
            student.ID = Convert.ToInt32(rdr["ID"]);
            student.FirstName = rdr["FirstName"].ToString();
            student.LastName = rdr["LastName"].ToString();
            student.Gender = rdr["Gender"].ToString();

            listStudents.Add(student);
        }
        con.Close();
        GridView1.DataSource = listStudents;
        GridView1.DataBind();

    }
}
-------------------------
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True
  Data Source=.\SQLEXPRESS12;Initial Catalog=MME_Sample; Integrated Security=True
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>
    <add name="StudentDB" connectionString="Data Source=.\SQLEXPRESS12;Initial Catalog=MME_Sample; Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
</configuration>

Open in new window

0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 40435225
change line 18 for:
SqlCommand cmd = new SqlCommand("Select ID, FirstName, LastName, Gender FROM Students WHERE Gender='Male'", con);

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40435227
I meant line 16, and be sure to move line 18 before 16 (open the connection before creating the command)
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40436149
Eric,

FYI "be sure to move line 18 before 16" is not necessary.

Mike
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question