Click here to Skip to main content
15,922,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void Button2_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=ABHINAV-PC\ABHI;Initial Catalog=swapnil;Integrated Security=True");
con.Open();
if (DropDownList1.SelectedIndex == 0)
{
SqlDataAdapter da = new SqlDataAdapter("select * from Biscuits",con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
}
else if(DropDownList1.SelectedIndex == 1)
{
SqlDataAdapter da = new SqlDataAdapter("select * from oil", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
}
when i am running the code it is giving error as
CSS
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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Posted

This happens if you have used any source binding in your .aspx page. Try removing the data source which you have specified in design page and execute.

Do let me know if you still have any issues.

Thanks,
DRS
 
Share this answer
 
Comments
Abhinav Chaudhary 6-Jul-14 7:09am    
Thanks Sir!
Ravi Shankar Dokka 11-Jul-14 5:37am    
No Problem :)
First of all remove
HTML
DataSourceID 
from gridview from ASPX designer page.. :)

C#
protected void Button2_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=ABHINAV-PC\ABHI;Initial Catalog=swapnil;Integrated Security=True");
con.Open();
if (DropDownList1.SelectedIndex == 0)
{

string SelectData="select * from Biscuits";
}
else if(DropDownList1.SelectedIndex == 1)
{
string SelectData="select * from oil";

}
SqlDataAdapter da = new SqlDataAdapter(SelectData,con);
DataTable dt = new DataTable ();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
 
Share this answer
 
Comments
Abhinav Chaudhary 6-Jul-14 7:09am    
Thanks Sir!
Nirav Prabtani 7-Jul-14 0:44am    
welcome.. :)

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