Error Handling for empty textboxes in aspnet and vbnet - asp.net

I have an ADD button in my FormView in ASP.NET. And I have txtName on it that should first check if the textbox is empty. If it was empty, the ADD button should message Please fill up the form.
I tried the traditional error handling in vb.net like
If txtbox.text = "" then
error msg
end if
but it is not applicable when making a web page using ASP.NET backend: VBNET.
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1" Height="92px" Width="835px" AllowPaging="True" CellPadding="3" DefaultMode="Insert" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" GridLines="Horizontal" BackImageUrl= "howto.jpg" style="margin-bottom: 12px">
<EditRowStyle BackColor="#738A9C" BorderStyle="Solid" Font-Bold="True" ForeColor="#F7F7F7" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<InsertItemTemplate>
<asp:Label ID="lblName" runat="server" Text="Name of Applicant" Font-Size ="10pt" Font-Names="Arial" ></asp:Label>
<asp:Label ID="lblBday" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Birthday"></asp:Label>
<asp:Label ID="Gender" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Gender"></asp:Label>
<br />
<asp:TextBox ID="txtName" runat="server" Height="19px" Text='<%# Bind("Name") %>' Width="216px" Font-Size ="10" Font-Names="Arial"></asp:TextBox>
<asp:TextBox ID="txtBday" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Birthday") %>' TextMode="Date" Width="216px"></asp:TextBox>
<asp:DropDownList ID="txtGender" runat="server" Height="19px" Width="222px" SelectedValue='<%# Bind("Gender") %>'>
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Label ID="lblAddress" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Address"></asp:Label>
<asp:Label ID="lblContact" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Contact"></asp:Label>
<br />
<asp:TextBox ID="txtAddress" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Address") %>' Width="488px"></asp:TextBox>
<asp:TextBox ID="txtContact" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Contact") %>' Width="216px"></asp:TextBox>
<br />
<br />
<br />
<asp:Label ID="lblEduc" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Educational Attainment"></asp:Label>
<asp:Label ID="lblCourse" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Course"></asp:Label>
<br />
<asp:DropDownList ID="txtEduc" runat="server" Height="19px" SelectedValue='<%# Bind("Education") %>' Width="495px">
<asp:ListItem>High School</asp:ListItem>
<asp:ListItem>College Diploma (2-3 years)</asp:ListItem>
<asp:ListItem>College Undergrad (4-5 years)</asp:ListItem>
<asp:ListItem>Bachelor's Degree</asp:ListItem>
<asp:ListItem Value="Master">Masteral</asp:ListItem>
<asp:ListItem>Doctor</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtCourse" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Course") %>' Width="216px"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="btnAdd" runat="server" Text="ADD" Font-Bold="True" Font-Size="Medium" ForeColor="Black" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" CausesValidation="True" ValidationGroup="Insert" Font-Names="Berlin Sans FB Demi" CommandName="Insert" />
<asp:Button ID="btnCancel" runat="server" Text="CANCEL" Font-Bold="True" Font-Names="Berlin Sans FB Demi" Font-Size="Medium" ForeColor="Black" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" CommandName="Cancel" />
</InsertItemTemplate>
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
</asp:FormView>

You can't show that alert on a webapp, but you can use javascript in order to do it.
In code behind declare this function:
Public Sub AlertMessage(ByVal mensaje As String, ByVal pag As Page)
pag.ClientScript.RegisterStartupScript(Me.GetType(), "showConfirm", "Alerta('" & mensaje & "');", True)
End Sub
Then call it from your ADD button like this:
If txtbox.text = "" then
AlertMessage(msg, Me.Page)
end if
Hope this helps

I've learn from some research that there is other way for error handling using ASPNET. I used the validation control and validation group.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup ="FillUp" runat="server" ControlToValidate ="txtName" ErrorMessage="Please Enter Your Name"></asp:RequiredFieldValidator><br />
<asp:Button ID="btnAdd" runat="server" Text="ADD" Font-Bold="True" Font-Size="Medium" ForeColor="Black" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" CausesValidation="True" ValidationGroup="FillUp" Font-Names="Berlin Sans FB Demi" CommandName="Insert" />
enter image description here
I also add this code in my Web.config
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
I know, what you are thinking,"This guy has a poor desig", lol, well the important is you know the code xD

Related

ajaxToolKit:CalendarExtender not working on row 2 of gridview

My page has two approaches to adding a date to a text box inside the gridview. The user can set a date for all the rows by choosing a date and clicking a button. The code behind updates each row. This is working fine. Now I want to add the calendar to each row.
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
This is outside the gridview and working fine.
<asp:TextBox ID="SetDateTextBox" runat="server"></asp:TextBox>
<ajaxToolkit:PopupControlExtender ID="SetDateTextBox_PopupControlExtender" runat="server" BehaviorID="SetDateTextBox_PopupControlExtender"
DynamicServicePath="" ExtenderControlID="" TargetControlID="SetDateTextBox" PopupControlID="Panel1" Position="Bottom">
</ajaxToolkit:PopupControlExtender>
<asp:Panel ID="Panel1" runat="server" Width="200px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="Black" BorderStyle="Solid" CellSpacing="1" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" Height="250px" NextPrevFormat="ShortMonth" Width="330px">
<DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" Height="8pt" />
<DayStyle BackColor="#CCCCCC" />
<NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
<OtherMonthDayStyle ForeColor="#999999" />
<SelectedDayStyle BackColor="#333399" ForeColor="White" />
<TitleStyle BackColor="#333399" BorderStyle="Solid" Font-Bold="True" Font-Size="12pt" ForeColor="White" Height="12pt" />
<TodayDayStyle BackColor="#999999" ForeColor="White" />
</asp:Calendar>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
Now the problem. I added the calendarExtender to each row inside the templatefield.
<asp:TemplateField HeaderText="FINISH DATE" SortExpression="SCHED_FINISH_DATE">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SCHED_FINISH_DATE", "{0:d}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="FinishDateTextBox" runat="server" Text='<%# Bind("SCHED_FINISH_DATE", "{0:d}") %>' ClientIDMode="AutoID"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="FinishDateTextBox_CalendarExtender" runat="server" BehaviorID="FinishDateTextBox_CalendarExtender"
TargetControlID="FinishDateTextBox"/>
</ItemTemplate>
</asp:TemplateField>
I set the textbox ClientIDMode to AutoID. The page loads without error. I click the textbox on row 1 and the calendar works great. Click row 2 and beyond and the calendar does not appear.
I removed the popupcontrolextender along with the asp:panel and asp:UpdatePanel and replaced with the following code. This calendar will set a global change to all rows. So much cleaner.
<asp:TextBox ID="SetDateTextBox" runat="server"></asp:TextBox>
<ajaxtoolkit:calendarextender ID="CalendarEntender2" runat="server" TargetControlID="SetDateTextBox" />
Then inside the gridview.
<asp:TemplateField HeaderText="FINISH DATE" SortExpression="SCHED_FINISH_DATE" ItemStyle-Width="100">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SCHED_FINISH_DATE", "{0:d}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="FinishDateTextBox" ClientIDMode="AutoID" runat="server" Text='<%# Bind("SCHED_FINISH_DATE", "{0:d}") %>'></asp:TextBox>
<ajaxtoolkit:calendarextender ID="CalendarEntender1" runat="server" TargetControlID="FinishDateTextBox" />
</ItemTemplate>
<ItemStyle Width="100px"></ItemStyle>
</asp:TemplateField>
I still don't understand why the popupextender was causing an issue.

select all checkboxes using one checkbox in datalist

this is my code when i click on checkbox chkall , all checkboxes should be activated.
so can please anyone help me
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" CellPadding="4" ForeColor="#333333">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderTemplate>
<asp:CheckBox ID="chkall" runat="server" onclick="checktrue()" />
EMPID ENAME DESIGNATION DOJ SALARY DEPTNO Edit/Update
</HeaderTemplate>
<ItemStyle BackColor="#EFF3FB" />
<ItemTemplate>
<asp:CheckBox ID="chkone" runat="server" />
<asp:Label ID="lblempid" runat="server" Text='<%#Eval("empid") %>' />
<%#Eval("ename") %>
<%#Eval("desg") %>
<%#Eval("doj") %>
<%#Eval("salary") %>
<%#Eval("deptno") %>
<asp:Button ID="btnedit" runat="server" Text="EDIT" CommandName="Edit" />
</ItemTemplate>
<AlternatingItemStyle BackColor="White" />
<EditItemTemplate>
<asp:Label ID="lblempid" runat="server" Text='<%#Eval("empid") %>' />
<asp:TextBox ID="txtename" runat="server" Text='<%#Eval("ename") %>' />
<asp:TextBox ID="txtdesg" runat="server" Text='<%#Eval("desg") %>' />
<asp:TextBox ID="txtdoj" runat="server" Text='<%#Eval("doj") %>' />
<asp:TextBox ID="txtsalary" runat="server" Text='<%#Eval("salary") %>' />
<asp:TextBox ID="txtdeptno" runat="server" Text='<%#Eval("deptno") %>' />
<asp:Button ID="btnupdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btncancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btmdelete" runat="server" OnClick="btndelete_Click" Text="Delete" />
<asp:TextBox ID="txtempid" runat="server" Width="60"></asp:TextBox>
<asp:TextBox ID="txtename" runat="server" Width="60"></asp:TextBox>
<asp:TextBox ID="txtdesg" runat="server" Width="60"></asp:TextBox>
<asp:TextBox ID="txtdoj" runat="server" Width="60"></asp:TextBox>
<asp:TextBox ID="txtsalary" runat="server" Width="60"></asp:TextBox>
<asp:TextBox ID="txtdeptno" runat="server" Width="60"></asp:TextBox>
<asp:Button ID="btnadd" runat="server" Text="add" CommandArgument="add" />
</FooterTemplate>
<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
I recommend doing it on the client side. Here is an example with jquery
function checkAll(){
$("#DataList1 :checkbox").prop("checked", $("#chkall").prop("checked"));
}

Gridview is out of content place holder in firefox browser

My code sample is given below!
<%# Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="SMS_Table.aspx.cs" Inherits="Web_sms_alert.SMS_Table" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp"%>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:Label ID="Label1" runat="server" Font-Bold="true" Text="From: "></asp:Label>
<asp:TextBox ID="tbDatefrom" Font-Names="Segoe UI" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" Format="yyyy-MM-dd" DefaultView="Days" TargetControlID="tbDatefrom" runat="server">
</asp:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="tbDatefrom" Font-Bold="true" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:Label ID="Label2" runat="server" Font-Bold="true" Text="To: "></asp:Label>
<asp:TextBox ID="tbDateTo" Font-Names="Segoe UI" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender2" Format="yyyy-MM-dd" DefaultView="Days" TargetControlID="tbDateTo" runat="server">
</asp:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="tbDateTo" Font-Bold="true" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:Button runat="server" ID="btnSearch" CssClass="buttons" Text="Search" OnClick="btnSearchDW" /></br>
<div style="float: left; width: 98%; height: 30px;"><asp:CheckBox ID="cbDateRange" runat="server" Font-Bold="true" Font-Names="Segoe UI" Text="Date Range" /> <asp:CheckBox ID="cbsmsday" Text="SMS/Day" Font-Bold="true" Font-Names="Segoe UI" runat="server" /></div>
<asp:UpdatePanel ID="upl" runat="server">
<ContentTemplate>
<asp:GridView ID="GridviewSMS_Table" runat="server"
EnableSortingAndPagingCallbacks="false" AllowPaging="true" PageSize="20"
BorderColor="Chocolate" BorderStyle="None" OnPageIndexChanging="GridView1_PageIndexChanging"
BorderWidth="1px" CellPadding="3" GridLines="Vertical" Font-Names="Segoe UI" Width="354px">
<RowStyle BackColor="Wheat" ForeColor="Black" HorizontalAlign="Left" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnLinkView" runat="server" OnClick="btnLinkView_click">Text</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" Font-Size="Medium" />
<EditRowStyle BackColor="#000084" Font-Bold="True" ForeColor="White" Font-Size="Medium" />
</asp:GridView>
<asp:Panel ID="PanelText" runat="server" CssClass="ModalPopup" style="display:none">
<table border="1" align="center" style="border-color: #FF6600">
<tr>
<td> <asp:Label ID="lblTextShow" ForeColor="Black" Text="Text Message: " runat="server"></asp:Label></td>
<td><asp:TextBox ID="tbMessage" runat="server" TextMode="MultiLine" ForeColor="Black" Height="150px" Width="200px"></asp:TextBox></td>
</tr>
<tr align="center" valign="middle">
<td colspan="2">
<asp:Button ID="btnClose" runat="server" ForeColor="Black" Text="Close" />
</td> </tr>
</table>
</asp:Panel>
<asp:Button ID="btncontrol" runat="server" style="display:none" />
<asp:ModalPopupExtender ID="mpeMessage" runat="server" Enabled="true"
TargetControlID="btncontrol" PopupControlID="PanelText"
BackgroundCssClass="ModalBackground" CancelControlID="btnClose">
</asp:ModalPopupExtender>
</ContentTemplate>
</asp:UpdatePanel>
<div align="center">
<asp:Label ID="lblTotalDp" Font-Bold="true" Visible="false" ForeColor="Black" runat="server" Text="Total: "></asp:Label>
<asp:Label ID="lbltotal" Font-Bold="true" Visible="false" ForeColor="Black" runat="server"></asp:Label></div>
</div>
the page is shown in google chrome and IE good Like below
while in fire fox the grid-view is outside content place holder of masterpage! Like below
I didnt understand why this problem i am facing??? Please help me out of this.
remove from <div style="float: left the float:left
I do not see any reason for that, and the float is making this problem.

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>

dropdown list won't polpuate values from database

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.

Resources