Solved

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

Posted on 2014-11-11
3
766 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
  • 2
3 Comments
 
LVL 69

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 69

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 33

Author Comment

by:Mike Eghtebas
ID: 40436149
Eric,

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

Mike
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now