Click here to Skip to main content
15,923,087 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (!IsPostBack)
        {
            BindData();
        }
.........
.........
void BindData()
    {
        SqlCommand cmd1 = new SqlCommand("select * from cntus",con);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.Fill(ds, "cntus");   //cntus is my table name
        GridView1.DataSource = ds.Tables["cntus"];
        GridView1.DataBind();  //gridview1 is my gridview id
    }

this is my code,but in 4th line da.Fill(ds,"cntus") it gives an error that The SelectCommand property has not been initialized before calling 'Fill'

How can i rectify my problem? Any body please help me
Posted
Updated 11-Nov-14 21:54pm
v2

Add command object while initializing the SqlDataAdapter.

Try this
C#
SqlCommand cmd1 = new SqlCommand("select * from cntus", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd1);
            DataSet ds = new DataSet();
 
Share this answer
 
Comments
Shivaram_i 12-Nov-14 5:50am    
Thank you...I got it now...
Karthik_Mahalingam 12-Nov-14 6:22am    
welcome :)
In the above code you have not specified the command object for the SqlDataAdapter .
Also make sure you assign a valid connection to the command object.

Sample below ;

da.SelectCommand=cmd1


Please refer below for details

http://msdn.microsoft.com/en-us/library/bh8kx08z%28v=vs.110%29.aspx[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900