Wael1988 0 Newbie Poster

hello i have an error while am trying to retrieve data from a tb in data base using Stored Procedure
any help please?
this is my web.config

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionStrings>
    <clear/>
    <add
name="myConnectionString"
connectionString=" Data Source=.; DataBase= TestDB; Integrated Security = true"
providerName="System.Data.SqlClient"></add>


  </connectionStrings>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

</configuration>

and this is my page that containes the GridView

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

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Employers_GetList";
            con.Open();
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();

                ad.Fill(ds);


                GridView1.DataSource = ds;
                GridView1.DataBind();
                con.Close();

        }
    }
}

and here you are the error msg

Server Error in '/' Application.
Fill: SelectCommand.Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.

Source Error:

Line 23: SqlDataAdapter ad = new SqlDataAdapter(cmd);
Line 24: DataSet ds = new DataSet();
Line 25: ad.Fill(ds);
Line 26: GridView1.DataSource = ds;
Line 27: GridView1.DataBind();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.