user383664
user383664

Reputation: 107

gridview databinding problem

i am using asp.net with c#

and i want to bind my data to the gridview

i have success fully done that by wizard

but now it is show me this error


protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataAdapter dbadapter = null;
        DataSet dbdata = new DataSet();
        using (SqlConnection dbconn = new SqlConnection("Server=CJ\\SQLEXPRESS;Database=elligiblity;User ID=sa;Password=123;Trusted_Connection=False;"))
        {
        dbadapter = new SqlDataAdapter("select * from Main_Table", dbconn);
        dbadapter.Fill(dbdata);

        }
        GridView1.DataSource = dbdata;
        GridView1.DataBind();

    }

 

i am getting this error Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition

Upvotes: 0

Views: 2741

Answers (2)

Zo Has
Zo Has

Reputation: 13038

You cannot specify custom binding from code if gridview is already binded to a datasource. Either remove DataSourceID in gridview properties from your aspx page or change the select command of the sql datasource to which it is binded from code like,

protected void Button1_Click(object sender, EventArgs e)
    {
        sdsFtrtDet.SelectParameters.Clear(); // Clear any innitial parameters of your sql datasource which is binded with gridview
        sdsFtrtDet.SelectCommand = "select * from tableA"; //Specify new query
        sdsFtrtDet.DataBind();   // Data Bind
}

Cheers.

Edit

You probably are not seeing your gridview because you would have used template fields binded with datasource ? You can use another gridview to bind with your 2nd query. Leave the 1st gridview as it is so you wont get in trouble with template fields.

Cheers.

Upvotes: 2

Rebecca Chernoff
Rebecca Chernoff

Reputation: 22635

So that means that in your markup, you're specifying a value for DataSourceID, but then in the code you've shown, you're setting DataSource. DataSourceID and DataSource are mutually exclusive.

Upvotes: 2

Related Questions