如何避免Silverlight中重复的用户ID [英] How to avoid duplicate userid in silverlight

查看:67
本文介绍了如何避免Silverlight中重复的用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,正在使用SQL Server 2008开发Silverlight导航应用程序.

我有一个注册"表,最初存储了用户的会员资格请求.管理员将对其进行验证,然后将特定的用户详细信息从注册"移至成员"表.此外,当成员资格的期限/期限结束时,相同的详细信息最终将从成员"表移至过去成员"表.

现在我想要的是,在提交注册详细信息之前,应该在所有三个表(Registration,Members,PastMembers)中检查是否指定了用户名,如果找到任何记录,则应通过提交详细信息来限制用户,以避免重复的用户名.


我搜索并尝试对行进行计数,但是即使我有一条具有相同用户名的记录,我也得到count = 0

请让我知道在此逻辑中是否还有其他更好的选择或更正(由于我是初学者,请更简短)


仅签入第一张桌子

提交按钮单击处理程序中的代码

I am beginner and developing a silverlight navigation application with SQL Server 2008.

I have a ''Registration'' table where initially users request for membership are stored. The admins will verify them and later the particular user details are moved to ''Members'' table from ''Registration''. Moreover when the term/period of membership comes to end the same details are finally moved to ''PastMembers'' table from ''Members'' table.

Now what I wanted is that before the registration details are submitted it should be checked in all three tables(Registration,Members,PastMembers) for username specified and if any record is found it should restrict user by submitting details to avoid duplicate userids.


I searched and tried to count the rows but even though I have a record with same userid I get count=0

Please let me know if any other better option or any rectification in this logic (please be a more brief since I am a beginner)


Checking in just first table

Code in submit button click handler

myDomainContext objctx1 = new myDomainContext();
var query1 = objctx1.GetregistrationsByIDQuery(userid_txtbx.Text);
objctx1.Load(query1);
var count = (from c in objctx1.registrations where c.userid == userid_txtbx.Text select c).Count();
// To see how many rows there
MessageBox.Show(count.ToString());
// Code for restricting details to be submitted




myDomainService.cs中的函数




Function in myDomainService.cs

// Query to get usernames from registrations table
   public IQueryable<registration> GetregistrationsByID(string id)
   {
           return this.ObjectContext.registrations.Where(s => s.userid == id);
   }





将表中的示例字段视为:

表格:注册,会员,具有
的过去会员
用户名,
全名,
contact





consider sample fields in tables as:

Tables: Registration,Members,PastMembers having

userid,
fullname,
contact

推荐答案

您好,维克拉姆,您的位置正确.从我所看到的来看,您有点像在做两次相同的事情.有两行没有用,但也许您放了它们是因为以后要用?

这是您需要计数的最低要求:

Hi Vikram, looks like you''re on the right lines. From what I can see you''re kind of doing the same thing twice. There are two lines which aren''t used, but maybe you''ve put them because they''re used later?

This is the bare minimum you need to get a count:

var objctx1 = new myDomainContext();
var count = (from c in objctx1.registrations where c.userid == userid_txtbx.Text select c).Count();
MessageBox.Show(count.ToString());



还是点语法中的同一件事...



Or the same thing in dot syntax...

var objctx1 = new myDomainContext();
int count = objctx1.registrations.Where(i => i.userid == userid_txtbx.Text).Count();
MessageBox.Show(count.ToString())



您的query1位也可以.您可以编写一个根据用户ID是否已被接受而给出true/false的方法,如下所示:



Your query1 bit is also fine. You could write a method which gives a true/false based on whether the userid is already taken like this:

bool IsUserIdTaken(string userid)
{
    var objctx1 = new myDomainContext();
    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();
    return hasReg || hasMem || hasPst;
}



然后,您可以轻松地将其与类似以下内容的内容一起使用:



Which you can then use easily with something like:

if(IsUserIdTaken(userid))
{
    // The userid already exists (it is taken)
    // Add whatever code you like here
}
else
{
    // The userid does not yet exist (it is available)
    // Add whatever code you like here
}



希望对您有所帮助.



Hope that helps.


好吧,我将逐步介绍为演示Silverlight导航应用程序执行的过程,该应用程序是在遇到类似问题的地方创建的.



1)使用VS 2010创建了Silverlight导航应用程序.

姓名:会员

http://i48.tinypic.com/21ep0d2.png [
现在,在同一数据库中,我添加了三个表:注册,成员,过去成员

这是每个脚本的SQL脚本



-用于注册的SQL脚本如下:
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

/****** Object:  Table [dbo].[registrations] ******/
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脚本如下:





-SQL Script for members is as follows:

USE [mydb]
GO

/****** Object:  Table [dbo].[members] ******/
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脚本如下:





-SQL Script for pastmembers is as follows:

USE [mydb]
GO

/****** Object:  Table [dbo].[pastmembers]  ******/
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)现在,我将仅使用相同的SQL Server Management Studio在成员表中添加一行,以便仅使用在应用程序中创建的一页来演示该问题.

好的,所以成员表中的新行为:

用户ID 全名 联系人
abc@abc.com 先生. ABC 9876543210



4)回到VS2010.现在,我添加了ADO.NET实体数据模型.名称: mydbModel.edmx

http://i49.tinypic.com/nohgn7.png [ http://i45.tinypic.com/n6zuc9.png [ http://i45.tinypic.com/21cdsu1.png [ http://i45.tinypic.com/11uyp0j.png [
这是所有这些控件的xaml代码.





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>




现在为应用程序提供构建过程,并在Home.xaml.cs文件中添加代码

最初添加名称空间:




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();
            // To see how many rows there (It shows zero always even we have record with userid as 'abc@abc.com' )
            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(ex.ToString());
                    MessageBox.Show("Unexpected error occured! Please try again.");
                }

            }
        }



现在运行应用程序,尝试输入相同的用户ID,即"abc@abc.com"和其他详细信息,然后单击提交.记录被保存,甚至重复的值在成员表中也可用.



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.


这篇关于如何避免Silverlight中重复的用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆