error in Update command - c#

in my project(Asp.net website using C#), I have a formview that is connected to a sqlserevrDataSource.
because Of using specify custom sql statement for sqlDataSource Configuration, I can't use Advanced button for active Insert,Update,Delete . Now for adding update feature for my datasource I add UpdateCommand in my source code manually, but it had an error that said :
Invalid column name 'Email'.
Invalid column name 'IsApproved'.
Invalid column name 'IsLockedOut'.
Invalid column name 'LastLockoutDate'.
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.
the code:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPageAdmin.master" AutoEventWireup="true" CodeFile="edit-user.aspx.cs" Inherits="edit_user" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:FormView ID="FormView1" runat="server" DefaultMode="Edit" DataSourceID="SqlDataSource1">
<EditItemTemplate>
UserName:
<asp:TextBox ID="UserNameTextBox" runat="server" Text='<%# Bind("UserName") %>' />
<br />
LastActivityDate:
<asp:TextBox ID="LastActivityDateTextBox" runat="server" Text='<%# Bind("LastActivityDate") %>' />
<br />
Email:
<asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
<br />
IsApproved:
<asp:CheckBox ID="IsApprovedCheckBox" runat="server" Checked='<%# Bind("IsApproved") %>' />
<br />
IsLockedOut:
<asp:CheckBox ID="IsLockedOutCheckBox" runat="server" Checked='<%# Bind("IsLockedOut") %>' />
<br />
LastLockoutDate:
<asp:TextBox ID="LastLockoutDateTextBox" runat="server" Text='<%# Bind("LastLockoutDate") %>' />
<br />
UserId:
<asp:TextBox ID="UserIdTextBox" runat="server" Text='<%# Bind("UserId") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
UserName:
<asp:TextBox ID="UserNameTextBox" runat="server" Text='<%# Bind("UserName") %>' />
<br />
LastActivityDate:
<asp:TextBox ID="LastActivityDateTextBox" runat="server" Text='<%# Bind("LastActivityDate") %>' />
<br />
Email:
<asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
<br />
IsApproved:
<asp:CheckBox ID="IsApprovedCheckBox" runat="server" Checked='<%# Bind("IsApproved") %>' />
<br />
IsLockedOut:
<asp:CheckBox ID="IsLockedOutCheckBox" runat="server" Checked='<%# Bind("IsLockedOut") %>' />
<br />
LastLockoutDate:
<asp:TextBox ID="LastLockoutDateTextBox" runat="server" Text='<%# Bind("LastLockoutDate") %>' />
<br />
UserId:
<asp:TextBox ID="UserIdTextBox" runat="server" Text='<%# Bind("UserId") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
UserName:
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Bind("UserName") %>' />
<br />
LastActivityDate:
<asp:Label ID="LastActivityDateLabel" runat="server" Text='<%# Bind("LastActivityDate") %>' />
<br />
Email:
<asp:Label ID="EmailLabel" runat="server" Text='<%# Bind("Email") %>' />
<br />
IsApproved:
<asp:CheckBox ID="IsApprovedCheckBox" runat="server" Checked='<%# Bind("IsApproved") %>' Enabled="false" />
<br />
IsLockedOut:
<asp:CheckBox ID="IsLockedOutCheckBox" runat="server" Checked='<%# Bind("IsLockedOut") %>' Enabled="false" />
<br />
LastLockoutDate:
<asp:Label ID="LastLockoutDateLabel" runat="server" Text='<%# Bind("LastLockoutDate") %>' />
<br />
UserId:
<asp:Label ID="UserIdLabel" runat="server" Text='<%# Bind("UserId") %>' />
<br />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:akhbarrConnectionString %>"
SelectCommand="SELECT aspnet_Users.UserName, aspnet_Users.LastActivityDate, aspnet_Membership.Email, aspnet_Membership.IsApproved, aspnet_Membership.IsLockedOut, aspnet_Membership.LastLockoutDate, aspnet_Users.UserId FROM aspnet_Users INNER JOIN aspnet_Membership ON aspnet_Membership.UserId = aspnet_Users.UserId WHERE (aspnet_Users.UserName = #UserName)"
updatecommand="update [aspnet_Users] set [UserName] =#UserName , [LastActivityDate]= #LastActivityDate , [Email]= #Email , [IsApproved] = #IsApproved , [IsLockedOut] = #IsLockedOut , [LastLockoutDate] = #LastLockoutDate , [UserId]= #UserId from aspnet_Users INNER JOIN aspnet_Membership ON aspnet_Membership.UserId = aspnet_Users.UserId">
<UpdateParameters>
<asp:Parameter Name="UserName" Type="string" />
<asp:Parameter Name="LastActivityDate" Type="DateTime" />
<asp:Parameter Name="Email" Type="string" />
<asp:Parameter Name="IsApproved" Type="Boolean" />
<asp:Parameter Name="IsLockedOut" Type="Boolean" />
<asp:Parameter Name="LastLockoutDate" Type="DateTime" />
<asp:Parameter Name="UserId" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0" Name="UserName" QueryStringField="UserName" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>

This error simply means that you are trying to run your command on some datatable that doesn't contains these columns. You need to make sure that your update command is correct. Copy the command that you are trying to run and run in sql server management studio directly against your database.

You can't update two table by this query. As I understand your
'Email',
'IsApproved'.
'IsLockedOut'.
'LastLockoutDate'
column are belong to table aspnet_Membership and you are using query
update [aspnet_Users] set [UserName] =#UserName , [LastActivityDate]= #LastActivityDate ,
[Email]= #Email , [IsApproved] = #IsApproved , [IsLockedOut] = #IsLockedOut ,
[LastLockoutDate] = #LastLockoutDate , [UserId]= #UserId from aspnet_Users INNER JOIN
aspnet_Membership ON aspnet_Membership.UserId = aspnet_Users.UserId
it only update column of table aspnet_Users.
Edit -
update [aspnet_Users] set [UserName] =#UserName , [LastActivityDate]=
#LastActivityDate Where [UserId]=#userId
and
update [aspnet_Membership] set [Email]= #Email , [IsApproved] = #IsApproved ,
[IsLockedOut] = #IsLockedOut , [LastLockoutDate] = #LastLockoutDate where UserId= #UserId

Related

Why does asp.net display table name instead of table field

im working on a project and im combining 3 tables the tables are already associated in the dbml but my problem is when i use the data list it diaplays my table names not the correct fields this is a college assignment but i have tried everything i know to do.
I've tried to combine them seperatly in sql manager in vs but im just not understanding why it isn't showing the correct thing
<form id="form1" runat="server">
<div>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="assign_6_final.DataClasses1DataContext" EntityTypeName="" Select="new (CustomerID, Name)" TableName="Customers">
</asp:LinqDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="LinqDataSource1" DataTextField="Name" DataValueField="CustomerID" OnSelectedIndexChanged="Page_Load">
</asp:DropDownList>
<asp:DataList ID="DataList1" runat="server" DataSourceID="LinqDataSource2" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" OnDataBinding="DataList1_SelectedIndexChanged">
<ItemTemplate>
IncidentID:
<asp:Label ID="IncidentIDLabel" runat="server" Text='<%# Eval("IncidentID") %>' />
<br />
DateOpened:
<asp:Label ID="DateOpenedLabel" runat="server" Text='<%# Eval("DateOpened") %>' />
<br />
DateClosed:
<asp:Label ID="DateClosedLabel" runat="server" Text='<%# Eval("DateClosed") %>' />
<br />
Title:
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
<br />
CustomerID:
<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' />
<br />
Technician:
<asp:Label ID="TechnicianLabel" runat="server" Text='<%# Eval("Technician") %>' />
<br />
Product:
<asp:Label ID="ProductLabel" runat="server" Text='<%# Eval("Product") %>' />
<br />
<br />
<br />
</ItemTemplate>
</asp:DataList>
</div>
<asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="assign_6_final.DataClasses1DataContext" EntityTypeName="" Select="new (IncidentID, DateOpened, DateClosed, Title, Description, CustomerID, Technician, Product)" TableName="Incidents" Where="CustomerID = #CustomerID">
<WhereParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="CustomerID" PropertyName="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<asp:EntityDataSource ID="EntityDataSource1" runat="server">
</asp:EntityDataSource>
</form>
</body>
the outcome i expected was that the last 2 items the product and technician would output correctly but they haven't and im kind of lost my teacher said this is a common problem in asp but ive never seen it
Try using the following and so forth:
<%# DataBinder.Eval(Container.DataItem,"CustomerID") %>

how to show data from db in dataList view on the basis of dropdown-list items

I want to show my data in data list view on the basis of selected item from drop down menu..
I have bonded the data with my data list view its showing all the data columns except the image ..
How should i display my image data from db as well .. on the basis of selected item from drop down it should respond but its not showing any response..
I didn't use any code behind logic for this all.. i have done it by the help of wizard to generate queries and bind specific data with my db, and show data to its respective controls..
if someone has done related work .. please comment out
thanks..
This is my code so far
<strong><span class="auto-style2">I wanna see: </span></strong> <strong><asp:DropDownList ID="DropDownList1" runat="server" Height="44px" Width="214px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="auto-style3" DataTextField="Brand" DataValueField="Brand">
<asp:ListItem Selected="True">clothes</asp:ListItem>
<asp:ListItem>jewelry</asp:ListItem>
<asp:ListItem>shoes</asp:ListItem>
</asp:DropDownList>
</strong>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
Width="99%" BorderColor="Blue" BorderStyle="Solid" BorderWidth="3px" DataSourceID="SqlDataSource1" Height="456px">
<ItemTemplate>
Brand:
<asp:Label ID="BrandLabel" runat="server" Text='<%# Eval("Brand") %>'></asp:Label>
<br />
<br />
Image:<br />
<br />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Image") %>' />
<br />
<br />
Price:
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
<br />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
<br />
<br />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:loginString %>" SelectCommand="SELECT [Brand], [Price], [Image], [Description] FROM [w_product] WHERE ([Category] = #Category)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="clothes" Name="Category" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

Passing Eval value to C# method for shopping cart

I am creating a shopping cart web app for my class. When the user clicks on the add to cart button, I want to pass the value of the ID of each specific product to a separate method in the code behind. Not sure if my syntax is off or if this just wont work. Here is my mark up:
<asp:Button ID="AddToCart" CommandName="Add"
OnClientClick ='<%# ListView1_AddToCart(Eval("ID"))%>'
CssClass ="Button" runat="server" Text="Add to Cart" />
Here is my code behind:
public void ListView1_AddToCart(string CatID)
{cart.AddToCart(CatID);}
I keep getting various issues, but this gives me the following error:
The best overloaded method match for 'OurCats_GrumpyCats.ListView1_AddToCart(string)'
has some invalid arguments.
How can I resolve this? Is there any better way?
EDIT: Here is my mark up
<%# Page Title="All Cats" Language="C#" MasterPageFile="~/MasterPage/Layout.master" AutoEventWireup="true" CodeFile="AllCats.aspx.cs" Inherits="OurCats_GrumpyCats" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<h1>Meet all of our kittens!</h1>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource2">
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<InsertItemTemplate>
<span style="">ID:
<asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("ID") %>' />
<br />
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
<br />
Price:
<asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' />
<br />
Imgu:
<asp:TextBox ID="ImguTextBox" runat="server" Text='<%# Bind("Img") %>' />
<br />
Description:
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
<br />
<br />
</span>
</InsertItemTemplate>
<ItemTemplate>
<table class ="Table" style="border-style: solid; ">
<tr>
<td >
<a href ="Details.aspx?ID=<%# Eval("ID")%>">
<img src="../Images/<%# Eval("Img") %>" width ="200" />
</a>
</td>
<td style="width:700px; margin-left: 100px">
<asp:Label ID="Label1" runat="server" Text="Name: "></asp:Label><asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<asp:Textbox ID="CatID" runat="server" Visible="false" Text='<%# Eval("ID")%>'></asp:Textbox>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Price: $"></asp:Label><asp:Label ID="PriceLabel" runat="server" Text= '<%# Eval("Price") %>'/>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Description: "></asp:Label><asp:Label ID="DescriptionLabel" runat="server" Text= '<%# Eval("Description") %>' />
<br />
<br />
<asp:Button ID="AddToCart" CommandName="Add" OnClientClick='<%# "AddToCart(" +Eval("ID") + " );" %>' CssClass ="Button" runat="server" Text="Add to Cart" />
</td>
</tr>
</table>
</ItemTemplate>
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server" style =" margin-left :30px;">
<span runat="server" id="itemPlaceholder" />
</div>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:420_Project_GLConnectionString1 %>" SelectCommand="SELECT * FROM [OurCats]">
</asp:SqlDataSource>
</asp:Content>
try below code
<asp:Button ID="AddToCart" CommandName="Add"
OnClientClick ='<%# ListView1_AddToCart((string)Eval("ID"))%>'
CssClass ="Button" runat="server" Text="Add to Cart" />
OnClientClick ='<%# "ListView1_AddToCart(" +Eval("ID") + " );" %>'

Insert database row via ASP.net 4.5 GridView on-board functionality?

I would like to insert a database entry by using standard functions of my GridView and SqlDataSource. Has there been any update on this topic since the dirty workarounds some versions ago (e.g. see here, here)? I could not find any new tutorials on the web.
For example, is there any handler that allows the entry to be written to the database by using the standard -Datafield and the UpdateCommand of the SqlDataSource? At the moment, the UpdateCommand does not seem to get my entered value.
How can I debug those SqlDataSource-Database-Queries properly?
Heres the solution I've tried to get it working with: My database table tblname contains only one column, "name".
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:VConStr %>"
DeleteCommand="DELETE FROM [tblname] WHERE [name] = #name"
InsertCommand="INSERT INTO [tblname] ([name]) VALUES (#name)"
SelectCommand="SELECT [name] FROM [tblname]"
UpdateCommand="UPDATE [tblname] SET [name] = #name WHERE [name] = #name">
<DeleteParameters><asp:Parameter Name="name" Type="String" /></DeleteParameters>
<InsertParameters><asp:Parameter Name="name" Type="String" /></InsertParameters>
<UpdateParameters><asp:Parameter Name="name" Type="String" /></UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="name" DataSourceID="SqlDataSource1" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="Name" ShowHeader="True">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="Insertname" name="name_new" runat="server" Text='<%# Bind("name") %>' />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="True">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Button4" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Sure?');" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Button2" runat="server" Text="Edit entry" CommandName="Update" />
<asp:Button ID="Button3" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="Button5" runat="server" Text="New" CommandName="Insert" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Exception handling in WebForms

I have a webform with a formview
<asp:FormView ID="formViewBrouwers" runat="server" AllowPaging="True"
DataKeyNames="BrouwerNr" DataSourceID="brouwerDataSource"
onitemupdated="formViewBrouwers_ItemUpdated"
onitemupdating="formViewBrouwers_ItemUpdating"
oniteminserted="formViewBrouwers_ItemInserted"
oniteminserting="formViewBrouwers_ItemInserting">
<EditItemTemplate>
BrouwerNr:
<asp:Label ID="BrouwerNrLabel1" runat="server"
Text='<%# Eval("BrouwerNr") %>' />
<br />
BrNaam:
<asp:TextBox ID="BrNaamTextBox" runat="server" Text='<%# Bind("BrNaam") %>' />
<br />
Adres:
<asp:TextBox ID="AdresTextBox" runat="server" Text='<%# Bind("Adres") %>' />
<br />
Postcode:
<asp:TextBox ID="PostcodeTextBox" runat="server"
Text='<%# Bind("Postcode") %>' />
<br />
Gemeente:
<asp:TextBox ID="GemeenteTextBox" runat="server"
Text='<%# Bind("Gemeente") %>' />
<br />
Omzet:
<asp:TextBox ID="OmzetTextBox" runat="server" Text='<%# Bind("Omzet") %>' />
<br />
Status:
<asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
BrNaam:
<asp:TextBox ID="BrNaamTextBox" runat="server" Text='<%# Bind("BrNaam") %>' />
<br />
Adres:
<asp:TextBox ID="AdresTextBox" runat="server" Text='<%# Bind("Adres") %>' />
<br />
Postcode:
<asp:TextBox ID="PostcodeTextBox" runat="server"
Text='<%# Bind("Postcode") %>' />
<br />
Gemeente:
<asp:TextBox ID="GemeenteTextBox" runat="server"
Text='<%# Bind("Gemeente") %>' />
<br />
Omzet:
<asp:TextBox ID="OmzetTextBox" runat="server" Text='<%# Bind("Omzet") %>' />
<br />
Status:
<asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
BrouwerNr:
<asp:Label ID="BrouwerNrLabel" runat="server" Text='<%# Eval("BrouwerNr") %>' />
<br />
BrNaam:
<asp:Label ID="BrNaamLabel" runat="server" Text='<%# Bind("BrNaam") %>' />
<br />
Adres:
<asp:Label ID="AdresLabel" runat="server" Text='<%# Bind("Adres") %>' />
<br />
Postcode:
<asp:Label ID="PostcodeLabel" runat="server" Text='<%# Bind("Postcode") %>' />
<br />
Gemeente:
<asp:Label ID="GemeenteLabel" runat="server" Text='<%# Bind("Gemeente") %>' />
<br />
Omzet:
<asp:Label ID="OmzetLabel" runat="server" Text='<%# Bind("Omzet") %>' />
<br />
Status:
<asp:Label ID="StatusLabel" runat="server" Text='<%# Bind("Status") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
<PagerSettings Mode="NextPreviousFirstLast" />
</asp:FormView>
In my property Postcode I check the value like this:
private Int16 postcodeValue;
public Int16 Postcode
{
get
{
return postcodeValue;
}
set
{
if (value < 1000 || value > 9999)
{
throw new Exception("Postcode moet tussen 1000 en 9999 liggen");
}
else
{
postcodeValue = value;
}
}
}
How can I handle the exception I threw? If there is an exception I want a label to appear with the following exception?
You can associate the PostCode textbox in InsertItemTemplate and EditItemTemplate with a RangeValidator and have a label beside that shows the error message when the condition fails.
Maybe you should use a derived exception type, like ArgumentException or a custom class, something like PostCodeException.
This way you can catch the correct exception type and react accordingly.
This article may be useful: http://msdn.microsoft.com/en-us/library/ms173160(v=VS.100).aspx
You could use a RegularExpressionValidator or RangeValidator that would validate the value on the client side before submitting it to the server.
This '^[1-9]{1}[0-9]{3} ?[A-Z]{2}$' would be a valid regex to check a Dutch postal code.
Or instead of throwing an exception in your property setter, you could show a label that is normally hidden in your markup.
If you really want to throw an exception have a look at the Page_Error event. You can use that to catch all exceptions in your page and handle them if appropriately.

Categories

Resources