Reputation: 93
I have an access database file with 3 tables - Courses,CoursesTaken and Students.
Data from these files are linked together as shown below
What I am wanting to do is make a delete button if a user wants to delete a row. I attempted the option where you can just right click on gridview and in advanced option you can add the delete,insert, update options. But it throws an error "No value given for one or more required parameters. "
So I decided to just code it in myself and have done the following at the bottom. Everything looks fine to me. It doesn't give me any error message also. It just does nothing. When I click the delete button nothing happens.
So, if anyone has experience with access databases and sql I would appreciate any help. Is it with my parameters now? Or are my SQL statements wrong?
The code in aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="StudentID,CourseID" DataSourceID="AccessDataSource1"
onrowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="StudentID" HeaderText="StudentID" ReadOnly="True"
SortExpression="StudentID" Visible="False" />
<asp:BoundField DataField="CourseID" HeaderText="CourseID" ReadOnly="True"
SortExpression="CourseID" />
<asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" Text="Delete" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/University.accdb"
SelectCommand="SELECT * FROM [CoursesTaken] WHERE ([StudentID] = ?)">
<SelectParameters>
<asp:SessionParameter Name="StudentID" SessionField="StudentID" Type="String" />
</SelectParameters>
</asp:AccessDataSource>
<br />
</asp:Content>
The code in C#:
public partial class ViewCourses : System.Web.UI.Page
{
//SelectedStudent student;
SelectedStudent student = new SelectedStudent();
protected void Page_Load(object sender, EventArgs e)
{
student = new SelectedStudent();
student = student.getStudent();
//lblNameInfo.Text = student.FirstName + " " + student.LastName;
// lblEmailInfo.Text = student.StudentID + "";
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.CompareTo("Delete") == 0)
{
int newIndex = Convert.ToInt32(e.CommandArgument);
string studentEmail = GridView1.Rows[newIndex].Cells[3].Text;
AccessDataSource1.DeleteCommand = "DELETE FROM [CoursesTaken] WHERE [StudentID] = '" + studentEmail + "'";
AccessDataSource1.Delete();
AccessDataSource1.DeleteCommand = "DELETE FROM [Students] WHERE [Email] = '" + studentEmail + "'";
AccessDataSource1.Delete();
AccessDataSource1.DataBind();
}
}
Upvotes: 1
Views: 342
Reputation: 7943
EDIT:
There are four mistakes in your process:
For Access, strings should be in double quote, not single quote.
The command strings you are using are very risky. Should consider parameterised queries instead.
In code, index for the cell containing StudentID should be 0, not 3.
In markup, you sat the StudentID visibility to false: the cell content would be empty. Change its visibility in css.
The following code works for me:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.CompareTo("Delete") == 0)
{
int newIndex = Convert.ToInt32(e.CommandArgument);
string studentEmail = GridView1.Rows[newIndex].Cells[0].Text;
AccessDataSource1.DeleteCommand = "DELETE FROM [CoursesTaken] WHERE [StudentID] = '" + studentEmail + "'";
AccessDataSource1.Delete();
AccessDataSource1.DeleteCommand = "DELETE FROM [Students] WHERE [Email] = '" + studentEmail + "'";
AccessDataSource1.Delete();
AccessDataSource1.DataBind();
}
}
I have change my markup like this:
<asp:BoundField DataField="StudentID" HeaderText="StudentID" ReadOnly="True"
SortExpression="StudentID" ItemStyle-CssClass="hidden" HeaderStyle-CssClass="hidden" />
And added the style to the page head:
<head runat="server">
<title></title>
<style>
.hidden {display: none;}
</style>
</head>
EDIT 2 :
Regarding the error you mentioned in comment- "The record cannot be deleted or changed because table 'CoursesTaken' includes related records". When you have StudentID in the CourseTaken table and you delete the Student from Students table, it lefts orphan records in CourseTaken, which is not allowed. To fix this, you have to modify the relationship (If you already have not done so). Here's how it looks in my db:
Upvotes: 1