THE SELECTCOMMAND PROPERTY HAS NOT BEEN INITIALIZED BEFORE CALLING “FILL

Babe Zaharias

Total Post:19

Points:133
Posted by  Babe Zaharias
 1588  View(s)
Ratings:
Rate this:
Hi Developers,

The SelectCommand property has not been initialized before calling “Fill

My line of code as following

SqlConnection cn = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\MANTHAN\Documents\Visual Studio 2010\WebSites\sample\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
    SqlCommand cmd = new SqlCommand("SELECT * FROM stu1", cn);
    SqlDataAdapter adp = new SqlDataAdapter(cmd);

    DataSet ds = new DataSet();

    adp.Fill(ds);

    GridView1.DataSource = ds.Tables[0];
  1. Dev Patel

    Post:26

    Points:182
    Re: The SelectCommand property has not been initialized before calling “Fill

    Hi,


    adp.fill(ds); will take care of every thing.
    Remove cmd.ExecuteNonQuery();.

    try as following

    try
    {
        SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\MANTHAN\\Documents\\Visual Studio 2010\\WebSites\\sample\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True");

        SqlCommand cmd = new SqlCommand("SELECT * FROM stu1", cn);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);

        //cmd.Executenonquery(); removed

        DataSet ds = new DataSet();

        adp.Fill(ds);

        GridView1.DataSource = ds.Tables[0];
    }

        catch(exception ex)
        {
          Messagebox.show(ex.Message);
        }

    I hope it helpful for you. 

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!