Wednesday, March 7, 2012

Defining a Select Command

I'm trying to populate a DropDownList from my SQL database. I'm using C# 2005 and when I compile my code I get an error.Compiler Error Message:CS0103: The name 'myConnection' does not exist in the current context

using

System;

using

System.Data;

using

System.Data.SqlClient;

using

System.Configuration;

using

System.Collections;

using

System.Web;

using

System.Web.Security;

using

System.Web.UI;

using

System.Web.UI.WebControls;

using

System.Web.UI.WebControls.WebParts;

using

System.Web.UI.HtmlControls;

public

partialclassDefault3 : System.Web.UI.Page

{

privatestring connectionString = WebConfigurationManager.ConnectionStrings["mewconsultingConnectionString"].ConnectionString;protectedvoid Page_Load(object sender,EventArgs e)

{

SqlCommand myCommand =newSqlCommand();

myCommand.Connection = myConnection;

myCommand.CommandText =

"SELECT * FROM tblPau";

myConnection.Open();

SqlDataReader myReader;

myReader = myCommand.ExecuteReader();

myReader.Read();

// The first row in the result set is now available.

lstPau.Items.Add(myReader[

"PauSiteName"]);

myReader.Close();

myConnection.Close();

}

}

Exactly as the error stated: in your code, you did not delcare myConnection.

You need something like this before you reference it:

SqlConnection

myConnection =newSqlConnection(connectionString);

No comments:

Post a Comment