Click here to Skip to main content
12,819,538 members (34,121 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hello friends,
I have bind the gridview with sqldatasource but whenever is execute my program it show following error:

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

Now how to solve this error????????
Posted 15-Sep-11 2:14am
Updated 8hrs 10mins ago
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I think you try to bind data to data grid using sqldatasource from design side as well as using c# code from code behind side..

My Friend you have to do this for solving your error..

1) You need to chose one way to bind the grid
2) if it is from code behind means using c# code then remove the datasourceid property from grid view from design view of grid
like this

<asp:gridview id="cartData" runat="server" autogeneratecolumns="False" 

//you have to make it like this

<asp:gridview id="cartData" runat="server" autogeneratecolumns="False">

3) if you use a design side binding to your grid then you have to remove the c# code to bind the grid.

Because you have to use only one method of biding, both at a same time is not permissible..
abhihits 15-Sep-11 7:57am
Hi Tejas,
Thankyou so much for this beautiful explaination. My query is solved........Thanks a lot.........
rajinder raviya 3-Apr-13 14:34pm
thanks a lot it's solve my problem as well as provide information..
Ashok Sharma 9-Jan-14 12:28pm
Thanks Tejas, mine error is also got resolved however gridview isn't visible, any suggestion? I haven't set any visible property.
Here is the code.

Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
Dim sql As String
Dim sda As SqlDataAdapter

Dim connectionString As String = "Data Source=THERAIN\SQLEXPRESS;Initial Catalog=Ashok;User ID=sa;Password=Ashok"
sqlCnn = New SqlConnection(connectionString)
sql = "SELECT * FROM Income WHERE date>='" + TextBox1.Text.ToString + "' and date<='" + TextBox2.Text.ToString + "'"

sda = New SqlDataAdapter(sql, sqlCnn)


Dim ds As New DataSet

Dim commandBuilder As SqlCommandBuilder = New SqlCommandBuilder(sda)

GridView1.DataSource = ds.Tables(0)
Tejas Vaishnav 10-Jan-14 1:54am
Have you checked that, your data set has fill up with data or not? also you are missing a line after assigning data source to your grid you need to call data bind method of grid view to bind assigned data to it. like...

GridView1.Datasource = ds.Tables(0)

Ashok Sharma 21-Jan-14 11:02am
After adding GridView1.DataBind() at the end of the code this got resolved.

Thanks a lot....
Tejas Vaishnav 22-Jan-14 0:19am
its great to here, it resolve your problem...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It requires you to do exactly that. You seem to have set both the DataSourceID and DataSource properties on GridView1. Remove one of them. The DataSourceID tends to be set in declarative markup and the DataSource tends to be set in codebehind. Because you're using a SqlDataSource, remove any lines that explicitly set the DataSource property in codebehind.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Solution is Here:

public partial class _Default : System.Web.UI.Page 
    SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
    protected void Page_Load(object sender, EventArgs e)
        string str = "select * from student";
        SqlDataAdapter adp = new SqlDataAdapter(str, con);
        DataSet set1 = new DataSet();
        GridView1.DataSource = set1.Tables[0];
nilkamal sahare 7-Feb-13 10:41am
When i am inserting data in the grid through link button then why it is entering only null values...please help
Rate this: bad
Please Sign up or sign in to vote.

Solution 7

i think already u used in grid view

<asp:gridview id="cartData" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
_____________here u used id connection

same time u used in C# code page load grid view like

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
GridView1.DataSource = SqlDataSource1; __________ don't use here

then only error came
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

YOur answer is given here. Please have a look on that [^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 5


In Gridview there are two properties for binding data to that data control

we use only one property for binding at a time.

I thing you are using both properties .

let me tell you difference between both properties

If you use SqlDataSource,LinqDataSource thn you've to assign that datasource IDs to DatasourceID

If you want to give datatable,dataset thn assign those to Datasource
Rate this: bad
Please Sign up or sign in to vote.

Solution 9

thanks a lot dear..very very thanks

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 25 Mar 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100