mino
mino

Reputation: 21

Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition

i want to add a record to database and see it on gridview in same page by click on add. for refresh and update gridview i use showData() like this:

 protected void ShowData()
{

    SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Khayati;Integrated Security=True");

    con.Open();
    string qu = string.Format("Select * from Ordertb ", con);
    SqlDataAdapter da = new SqlDataAdapter(qu, con);
    DataSet ds = new DataSet();
    da.Fill(ds, "logtbl");
    if (ds.Tables["logtbl"].Rows.Count > 0)
    {
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
    con.Close();
}

but there is this error:

"Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition. 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: Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition."

and this error is pointing on:

GridView1.DataBind();

i must finish this project in one day plz help me. thanks.

Upvotes: 1

Views: 4539

Answers (2)

Clark Froebe
Clark Froebe

Reputation: 1

Just an FYI for those coming into this problem cold and hitting this thread after searches on the error syntax above:

If it’s reasonably likely that there were no code changes – or if there were, but they should be unrelated to the error - check the TLS level used by the application and ensure that it is supported by the .Net Framework level.

I just helped a client with this exact error and discovered that – despite the verbiage - the issue was due to disablement of protocols below TLS version 1.2 at the OS level.

With numerous organizations attempting to implement Microsoft’s guidance to disable protocols below TLS 1.2, this is likely a common misleading error message.

Upvotes: 0

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

"Both DataSource and DataSourceID are defined on X" often occurs if you set data source for the GridView that has already defined DataSourceID attribute value to certain data source in the ASPX page markup.

To solve this issue, either set DataSourceID property to null at code-behind:

if (ds.Tables["logtbl"].Rows.Count > 0)
{
    GridView1.DataSourceID = null; // string.Empty can also used
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
}

Or remove DataSourceID attribute from GridView page markup:

<%-- Before --%>
<asp:GridView ID="GridView1" runat="server" DataSourceID="ADataSource" ... />

<%-- After --%>
<asp:GridView ID="GridView1" runat="server" ... />

References:

Both DataSource and DataSourceID are defined on ‘GridView1’. Remove one definition.

Both DataSource and DataSourceID are defined on 'GridView2'. Remove one definition

Upvotes: 1

Related Questions