Insert using a submit button in ASP.NET doesn't work? - c#

Hello I have a bit of ASP.Net Code below. I am trying to figure out how to make my submit button:
<asp:Button ID="makepost" runat="server" Text="Post My Tip/Story"
ValidationGroup="createpost" />
work properly. When I try to add AccessDataSource1.Insert() I get an error saying this:
CS1061: 'ASP.main_aspx' does not contain a definition for
'AccessDataSource1' and no extension method 'AccessDataSource1'
accepting a first argument of type 'ASP.main_aspx' could be found (are
you missing a using directive or an assembly reference?)
What do I need to type to make this work? All of my code is below.
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<asp:LoginStatus ID="LoginStatus1" runat="server" />
<div style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #FFFFFF">
<p style="font-size: 20px">Welcome Back!<br />
Begin Sharing and Reading Space Tips Below!
</p>
<p> Current Posts:
<asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" DataKeyNames="ID">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:BoundField DataField="User" HeaderText="User" SortExpression="User" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Timeof" HeaderText="Timeof"
SortExpression="Timeof" />
<asp:BoundField DataField="Post" HeaderText="Post" SortExpression="Post" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Posts.mdb"
SelectCommand="SELECT * FROM [UserPost] ORDER BY [Timeof]"
DeleteCommand="DELETE FROM [UserPost] WHERE [ID] = ?"
InsertCommand="INSERT INTO [UserPost] ([ID], [User], [Title], [Timeof], [Post]) VALUES (?, ?, ?, ?, ?)"
UpdateCommand="UPDATE [UserPost] SET [User] = ?, [Title] = ?, [Timeof] = ?, [Post] = ? WHERE [ID] = ?">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="User" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Timeof" Type="DateTime" />
<asp:Parameter Name="Post" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:ControlParameter Name="ID" Type="Int32" ControlID="TextBox5" PropertyName="Text" />
<asp:ControlParameter Name="User" Type="String" ControlID="TextBox1" PropertyName="Text" />
<asp:ControlParameter Name="Title" Type="String" ControlID="TextBox2" PropertyName="Text" />
<asp:ControlParameter Name="Timeof" Type="DateTime" ControlID="TextBox3" PropertyName="Text" ConvertEmptyStringToNull="true"/>
<asp:ControlParameter Name="Post" Type="String" ControlID="TextBox4" PropertyName="Text" />
</InsertParameters>
</asp:AccessDataSource>
</p>
<table><tr><td> <asp:Label ID="Label5" runat="server" Text="ID: "></asp:Label></td><td>
<asp:TextBox ID="TextBox5" runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
<tr><td> <asp:Label ID="Label1" runat="server" Text="User: "></asp:Label></td><td>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
<tr><td><asp:Label ID="Label2" runat="server" Text="Title: "></asp:Label></td><td><asp:TextBox ID="TextBox2"
runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
<tr><td><asp:Label ID="Label3" runat="server" Text="Date: "></asp:Label></td><td> <asp:TextBox ID="TextBox3"
runat="server"></asp:TextBox></td></tr>
<tr><td valign="top"><asp:Label ID="Label4" runat="server" Text="Story: "></asp:Label></td><td valign="top">
<asp:TextBox ID="TextBox4" runat="server" Height="129px" Width="474px" TextMode="MultiLine"></asp:TextBox></td></tr>
</table>
<asp:Button ID="makepost" runat="server" Text="Post My Tip/Story"
ValidationGroup="createpost" /><br />
</div>
</LoggedInTemplate><AnonymousTemplate>
<asp:Login ID="Login1" runat="server" ForeColor="White">
</asp:Login><br /><div style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; color:#FFFFFF">Please Register to be begin viewing and sharing your
<br />Star Wars Old Republic Tips and Stories!</div>
</AnonymousTemplate>
</asp:LoginView>
</asp:Content>

Your AccessDataSource is inside LoginView, you have to find the control:
Dim AccessDataSource1 As AccessDataSource = DirectCast(LoginView1.FindControl("AccessDataSource1"), AccessDataSource)
Regards.
in C# it would be this then
AccessDataSource AccessDataSource1 = (AccessDataSource)LoginView1.FindControl("AccessDataSource1");

'ASP.main_aspx' does not contain a definition for 'AccessDataSource1'
I think that your AccessDataSource control is located in the wrong place: you've put it in the LoginView. The error message says that the runtime can't find the control in the root of your page. So, try to put the AccessDataSource outside the LoginView.

This line. Take a look at:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
Do you see AccessDataSource1? Are you perhaps pointing to the incorrect Datasource? Or, did you name it differently and not realize it?

Related

DetailsView Error: "Must declare the scalar value..."

I am attempting to use DetailsView in ASP.NET and c# to show a list of students that are associated with the logged-in user (which will be a teacher after they login to the system). I have been trying to using scalar values so my program can be more dynamic once a teacher types in the last name of the student they want to find in the system. I'm not sure why I am getting the error
must declare the scalar value
when I (think I) am declaring it on the ASP side of my code. I've been searching for solutions to this for a couple of hours now so any and all help would be greatly appreciated.
ASP.NET Code:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Table runat="server">
<asp:TableRow>
<asp:TableCell>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="lblUserBlank" runat="server" Text="joeB" ></asp:Label>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btSearch" runat="server" OnClick="btSearch_Click" Text="Search" />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="LastName"
DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="StudentID" HeaderText="StudentID" ReadOnly="True" SortExpression="StudentID" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:BoundField DataField="TShirt_Size" HeaderText="TShirt_Size" SortExpression="TShirt_Size" />
</Fields>
<EmptyDataTemplate>
No Student found
</EmptyDataTemplate>
</asp:DetailsView>
<asp:SqlDataSource
runat="server"
ID="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:Lab3 %>"
SelectCommand="SELECT Student.StudentID, Student.LastName, Student.FirstName, Student.Age, Student.TShirt_Size FROM Student INNER JOIN Teacher ON Student.TeacherID = Teacher.TeacherID WHERE Teacher.Username = #Username AND Student.LastName = #LastName">
<SelectParameters>
<asp:ControlParameter ControlID="txtSearch" Name="#LastName" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="lblUserBlank" Name="#Username" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Content>
c# code behind:
protected void btSearch_Click(object sender, EventArgs e)
{
DetailsView1.DataBind();
}
Change this line:
<asp:ControlParameter ControlID="txtSearch" Name="#LastName" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="lblUserBlank" Name="#Username" PropertyName="Text" Type="String" />
for this (remove the # char from the Name flag value):
<asp:ControlParameter ControlID="txtSearch" Name="LastName" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="lblUserBlank" Name="Username" PropertyName="Text" Type="String" />

How can i rotate gridview on page?

I want to rotate gridview in Web Form to print it clear because it doesn't fit in portrait orientation of the page. Gridview is going after page-break-before:always;
But when I rotate it, it cuts 50% of the grid for some strange reasons.
How can I rotate grid and make it fit 100% on the page ?
I want to have something like this :
But i have this situation
My CSS to rotate :
.gridCss {
width: 100%;
-ms-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-webkit-transform: rotate(90deg);
-o-transform: rotate(90deg);
font-size:8px;
word-wrap: break-word;
height:100%;
}
My code:
<div class="gridCss" style="page-break-before:always;" >
<br />
<br />
<center CssClass="rotateText" > <asp:Label ID="MainLbl" runat="server" /> и</center> <br />
<center CssClass="rotateText" ><asp:Label ID="Lab1" runat="server" Text=""></asp:Label></center> <br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="DSI" Width="100%" >
<Columns>
<asp:BoundField DataField="one" HeaderText="" SortExpression="1" HtmlEncode="True" />
<asp:BoundField DataField="fio" HeaderText="two" SortExpression="2" />
<asp:BoundField DataField="3" HeaderText="" SortExpression="3" />
<asp:BoundField DataField="4" HeaderText="К4" SortExpression="4" />
<asp:BoundField DataField="Date" HeaderText="5" SortExpression="Date" />
...
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="DSI" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectSta %>" SelectCommand="Command" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter Name="Sess" SessionField="Sess" Type="String" />
<asp:Parameter DefaultValue="1" Name="st" Type="Int32" />
<asp:Parameter DefaultValue="1" Name="nr" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<br />
<br />
<tr>
<td class="abz"/>
<td align="right">
<pre>
_______
<asp:Label ID="Label_gv_2" runat="server" Text="_____"></asp:Label>
_______ __________________ Дата:___
<asp:Label ID="Label_dt_2" runat="server" Text="_____"></asp:Label>
________ (ФИО) (подпись)<br />
</pre>
</div>
I used DevExpress Data Grid that supports export and printing with extended options, including document page orientation. If you do not need the grid, you can use their reporting tool to print tabled data.

How to handle "Violation of UNIQUE KEY constraint" exception in asp.net web application

<AlternatingRowStyle BackColor="#F7F7F7" HorizontalAlign="Justify" Wrap="False" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ButtonType="Image" />
<asp:CommandField ShowEditButton ="True" ButtonType="Image" />
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="Email">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Occupation" SortExpression="Occupation">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Occupation") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Occupation") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Role" SortExpression="Role">
<EditItemTemplate>
<asp:DropdownList ID="Roles" runat="server" Text='<%# Bind("Roles") %>'>
<asp:ListItem Selected="True" Text="User" value="User"></asp:ListItem>
<asp:ListItem Selected="False" Text="Admin" value="Admin"></asp:ListItem>
</asp:DropdownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Roles") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" SortExpression="Address">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile" SortExpression="Mobile">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle HorizontalAlign="Center" Wrap="False" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:UsTravelConnectionString %>"
DeleteCommand="DELETE FROM [Users] WHERE [Id] = #Id"
InsertCommand="INSERT INTO [Users] ([Name], [Email], [Occupation],[Roles], [Address], [Mobile]) VALUES (#Name, #Email, #Occupation,#Roles, #Address, #Mobile)"
SelectCommand="SELECT [Name], [Email], [Occupation],[Roles], [Address], [Mobile], [Id] FROM [Users]"
UpdateCommand="UPDATE [Users] SET [Name] = #Name, [Email] = #Email, [Occupation] = #Occupation, [Roles]=#Roles, [Address] = #Address, [Mobile] = #Mobile WHERE [Id] = #Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Occupation" Type="String" />
<asp:Parameter Name="Roles" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="Mobile" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Occupation" Type="String" />
<asp:Parameter Name="Roles" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="Mobile" Type="String" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<p runat="server" id="noRowsMsg" sytle="display:none"></p>
</form>
Email field has unique key constraint in tables. I am getting difficulty in handling the exception and outputting an user friendly message that informs to update a new EmailID while updating email.
one possible way is to introduce validation between the "Insert Click" and the actual DB insert action.
you can do this by adding an Inserting event handler to the Sql Data Source.
OnInserting="SqlDataSource_Inserting"
Once you have done that, define the handler as:
protected void SqlDataSource_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
var emailToBeInserter = e.Command.Parameters["#Email"].Value;
// do a DB lookup call and validate if it is unique.
// if not unique, cancel the Insert and display an error message.
e.Cancel = true; // if email already exists in DB.
// if unique, do nothing.
}
Doing explicit validation has the cost of additional DB calls, but is normally a better alternative than letting the "INSERT" logic run through and throw "Unique key" and other such exceptions.

ASP.NET Delete/Insert/Update issue but display works? Used wizard

I utilized the wizard to create Insert Edit Update and Delete statements. Problem is they are not work in my grid view. When I go to use them I get an error:
No value given for one or more required parameters.
I was wondering what could be the problem? It queries and displays data ok.
<LoggedInTemplate>
<asp:LoginStatus ID="LoginStatus1" runat="server" />
<div style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #FFFFFF">
<p style="font-size: 20px">Welcome Back!<br />
Begin Sharing and Reading Space Tips Below!
</p>
<p> Current Posts:
<asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" DataKeyNames="Title">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title"
ReadOnly="True" />
<asp:BoundField DataField="User" HeaderText="User" SortExpression="User" />
<asp:BoundField DataField="Timeof" HeaderText="Date"
SortExpression="Timeof" />
<asp:BoundField DataField="Post" HeaderText="Post" SortExpression="Post" />
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Posts.mdb"
SelectCommand="SELECT * FROM [UserPost] ORDER BY [Timeof]"
DeleteCommand="DELETE FROM [UserPost] WHERE (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL))"
InsertCommand="INSERT INTO [UserPost] ([Title], [User], [Timeof], [Post]) VALUES (?, ?, ?, ?)"
UpdateCommand="UPDATE [UserPost] SET [User] = ?, [Timeof] = ?, [Post] = ? WHERE (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL))">
<DeleteParameters>
<asp:Parameter Name="Title" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="User" Type="String" />
<asp:Parameter Name="Timeof" Type="DateTime" />
<asp:Parameter Name="Post" Type="String" />
<asp:Parameter Name="Title" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="User" Type="String" />
<asp:Parameter Name="Timeof" Type="DateTime" />
<asp:Parameter Name="Post" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
</p>
<table>
<tr><td> <asp:Label ID="Label1" runat="server" Text="User: "></asp:Label></td><td>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
<tr><td><asp:Label ID="Label2" runat="server" Text="Title: "></asp:Label></td><td><asp:TextBox ID="TextBox2"
runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
<tr><td><asp:Label ID="Label3" runat="server" Text="Date: "></asp:Label></td><td> <asp:TextBox ID="TextBox3"
runat="server"></asp:TextBox></td></tr>
<tr><td valign="top"><asp:Label ID="Label4" runat="server" Text="Story: "></asp:Label></td><td valign="top">
<asp:TextBox ID="TextBox4" runat="server" Height="129px" Width="474px"
TextMode="MultiLine"></asp:TextBox></td></tr>
</table>
<asp:Button ID="makepost" runat="server" Text="Post My Tip/Story"
ValidationGroup="createpost" onclick="makepost_Click" /><br />
</div>
</LoggedInTemplate><AnonymousTemplate>
<asp:Login ID="Login1" runat="server" ForeColor="White">
</asp:Login><br /><div style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; color:#FFFFFF">Please Register to be begin viewing and sharing your
<br />Star Wars Old Republic Tips and Stories!</div>
</AnonymousTemplate>
</asp:LoginView>
I'm not very good with access now, but as I can see (and tested) in DeleteCommand you specify 2 parameters but just declare 1, this is not like SQL Server where you name the parameters and you can use one parameter a lot of time in the query, tha same occures in UpdateCommand but with more parameters... see in Where statement.
Try to do something like this:
DeleteCommand="DELETE FROM UserPost WHERE (Title = ?)"
UpdateCommand="UPDATE UserPost SET [User] = ?, Timeof = ?, Post = ? WHERE (Title = ?)"
I tested and it worked.
Edit: You most have an ID (unique primary key) in your table to work with it and not with the Title field.

No value given for one or more required parameters ERROR

I'm aware that this question has been asked before. I have looked through them in hopes to find a reason I'm having issues.
I keep getting this error whenever I try to delete a user from a table within my form. I can edit them, I just can't delete them. I have done research to try and figure it out with no luck.
html code:
<div align="center">
<asp:Label ID="Label1" runat="server" Text="Manage Users"></asp:Label>
<p>
<asp:Label ID="Label2" runat="server" Text="User Name:"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label3" runat="server" Text="Password:"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label4" runat="server" Text="Security Level:"></asp:Label>
<asp:DropDownList ID="drpdwnlstSecurityLevel" runat="server"
onselectedindexchanged="drpdwnlstSecurityLevel_SelectedIndexChanged">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>U</asp:ListItem>
</asp:DropDownList>
</p>
<p>
<asp:Button ID="btnAddUser" runat="server" onclick="btnAddUser_Click1"
Text="Add User" />
</p>
<p>
<asp:Label ID="lblError" runat="server"></asp:Label>
</p>
</div>
<p>
</p>
<div align="center">
<asp:GridView ID="tblUserLogin" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False"
SortExpression="UserID"></asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName"></asp:BoundField>
<asp:BoundField DataField="UserPassword" HeaderText="UserPassword"
SortExpression="UserPassword"></asp:BoundField>
<asp:BoundField DataField="SecurityLevel" HeaderText="SecurityLevel"
SortExpression="SecurityLevel"></asp:BoundField>
<asp:CommandField ShowEditButton="True"></asp:CommandField>
<asp:CommandField ShowDeleteButton="True"></asp:CommandField>
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/PayrollSystem_DB.mdb"
SelectCommand="SELECT [UserID], [UserName], [UserPassword], [SecurityLevel] FROM [tblUserLogin]"
DeleteCommand="DELETE FROM [tblUserLogin] WHERE [UserID] = ?"
InsertCommand="INSERT INTO [tblUserLogin] ([UserID], [UserName], [UserPassword], [SecurityLevel]) VALUES (?, ?, ?, ?)"
UpdateCommand="UPDATE [tblUserLogin] SET [UserName] = ?, [UserPassword] = ?, [SecurityLevel] = ? WHERE [UserID] = ?">
<DeleteParameters>
<asp:Parameter Name="UserID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="UserPassword" Type="String" />
<asp:Parameter Name="SecurityLevel" Type="String" />
<asp:Parameter Name="UserID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="UserID" Type="Int32" />
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="UserPassword" Type="String" />
<asp:Parameter Name="SecurityLevel" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
</form>
</body>
You need to set the DataKeyNames property of the gridview:
datakeynames="UserID"
According to the MSDN documentation:
"You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work. The values of these key fields are passed to the data source control in order to specify the row to update or delete."

Categories

Resources