Click here to Skip to main content
12,300,021 members (50,646 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 1:14am
Rate this: bad
 
good
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" 
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..
  Permalink  
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....
Tejas Vaishnav 22-Jan-14 0:19am
   
its great to here, it resolve your problem...
Rate this: bad
 
good
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.
  Permalink  
Rate this: bad
 
good
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">
DataSourceID="Datasource1">
_____________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();
da.Fill(ds);
GridView1.DataSource = SqlDataSource1; __________ don't use here
GridView1.DataBind();

then only error came
  Permalink  
v2
Rate this: bad
 
good
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();
        adp.Fill(set1);
        GridView1.DataSource = set1.Tables[0];
        GridView1.DataBind();
}
}
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

Solution 5

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
  Permalink  

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
Web01 | 2.8.160525.2 | Last Updated 21 Jan 2015
Copyright © CodeProject, 1999-2016
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