Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
2.08/5 (4 votes)
See more:
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
Updated 24-Mar-17 21:34pm

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

C#
<asp:gridview id="cartData" runat="server" autogeneratecolumns="False" 
DataSourceID="Datasource1">


//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..
 
Share this answer
 
v4
Comments
abhihits 15-Sep-11 7:57am    
Hi Tejas,
Thankyou so much for this beautiful explaination. My query is solved........Thanks a lot.........
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)

sqlCnn.Open()

Dim ds As New DataSet

Dim commandBuilder As SqlCommandBuilder = New SqlCommandBuilder(sda)

sda.Fill(ds)
GridView1.DataSource = ds.Tables(0)
sqlCnn.Close()
rajinder raviya 3-Apr-13 14:34pm    
thanks a lot it's solve my problem as well as provide information..
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)
GridView1.DataBind()

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

Thanks a lot....
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.
 
Share this answer
 
Solution is Here:

C#
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();
        adp.Fill(set1);
        GridView1.DataSource = set1.Tables[0];
        GridView1.DataBind();
}
}
 
Share this answer
 
v2
Comments
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
i think already u used in grid view

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


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

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


then only error came
 
Share this answer
 
v2
YOur answer is given here. Please have a look on that [^]
 
Share this answer
 
Hi
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
 
Share this answer
 
thanks a lot dear..very very thanks
 
Share this answer
 
Comments
CHill60 25-Mar-17 15:23pm    
Don't post comments as solutions. Use the "Have a question or comment"

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