dropdown list won't polpuate values from database - asp.net

I'm stuck with a problem to populate a DropDownList control with values from the database using item field template in read only mode. I appreciate a detailed explanation, since I'm new to ASP.NET. Below is the code and the error I'm getting:
'PictureReadOnlyCategories' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value 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.ArgumentOutOfRangeException: 'PictureReadOnlyCategories' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
The code for this is :
<asp:SqlDataSource ID="categoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand ="SELECT [CategoryID], [Name] FROM [Categories] WHERE ([UserId] = #UserId)">
<SelectParameters>
<asp:ControlParameter ControlID="UserIdValue"
Name="UserId"
PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label ID="UserIdValue" runat="server" Visible="False"></asp:Label>
<asp:GridView ID="gvPictures"
runat="server"
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False"
CellPadding="4"
DataKeyNames="PictureID"
DataSourceID="picturesDataSource"
ForeColor="#333333"
GridLines="None" Width="800px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True"
ValidationGroup="PictureEdit" />
<asp:TemplateField HeaderText="Category"
SortExpression="CategoryID">
<EditItemTemplate>
<asp:DropDownList ID="pictureEditCategories"
runat="server"
AppendDataBoundItems="True"
DataSourceID="categoriesDataSource"
DataTextField="Name"
DataValueField="CategoryID"
SelectedValue='<%# Bind("CategoryID") %>'>
<asp:ListItem Value="" Text="--Select Category -- "/>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="PictureReadOnlyCategories"
runat="server"
AppendDataBoundItems="True"
DataSourceID="categoriesDataSource"
DataTextField="Name"
DataValueField="CategoryID"
Enabled="False"
SelectedValue='<%# Bind("CategoryID") %>'>
<asp:ListItem Selected="True" Value="">-- No Category --</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<EditItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
EnableViewState="False"
Text='<%# Bind("Title") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4"
runat="server"
ControlToValidate="TextBox1"
Display="Dynamic"
ErrorMessage="must enter a title"
ValidationGroup="PictureEdit">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1"
runat="server"
Text='<%# Bind("Title") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="TextBox2"
runat="server"
Columns="25"
Rows="4"
Text='<%# Bind("Description") %>'
TextMode="MultiLine">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5"
runat="server"
ControlToValidate="TextBox2"
Display="Dynamic"
ErrorMessage="you must enter a description"
ValidationGroup="PictureEdit">
</asp:RequiredFieldValidator><
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2"
runat="server"
Text='<%# Bind("Description") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Added" SortExpression="UploadedOn">
<EditItemTemplate>
<asp:Label ID="Label4"
runat="server"
Text='<%# Bind("UploadedOn") %>'>
</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3"
runat="server"
Text='<%# Bind("UploadedOn") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ImageField DataImageUrlField="PictureID"
DataImageUrlFormatString="~/UploadedImages/{0}.jpg"
HeaderText="Image"
ReadOnly="True">
<ControlStyle Width="100px" />
</asp:ImageField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

You need to make sure that you are binding the PictureReadOnlyCategories DDL to populate the items before you are binding the selected item. The problem looks like it is with the order things are being done and that is all. If you try to select an item in a DDL that does not exist then you get that error.

Related

Asp.net Detailview not updating in realtime

When I add a product category and the detailview will not update by itself. Is there anyway that I can do that?
This is the code of adding category
<div class="container "style="background-color:lavender">
<h2>Product Category </h2>
<asp:DetailsView ID="DetailsViewCategory" runat="server" AutoGenerateRows="False" CellPadding="4" DataKeyNames="CategoryId" DataSourceID="SqlDataSourceCategory" DefaultMode="Insert" Height="83px" Width="360px" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="CategoryId" HeaderText="CategoryId" InsertVisible="False" ReadOnly="True" SortExpression="CategoryId" />
<asp:BoundField DataField="CategoryName" HeaderText="Category Name" SortExpression="CategoryName" />
<asp:CommandField CancelText="" ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#284775" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:DetailsView>
<br />
</div>
and this is the code of my detail view
<asp:DetailsView ID="DetailsViewProduct" runat="server" AutoGenerateRows="False" DataKeyNames="ProductId" DataSourceID="SqlDataSourceProduct" DefaultMode="Insert" Height="16px" Width="46%" OnItemInserted="InsertProduct" HorizontalAlign="Center">
<Fields>
<asp:TemplateField HeaderText="Picture">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:FileUpload ID="FileUploadPicture" runat="server" Height="22px" Width="400px" CssClass="center-block" ForeColor="Black"/>
</InsertItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductId" HeaderText="ProductId" SortExpression="ProductId" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="ProductName" HeaderText="Name" SortExpression="ProductName"/>
<asp:TemplateField HeaderText="Description" SortExpression="ProductDescription">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ProductDescription") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Height="42px" Text='<%# Bind("ProductDescription") %>' TextMode="MultiLine" Width="400px" CssClass="center-block"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductDescription") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="ProductCategory">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ProductCategory") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:SqlDataSource ID="SqlDataSourcePC" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT * FROM [ProductCategory]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSourcePC" DataTextField="CategoryName" DataValueField="CategoryId" Height="18px" SelectedValue='<%# Bind("ProductCategory") %>' Width="400px" CssClass="center-block">
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductCategory") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Brand" SortExpression="ProductBrand">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("ProductBrand") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:SqlDataSource ID="SqlDataSourceBrand1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT * FROM [ProductBrand]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SqlDataSourceBrand1" DataTextField="ProductBrand" DataValueField="BrandId" SelectedValue='<%# Bind("ProductBrand") %>' Width="400px" CssClass="center-block">
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductBrand") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductPrice" HeaderText="Price (RM)" SortExpression="ProductPrice" />
<asp:BoundField DataField="ProductQty" HeaderText="Stock Quantity" SortExpression="ProductQty" />
<asp:CommandField CancelText="" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
Yes data controls do not update automatically as they need page reload and databinding. call DataBind() method of your control in the page load event
DetailsViewCategory.DataBind();

Checking and Unchecking single checkbox and saving value in database in grid view in edit button click even

I need a Status column in database which contains 1,0 for representing Employee's Status "Active" and "Inactive" respectively. I want edit this record through front-end,having grid-view, and want to perform on GridView's update event, after clicking on Edit Button. I have a TemplateField having header-text "Status". I am unable to update changed value of checkbox.
Moreover, if will get checked or unchecked CheckBox on the basis of value stored in database, if it is 0 then CheckBox will be uncheked, otherwise will be checked. If user click on edit button, and then check or uncheck any Check-box, then on the basis of this, value should be updated in database.
DataBase:-
CREATE TABLE [dbo].[Employee](
[Employee_ID] [int] IDENTITY(1,1) NOT NULL,
[Employee_Name] [varchar](50) NULL,
[Employee_Address] [varchar](100) NULL,
[Emp_Status] [int] NULL,
PRIMARY KEY CLUSTERED
(
[Employee_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON
Front-End:-
Mark-up:
<asp:GridView ID="GV_Product" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="false" ShowFooter="true" OnRowEditing="GV_Product_RowEditing" OnRowUpdating="GV_Product_RowUpdating" OnRowDeleting="GV_Product_RowDeleting" OnRowCancelingEdit="GV_Product_RowCancelingEdit" OnRowCommand="GV_Product_RowCommand" AllowPaging="true" PageSize="5" OnPageIndexChanging="GV_Product_PageIndexChanging">
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:TextBox ID="txt_Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Name") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEdit_Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAdd_Name" runat="server"></asp:TextBox>
<%--<asp:RequiredFieldValidator ID="txtName" runat="server" ControlToValidate="txtAdd_Name" ErrorMessage="Please enter Employee Name"></asp:RequiredFieldValidator>--%>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Address">
<ItemTemplate>
<asp:TextBox ID="txt_Address" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Address") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEdit_Address" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Address") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAdd_Address" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lblAdd" runat="server" Width="100px" Text="Add" CommandName="ADD"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:CheckBox ID="ChkBox" runat="server" Checked='<%# GetStatus(DataBinder.Eval(Container.DataItem,"Emp_Status").ToString() )%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="EditChkBox" runat="server" Checked='<%# GetStatus(DataBinder.Eval(Container.DataItem,"Emp_Status").ToString() )%>'/>
<%--<asp:CheckBoxList ID="ChkBoxList" runat="server">
<asp:ListItem>1</asp:ListItem>
</asp:CheckBoxList>--%>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
Code:
protected bool GetStatus(string str)
{
if (str=="1")
{
return true;
}
else
{
return false;
}
}

Footer Row failing to Insert into table via asp gridview

I'm working with vb.net and have followed a tutorial to set up a gridview to update/delete/insert records into a table. I have a footer row that isn't working for me. when I click my insert button the textboxes from the footer do not do the insert. I think I am missing something fundamental that is causing this. May the page is posting back before the sub grabs the text from the textboxes? My understanding of it is pretty but I feel this could be the issue because I have two radio buttons that I use to switch between SQL data adapters because I want to be able to work with different tables. I have these set to postback="true". Could that be my issue?
SqlDataAdapter:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Server=SQLSERVER;Database=DB1;UID=user;PWD=password"
DeleteCommand="delete from AVZV_LoginRequestSales where id = #id"
SelectCommand="select * from AVZV_LoginRequestSales"
UpdateCommand="update AVZV_LoginRequestSales set name=#name, role = #role, supervisor = #supervisor, location = #location, trainingSdt = #trainingSdt, productionSdt = #productionSdt, terminatedDate = #terminatedDate, notes = #notes where id = #id">
<DeleteParameters>
<asp:Parameter Name="id" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="firstname" />
<asp:Parameter Name="lastname" />
<asp:Parameter Name="role" />
<asp:Parameter Name="supervisor" />
<asp:Parameter Name="location" />
<asp:Parameter Name="trainingSdt" />
<asp:Parameter Name="productionSdt" />
<asp:Parameter Name="terminatedDate" />
<asp:Parameter Name="notes" />
</UpdateParameters>
</asp:SqlDataSource>
Datagrid:
<asp:GridView ID="gvMatrix" runat="server"
AutoGenerateColumns="false" enableviewstate="false" showheaderwhenempty="true"
AllowSorting="True" emptydatatext="No Agents Listed" showfooter="true"
OnDataBound="OnDataBound" DataKeyNames="id" >
<Columns>
<asp:TemplateField HeaderText="Full Name" InsertVisible="False" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newName" runat="server" placeholder="Full Name"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Role" InsertVisible="False" SortExpression="role">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("role") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtrole" runat="server" Text='<%# Bind("role") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newrole" runat="server" placeholder="Role"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Supervisor" InsertVisible="False" SortExpression="supervisor">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("supervisor") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtsupervisor" runat="server" Text='<%# Bind("supervisor") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newsupervisor" runat="server" placeholder="Supervisor"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" InsertVisible="False" SortExpression="location">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("location") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtlocation" runat="server" Text='<%# Bind("location") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newlocation" runat="server" placeholder="Location"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Training Start Date" InsertVisible="False" SortExpression="trainingsdt">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("trainingsdt") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txttrainingsdt" runat="server" Text='<%# Bind("trainingsdt") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newtrainingsdt" runat="server" placeholder="Training Start Date"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False"/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Production Start Date" InsertVisible="False" SortExpression="Productionsdt">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Productionsdt") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtProductionsdt" runat="server" Text='<%# Bind("Productionsdt") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newProductionsdt" runat="server" placeholder="Production Start Date"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False"/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Terminated Date" InsertVisible="False" SortExpression="Terminateddate">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Terminateddate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtTerminateddate" runat="server" Text='<%# Bind("Terminateddate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newTerminateddate" runat="server" placeholder="Terminated Date"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False"/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes" InsertVisible="False" SortExpression="notes">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("notes") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtNotes" runat="server" Text='<%# Bind("notes") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:TextBox ID="newNotes" runat="server" placeholder="Notes"></asp:TextBox>
</FooterTemplate>
<FooterStyle Wrap="False"/>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<ItemTemplate>
<asp:Button ID="InsertRecord" runat="server" Text="Add Record" CommandName="Insert" />
</ItemTemplate>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" showeditbutton="true"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="deleteButton" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#3367a2" ForeColor="White" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#2461bf" Font-Bold="True" ForeColor="blue" HorizontalAlign="Center" />
<EditRowStyle CssClass="gridview_edit" />
<FooterStyle CssClass="GVFixedFooter" />
<AlternatingRowStyle BackColor="#eff3fb" ForeColor="#3367a2" HorizontalAlign="Center" />
</asp:GridView>
Code for Insert:
Protected Sub gvMatrix_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvMatrix.RowCommand
' Insert data if the CommandName == "Insert"
If e.CommandName = "Insert" AndAlso Page.IsValid Then
If rbSales.checked = True Then
SqlDataSource1.Insert()
Else
SqlDataSource2.Insert()
End If
End If
End Sub
Protected Sub SqlDataSource1_Inserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Inserting
' Programmatically reference Web controls in the inserting interface...
Dim NewName As TextBox = _
gvMatrix.FooterRow.FindControl("NewName")
Dim NewRole As TextBox = _
gvMatrix.FooterRow.FindControl("NewRole")
Dim NewSupervisor As TextBox = _
gvMatrix.FooterRow.FindControl("NewSupervisor")
Dim NewLocation As TextBox = _
gvMatrix.FooterRow.FindControl("NewLocation")
Dim Newtrainingsdt As TextBox = _
gvMatrix.FooterRow.FindControl("Newtrainingsdt")
Dim Newproductionsdt As TextBox = _
gvMatrix.FooterRow.FindControl("Newproductionsdt")
Dim Newterminateddate As TextBox = _
gvMatrix.FooterRow.FindControl("Newterminateddate")
Dim NewNotes As TextBox = _
gvMatrix.FooterRow.FindControl("NewNotes")
' Set the ObjectDataSource's InsertParameters values...
e.command.parameters("Name").Value = NewName.Text
e.command.parameters("role").Value = NewRole.Text
e.command.parameters("supervisor").Value = NewSupervisor.Text
e.command.parameters("location").Value = NewLocation.Text
e.command.parameters("trainingsdt").Value = Newtrainingsdt.Text
e.command.parameters("productionsdt").Value = Newproductionsdt.Text
e.command.parameters("terminatedDate").Value = Newterminateddate.Text
e.command.parameters("notes").Value = NewNotes.Text
End Sub
I know its a lot to look at but again I feel my issue might not be with the code so much as me missing a concept. Thanks in advance for any ideas/suggestions.

ASP issue giving me, ERROR: Procedure or function uspPatientUpdate has too many arguments specified

What is happening: I'm trying to update a patient table in a gridview table using a stored procedure.
Error:
'DropDownList2' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
Here is my procedure:
ALTER PROCEDURE dbo.uspPatientUpdate
#ID int,
#OHIP char(10),
#FirstName nvarchar(20),
#LastName nvarchar(40),
#DOB date,
#VisitsPerYear int,
#DoctorID int
AS
BEGIN
SET NOCOUNT OFF
UPDATE Patient
SET OHIP = #OHIP,
FirstName = #FirstName,
LastName = #LastName,
DOB = #DOB,
VisitsPerYear = #VisitsPerYear,
DoctorID = #DoctorID
WHERE ID = #ID
END
Here is my asp:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MedicalOffice.aspx.cs" Inherits="MedicalOffice" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="StyleSheet.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Maintain Patients</h1>
<p>Select Patient By Doctor:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="sdDoctorList" DataTextField="Doctor" DataValueField="ID">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" DataSourceID="sdPatient" GridLines="Horizontal">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField HeaderText="OHIP" SortExpression="OHIP">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("OHIP") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("OHIP") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOB" SortExpression="DOB">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("DOB") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("DOB") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Visits Per Year" SortExpression="VisitsPerYear">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("VisitsPerYear") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("VisitsPerYear") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Doctor" SortExpression="Doctor">
<EditItemTemplate>
#<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="sdDoctorList" DataTextField="Doctor" DataValueField="ID">#
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Doctor") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" HeaderText="Insert Patient" Height="50px" Width="125px">
<EditRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:TemplateField HeaderText="OHIP" SortExpression="OHIP">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("OHIP") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("OHIP") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("OHIP") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOB" SortExpression="DOB">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("DOB") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("DOB") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("DOB") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VisitsPerYear" SortExpression="VisitsPerYear">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("VisitsPerYear") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("VisitsPerYear") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("VisitsPerYear") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Doctor" SortExpression="Doctor">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Doctor") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="sdDoctorList" DataTextField="Doctor" DataValueField="ID" SelectedValue='<%# Bind("Doctor") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Doctor") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" />
</asp:DetailsView>
<asp:SqlDataSource ID="sdDoctorList" runat="server" ConnectionString="<%$ ConnectionStrings:MedicalOfficeConnectionString %>" SelectCommand="uspDoctorList" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
</p>
</div>
<asp:SqlDataSource ID="sdPatient" runat="server" ConnectionString="<%$ ConnectionStrings:MedicalOfficeConnectionString %>" DeleteCommand="usp_PatientDelete" InsertCommand="uspPatientInsert" SelectCommand="uspPatientSelectByIDOrSelectAll" UpdateCommand="uspPatientUpdate" SelectCommandType="StoredProcedure" DeleteCommandType="StoredProcedure" InsertCommandType="StoredProcedure" UpdateCommandType="StoredProcedure">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="OHIP" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter DbType="Date" Name="DOB" />
<asp:Parameter Name="VisitsPerYear" Type="Int32" />
<asp:Parameter Name="DoctorID" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="doctorID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="OHIP" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter DbType="Date" Name="DOB" />
<asp:Parameter Name="VisitsPerYear" Type="Int32" />
<asp:Parameter Name="DoctorID" Type="Int32" />
<asp:Parameter Name="Timestamp" Type="Object"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please fix the following:" />
</form>
</body>
</html>
uspPatientUpdate is missing the TimeStamp parameter:
<UpdateParameters>
...
<asp:Parameter Name="Timestamp" Type="Object"></asp:Parameter>
</UpdateParameters>
Or, perhaps it isn't missing it and you simply shouldn't specify it in the markup.

HtmlEncode not working in asp.net

I having a problem to make HtmlEncode work properly inside a GridView in ASP.Net. Basically I'm using TemplateField to toggle ItemTemplate and EditItemTemplate to have data entry on the GridView itself. I'm looking for some resource in how I can encode Eval and Bind method in ASP.Net but all of them not work in my end.
here's my code below:
<div id="dvShowContent" runat="server" style="text-align: center">
<asp:GridView ID="dgvSortKey" runat="server" AllowSorting="True" OnRowDataBound="gv_drb"
AutoGenerateColumns="False" AllowPaging="True" BackColor="White" BorderColor="#336666"
BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal"
Height="73px" AutoGenerateEditButton="True" OnRowEditing="dgvSortKey_RowEditing"
OnRowUpdating="dgvSortKey_RowUpdating" OnRowCancelingEdit="dgvSortKey_RowCancelingEdit"
OnSelectedIndexChanging="dgvSortKey_SelectedIndexChanging" OnPageIndexChanged="dgvSortKey_PageIndexChanged"
OnPageIndexChanging="dgvSortKey_PageIndexChanging" OnRowCommand="dgvSortKey_RowCommand"
OnRowDeleted="dgvSortKey_RowDeleted" OnRowUpdated="dgvSortKey_RowUpdated" Width="561px"
PageSize="15" DataKeyNames="KeyCode,KeyDescription">
<FooterStyle BackColor="White" ForeColor="#333333" />
<RowStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="LightCyan" />
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="lnkdelete" runat="server" OnClick="lnkdelete_Click">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Instruction Key Code">
<ItemTemplate>
<asp:Label ID="lblValKeyCode" runat="server" Text='<%# Server.HtmlEncode((string)Eval("KeyCode")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#Bind("KeyCode") %>' MaxLength="10"></asp:TextBox>
<%--<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode(Eval("KeyCode").ToString()) %>' MaxLength="10"></asp:TextBox>--%>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="GvBorderGreen" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblValKeyDescription" runat="server" Text='<%# Server.HtmlEncode((string)Eval("KeyDescription")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtValKeyDescription" runat="server" Text='<%#Bind("KeyDescription") %>'
MaxLength="10"></asp:TextBox>
<%--<asp:TextBox ID="txtValKeyDescription" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode(Eval("KeyDescription").ToString()) %>'
Width="300" MaxLength="20"></asp:TextBox>--%>
</EditItemTemplate>
<ItemStyle CssClass="GvBorderGreen" />
</asp:TemplateField>
<%--<asp:BoundField DataField="KeyCode" HeaderText="Instruction Key Code" HtmlEncode="true" />
<asp:BoundField DataField="KeyDescription" HeaderText="Description" HtmlEncode="true" />--%>
</Columns>
</asp:GridView>
</div>
Please help me to solve this problem.
As Waqar suggests the answer is in the link he provided - to make this more clear you need to change this line:
<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#Bind("KeyCode") %>' MaxLength="10"></asp:TextBox>
To this:
<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode((string)Eval("KeyCode")) %>' MaxLength="10"></asp:TextBox>

Resources