Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.33/5 (4 votes)
See more:
I am trying to bind SQL DataSource which is available in ASP.Net (inbuilt) with GridView but i cannot complete the task please help me and give step by step solution.
thankyou,
Posted
Updated 22-Sep-17 5:57am

 
Share this answer
 
Comments
abhihits 15-Sep-11 5:07am    
Hi anup,
i am trying to bind sqldata source to gridview in '.cs' file but it show error "Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition." Please give some solution for this:
my coding in cs file is as follow:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

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();
}
}
Anup Das Gupta (asteranup) 19-Sep-11 1:49am    
Hi,
Go to gridview UI. You will see DataSourceID in the html. Remove it.
C#
SqlConnection cn = new SqlConnection("YOUR CONNECTION SCRING");
SqlDataAdapter cmd = new SqlDataAdapter();
DataTable dt = new DataTable("TESTTABLE");
cmd.SelectCommand = new SqlCommand();
cmd.SelectCommand.Connection = cn;
cmd.SelectCommand.CommandText = "YOUR SELECT QUERY";
cmd.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
 
Share this answer
 
Comments
abhihits 15-Sep-11 5:16am    
Thankyou Tejas
I am also doing the same thing but i always getting following error:
"Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition."
Now what does this mean? please provide me solution for above query.
Tejas Vaishnav 15-Sep-11 7:15am    
Please show some code so i can help you....
Tejas Vaishnav 15-Sep-11 7:16am    
Thnax for accepting my answer...
Please Rate it...
Tejas Vaishnav 15-Sep-11 7:33am    
For your above question ans just see your question

http://www.codeproject.com/Answers/255079/Both-DataSource-and-DataSourceID-are-defined-on-Gr#answer3

and my answer
Member 10557214 6-Feb-14 0:24am    
thnx
C#
string selectSQL = "select query";
           SqlConnection cnn = new SqlConnection(connectionString);
           SqlDataAdapter adp = new SqlDataAdapter(selectSQL, cnn);
           DataSet ds = new DataSet();
           adp.Fill(ds);
           GridView1.DataSource = ds;
           GridView1.DataBind();
 
Share this answer
 
Comments
Er. Anil Yadav 18-Feb-15 5:53am    
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ID = "SqlDataSource1";
this.Page.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlDataSource1.SelectCommand = "SELECT top 10 ContactName, City, Country, PostalCode from Customers";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
}
CHill60 18-Feb-15 6:34am    
What is this meant to be?
The post you are replying to is over 3 years old
XML
<asp:SqlDataSource ID="SqlDSVisitorDetails" runat="server" ConnectionString="<%$ ConnectionStrings:collegeConnStr %>"
                        SelectCommand="SELECT college,stuid,bid,vname,vrelat,photo FROM visiterdet">
</asp:SqlDataSource>
 
Share this answer
 
 
Share this answer
 

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