Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
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:
Line 26:
Line 27: GridView1.DataSource = dt;
Line 28: GridView1.DataBind();
Line 29: }
Line 30: }
Source File: c:\Users\Ujwal S shetty\OneDrive\Mobile =doc,pic,videos\Desktop\Mini Project\studentmanagement\studentmanagement\ManageStudent.aspx.cs Line: 28
Stack Trace:
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
public partial class Pages_ManageStudent : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["AccessConn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
void BindGrid()
{
using (OleDbConnection conn = new OleDbConnection(connStr))
{
string sql = "SELECT * FROM Students";
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
string name = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string roll = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string sclass = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string contact = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string email = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
string courseId = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
using (OleDbConnection conn = new OleDbConnection(connStr))
{
string sql = "UPDATE Students SET Name=?, RollNo=?, ClassName=?, Contact=?, Email=?, CourseId=? WHERE StudentId=?";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Parameters.AddWithValue("?", name);
cmd.Parameters.AddWithValue("?", roll);
cmd.Parameters.AddWithValue("?", sclass);
cmd.Parameters.AddWithValue("?", contact);
cmd.Parameters.AddWithValue("?", email);
cmd.Parameters.AddWithValue("?", courseId);
cmd.Parameters.AddWithValue("?", id);
conn.Open();
cmd.ExecuteNonQuery();
}
GridView1.EditIndex = -1;
lblMsg.Text = "Student updated successfully!";
BindGrid();
}
protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
using (OleDbConnection conn = new OleDbConnection(connStr))
{
string sql = "DELETE FROM Students WHERE StudentId=?";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Parameters.AddWithValue("?", id);
conn.Open();
cmd.ExecuteNonQuery();
}
lblMsg.Text = "Student deleted successfully!";
BindGrid();
}
protected void btnHome_Click(object sender, EventArgs e)
{
Response.Redirect("Home.aspx");
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ManageStudent.aspx.cs" Inherits="Pages_ManageStudent" %>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
text-align: center;
}
h2 {
color: #333;
margin-top: 20px;
}
.grid-container {
margin: 20px auto;
width: 90%;
}
.message {
color: green;
font-weight: bold;
margin: 10px;
}
Tuhin PaulPosted Sep 26, 2025, 3:31 PM
In your .aspx file, you have:
And in your code-behind (
ManageStudent.aspx.cs), insideBindGrid():This is a conflict. The framework sees that you’ve already told it to use
AccessDataSource1viaDataSourceID, but then you manually assign aDataSourcein code — which is not allowed.Use Code-Behind Only (Recommended for your case)
Since you're already writing custom logic for editing, updating, and deleting (and using
OleDb), removeDataSourceIDfrom the GridView in your.aspxfile.Change this:
To this:
Keep your code-behind exactly as is — including:
You're already manually handling data operations (update/delete) with
OleDbCommand. UsingAccessDataSourcewould require you to configureUpdateCommand,DeleteCommand, etc., on it — which you haven’t done. So sticking with code-behind gives you full control.Jayraj ChhayaPosted Sep 26, 2025, 11:54 AM
The error is clear: you are binding the GridView twice → once with
DataSourceID="AccessDataSource1"in your.aspxmarkup, and again in code-behind withGridView1.DataSource = dt; GridView1.DataBind();.Fix:
Option 1 (use DataSourceID only)
Remove the code-behind binding:
Keep
DataSourceID="AccessDataSource1"in.aspx.Option 2 (use code-behind only)
Remove
DataSourceID="AccessDataSource1"from the GridView in.aspx.Example:
Choose one approach only (DataSourceID or DataSource in code).