Home > DeveloperSection > Forums > The SelectCommand property has not been initialized before calling “Fill
Babe Zaharias
Babe Zaharias

Total Post:19

Points:133
Posted on    June-19-2013 2:52 AM

 ADO.Net ADO.Net 
Ratings:


 1 Reply(s)
 1110  View(s)
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];


Dev Patel

Total Post:26

Points:182
Posted on    June-19-2013 8:20 AM

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. 

Don't want to miss updates? Please click the below button!

Follow MindStick