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

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.

Related

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.

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

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?

C# - Must declare the scalar variable "#ms_id" - Error

I'm writing an web-app that keeps track of deadlines. With this app you have to be able to update records that are being saved in an SQL DB.
However I'm having some problem with my update in my aspx-file.
<asp:GridView ID="gv_editMilestones" runat="server" DataSourceID="sql_ds_milestones"
CellPadding="4" ForeColor="#333333" GridLines="None" Font-Size="Small"
AutoGenerateColumns="False" DataKeyNames="id" Visible="false"
onrowupdated="gv_editMilestones_RowUpdated"
onrowupdating="gv_editMilestones_RowUpdating"
onrowediting="gv_editMilestones_RowEditing">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"
ReadOnly="True" Visible="false"/>
<asp:BoundField DataField="ms_id" HeaderText="ms_id"
SortExpression="ms_id" ReadOnly="True"/>
<asp:BoundField DataField="ms_description" HeaderText="ms_description"
SortExpression="ms_description"/>
<%-- <asp:BoundField DataField="ms_resp_team" HeaderText="ms_resp_team"
SortExpression="ms_resp_team"/>--%>
<asp:TemplateField HeaderText="ms_resp_team" SortExpression="ms_resp_team">
<ItemTemplate>
<%# Eval("ms_resp_team") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DDL_ms_resp_team" runat="server"
DataSourceID="sql_ds_ms_resp_team" DataTextField="team_name"
DataValueField="id">
<%--SelectedValue='<%# Bind("ms_resp_team") %>'--%>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ms_focal_point" HeaderText="ms_focal_point"
SortExpression="ms_focal_point" />
<asp:BoundField DataField="ms_exp_date" HeaderText="ms_exp_date"
SortExpression="ms_exp_date" DataFormatString="{0:d}"/>
<asp:BoundField DataField="ms_deal" HeaderText="ms_deal"
SortExpression="ms_deal" ReadOnly="True"/>
<asp:CheckBoxField DataField="ms_active" HeaderText="ms_active"
SortExpression="ms_active"/>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#999999" />
</asp:GridView>
<asp:SqlDataSource ID="sql_ds_milestones" runat="server"
ConnectionString="<%$ ConnectionStrings:testServer %>"
SelectCommand="SELECT [id]
,[ms_id]
,[ms_description]
,(SELECT [team_name] FROM [NSBP].[dbo].[tbl_teams] as teams
WHERE milestones.[ms_resp_team] = teams.[id]) as 'ms_resp_team'
,[ms_focal_point]
,[ms_exp_date]
,(SELECT [deal] FROM [NSBP].[dbo].[tbl_deals] as deals
WHERE milestones.[ms_deal] = deals.[id]) as 'ms_deal'
,[ms_active]
FROM [NSBP].[dbo].[tbl_milestones] as milestones"
UpdateCommand="UPDATE [NSBP].[dbo].[tbl_milestones]
SET [ms_description] = #ms_description
,[ms_focal_point] = #ms_focal_point
,[ms_active] = #ms_active
WHERE [ms_id] = #ms_id">
<UpdateParameters>
<asp:Parameter Name="ms_description" Type="String" />
<%-- <asp:Parameter Name="ms_resp_team" Type="String" />--%>
<asp:Parameter Name="ms_focal_point" Type="String" />
<asp:Parameter Name="ms_exp_date" Type="DateTime" />
<asp:Parameter Name="ms_active" Type="Boolean" />
<%-- <asp:Parameter Name="ms_id" Type="String" />--%>
</UpdateParameters>
</asp:SqlDataSource>
You can see my complete GridView-structure + my datasource bound to this GridView.
There is nothing written in my onrowupdating-function in my code-behind file.
Thx in advance
You are using #ms_id in the where clause of your SQL Statement, but the line that sets that <asp:Parameter ... /> is commented out. Try uncommenting:
<%-- <asp:Parameter Name="ms_id" Type="String" /> --%>
and try again
This line;
<%-- <asp:Parameter Name="ms_id" Type="String" />--%>
appears to be commented out and is used in the SQL where clause.

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."

C# 'Must declare the scalar variable "#ID_ZAPISA"' error

I am having problem with the following code. Delete function does not work (throws scalar variable error). Update function does not work ... it does not throw any error, but it simply does not make changes to the database.
If I make exact DetailsView, everything works OK. Where is the problem?
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ServisnaKnjiga.aspx.cs" Inherits="CernaticJurij_Default2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<p>
Registrska številka:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Iskanje" />
</p>
<p>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
AutoGenerateRows="False" CellPadding="4" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" Height="50px" Width="915px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="REG_STEVILKA" HeaderText="REGISTRSKA ŠTEVILKA"
SortExpression="REG_STEVILKA" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="VIN_STEVILKA" HeaderText="VIN ŠTEVILKA"
SortExpression="VIN_STEVILKA" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="ZNAMKA" HeaderText="ZNAMKA"
SortExpression="ZNAMKA" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="MODEL" HeaderText="MODEL" SortExpression="MODEL"
InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="LETO_IZDELAVE" HeaderText="LETO IZDELAVE"
SortExpression="LETO_IZDELAVE" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="ID_ZAPISA" HeaderText="ID_ZAPISA"
InsertVisible="False" ReadOnly="True" SortExpression="ID_ZAPISA" />
<asp:BoundField DataField="DATUM" HeaderText="DATUM"
SortExpression="DATUM" />
<asp:BoundField DataField="KILOMETRINA" HeaderText="KILOMETRINA"
SortExpression="KILOMETRINA" />
<asp:BoundField DataField="OPIS" HeaderText="OPIS" SortExpression="OPIS" />
<asp:BoundField DataField="ODG_OSEBA" HeaderText="ODGOVORNA OSEBA"
SortExpression="ODG_OSEBA" />
<asp:BoundField DataField="CENA" HeaderText="CENA" SortExpression="CENA" />
<asp:BoundField DataField="REG_STEVILKA1" HeaderText="REG_STEVILKA1"
SortExpression="REG_STEVILKA1" Visible="False" />
<asp:BoundField DataField="ID_STRANKE" HeaderText="ID_STRANKE"
SortExpression="ID_STRANKE" Visible="False" />
<asp:BoundField DataField="PROSTORNINA_MOTORJA" HeaderText="PROSTORNINA_MOTORJA"
SortExpression="PROSTORNINA_MOTORJA" Visible="False" />
<asp:BoundField DataField="MOC_MOTORJA"
HeaderText="MOC_MOTORJA" SortExpression="MOC_MOTORJA"
Visible="False" />
<asp:BoundField DataField="TIP_MOTORJA" HeaderText="TIP_MOTORJA"
SortExpression="TIP_MOTORJA" Visible="False" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:DetailsView>
</p>
<p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Stranke %>"
DeleteCommand="DELETE FROM [SERVISNI_ZAPIS] WHERE [ID_ZAPISA] = #ID_ZAPISA"
InsertCommand="INSERT INTO [SERVISNI_ZAPIS] ([REG_STEVILKA], [DATUM], [KILOMETRINA], [OPIS], [ODG_OSEBA], [CENA]) VALUES (#REG_STEVILKA, #DATUM, #KILOMETRINA, #OPIS, #ODG_OSEBA, #CENA)" SelectCommand="SELECT *
FROM SERVISNI_ZAPIS
FULL JOIN VOZILO
ON (SERVISNI_ZAPIS.REG_STEVILKA=VOZILO.REG_STEVILKA)
WHERE (SERVISNI_ZAPIS.REG_STEVILKA = #REG_STEVILKA)"
UpdateCommand="UPDATE [SERVISNI_ZAPIS] SET [REG_STEVILKA] = #REG_STEVILKA, [DATUM] = #DATUM, [KILOMETRINA] = #KILOMETRINA, [OPIS] = #OPIS, [ODG_OSEBA] = #ODG_OSEBA, [CENA] = #CENA WHERE [ID_ZAPISA] = #ID_ZAPISA">
<DeleteParameters>
<asp:Parameter Name="ID_ZAPISA" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:ControlParameter ControlID="TextBox1" Name="REG_STEVILKA"
PropertyName="Text" />
<asp:Parameter DbType="Date" Name="DATUM" />
<asp:Parameter Name="KILOMETRINA" Type="String" />
<asp:Parameter Name="OPIS" Type="String" />
<asp:Parameter Name="ODG_OSEBA" Type="String" />
<asp:Parameter Name="CENA" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="REG_STEVILKA"
PropertyName="Text" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="REG_STEVILKA" Type="String" />
<asp:Parameter DbType="Date" Name="DATUM" />
<asp:Parameter Name="KILOMETRINA" Type="String" />
<asp:Parameter Name="OPIS" Type="String" />
<asp:Parameter Name="ODG_OSEBA" Type="String" />
<asp:Parameter Name="CENA" Type="String" />
<asp:Parameter Name="ID_ZAPISA" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</p>
<p>
</p>
<p>
</p>
</asp:Content>
Do you mean it doesn't work with a gridview but does work with a DetailsView? If so, then you need to set DataKeyNames for the gridview.
You can look at this for further details: http://fabdata.wordpress.com/2007/03/23/must-declare-the-scalar-variable-id/
I solved it by adding:
<asp:DetailsView ID="DetailsView1" runat="server" DataKeyNames="ID"....

Categories

Resources