Ok I am giving the step-by-step process I performed for a demo silverlight navigation application which I created where I face similar issue.
1) Created a silverlight navigation application using VS 2010.
Name: Membership
http://i48.tinypic.com/21ep0d2.png[
^]
2) In SQL Server 2008 Managment Studio I created a database named as "mydb"
Now in the same database I add three tables : registrations, members, pastmembers
Here is the SQL script for each one
-SQL Script for registration is as follows:
USE [mydb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[registrations](
[userid] [nvarchar](50) NOT NULL,
[fullname] [nvarchar](50) NOT NULL,
[contact] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_registration] PRIMARY KEY CLUSTERED
(
[userid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-SQL Script for members is as follows:
USE [mydb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[members](
[userid] [nvarchar](50) NOT NULL,
[fullname] [nvarchar](50) NOT NULL,
[contact] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_members] PRIMARY KEY CLUSTERED
(
[userid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-SQL Script for pastmembers is as follows:
USE [mydb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[pastmembers](
[userid] [nvarchar](50) NOT NULL,
[fullname] [nvarchar](50) NOT NULL,
[contact] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_pastmembers] PRIMARY KEY CLUSTERED
(
[userid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
3) Now I will just add a row in members table intentionally using same SQL Server Management Studio so that I can demonstrate the issue with only one page that I create in application.
Ok so the new row in members table is as:
userid | fullname | contact |
abc@abc.com | Mr. ABC | 9876543210 |
4) Coming back to VS 2010. Now I add the ADO.NET Entity Data Model. Name:
mydbModel.edmx
http://i49.tinypic.com/nohgn7.png[
^]
Select all entities and enable editing.
http://i45.tinypic.com/n6zuc9.png[
^]
Select
Generate from database option from
Entity Data Model Wizard
and click
Next in the wizard.
I then selected the database from new connection and gave test connection for confirming the connectivity and proceed to Next step in wizard.
http://i45.tinypic.com/21cdsu1.png[
^]
I selected all tables and clicked on Finish and gave build process to my application to work fine for next step.
5) Next I add DomainService. Name:
MembershipDomainService.cs
http://i45.tinypic.com/11uyp0j.png[
^]
6) Next I remove existing contents and add new controls to Home page available in Views folder.
So here is the xaml code of all those controls
<grid x:name="LayoutRoot" xmlns:x="#unknown">
<canvas name="canvas1">
<textbox canvas.left="297" canvas.top="100" height="23" name="userid_txtbx" width="120" text="" />
<textbox canvas.left="297" canvas.top="143" height="23" name="fullname_txtbx" width="120" />
<textbox canvas.left="297" canvas.top="186" height="23" name="contact_txtbx" width="120" />
<textblock canvas.left="215" canvas.top="104" height="23" name="textBlock1" text="User ID" />
<textblock canvas.left="215" canvas.top="147" height="23" name="textBlock2" text="Full Name" />
<textblock canvas.left="215" canvas.top="190" height="23" name="textBlock3" text="Contact" />
<Button Canvas.Left="280" Canvas.Top="268" Content="Submit" Height="23" Name="submit_btn" Width="75" Click="submit_btn_Click" />
</canvas>
</grid>
Now giving a build process to application and adding code in the Home.xaml.cs file
adding namespace initially:
using Membership.Web;
adding a function:
private bool IsUserIdTaken(string userid)
{
var objctx1 = new MembershipDomainContext();
bool hasReg = objctx1.registrations.Where(i => i.userid == userid_txtbx.Text).Any();
bool hasMem = objctx1.members.Where(i => i.userid == userid_txtbx.Text).Any();
bool hasPst = objctx1.pastmembers.Where(i => i.userid == userid_txtbx.Text).Any();
if (hasReg == true)
{
return hasReg;
}
else if (hasMem == true)
{
return hasMem;
}
else
{
return hasPst;
}
}
Finally adding the submit button handler:
private void submit_btn_Click(object sender, RoutedEventArgs e)
{
var objctx1 = new MembershipDomainContext();
var count = (from c in objctx1.members where c.userid == userid_txtbx.Text select c).Count();
MessageBox.Show(count.ToString());
if (IsUserIdTaken(userid_txtbx.Text.Trim()) == false)
{
registration r = new registration();
r.userid = userid_txtbx.Text.Trim();
r.fullname = fullname_txtbx.Text.Trim();
r.contact = contact_txtbx.Text.Trim();
var objctx = new MembershipDomainContext();
objctx.registrations.Add(r);
try
{
objctx.SubmitChanges();
MessageBox.Show("Records successfully added!");
}
catch (Exception ex)
{
MessageBox.Show("Unexpected error occured! Please try again.");
}
}
}
Now
Run application and try entering same userid i.e "abc@abc.com" and other details and click submit. Record is saved even duplicate value available in members table.