I am trying to connect to the SQL Server from Visual Web Developer using asp.net but I am facing some problems If anybody helps in this regard i will be greatful.
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Server=localhost;" + "Database=DB;User ID=aaaa;" + "Password=aaaa");
conn.Open(); SqlDataReader reader = conn.ExecuteReader();
while (reader.Read()) {
employeesLabel.Text += reader["Name"] + "<br />";
}
reader.Close();
conn.Close();
}
}
I am getting errors saying
'System.Data.SqlClient.SqlConnection' does not contain a definition for 'ExecuteReader' and no extension method 'ExecuteReader' accepting a first argument of type 'System.Data.SqlClient.SqlConnection' could be found (are you missing a using directive or an assembly reference?)
The name 'employeesLabel' does not exist in the current context.
Can anybody tell the possible reason?
You are not passing any command/query to ExecuteReader something like this would be correct:
SqlConnection doesn't have any ExecuteReader() method. You have to make object of SqlCommand. Your code should like this:
Oops! Only IDbCommand implementations have ExecuteReader returning the whole DataReader:
i think you have to create object of SqlCommand class also and pass the command string to its constructor. try this"
Try