找不到用于实现部分方法声明的定义声明 [英] No defining declaration found for implementing declaration of partial method
问题描述
你好
我是linq的新手,并且正在使用"LINQ To Sql"教程系列,因此我使用了以下代码来更新
的电话号码
客户表(示例1),然后添加一个类并将其命名为"customer.cs"(示例2)
用于防止将虚假电话号码添加到我们的数据库中.
现在,当我调试程序时,出现如下错误(示例3),
请帮忙,实际上是什么问题,我不知道如何定义局部类
我只知道该类应与northwind.desiner.cs中的客户"一起使用.
在此先谢谢您.
范例1:
Hello
I am new to linq and I am using ‘LINQ To Sql’ Tutorial Series , I used the code like below for updating the phone number of
customers table (sample 1) and then add a class and named it "customer.cs" (sample 2)
for preventing bogus phone numbers from being added into our database.
Now when I debug the program I take the error like below (sample 3) ,
please help what is the problem actually I don''t know how I have to define partial class
just I know this class should works with "customer" in northwind.desiner.cs.
Thanks in advanced.
Sample 1:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
NorthwindDataContext northwind=new NorthwindDataContext();
Customer mycustumer = northwind.Customers.Single(c => c.CompanyName == "B''s Beverages");
mycustumer.Phone = "999";
northwind.SubmitChanges();
}
}
}
示例2:
sample 2:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
namespace WebApplication1
{
public partial class customer
{
partial void onphonechanging(string value)
{
Regex phoneNumber = new Regex(@"^[2-9]\d{2}-\d{3}-\d{4}$");
if (phoneNumber.IsMatch(value) == false)
{
throw new Exception("not valid");
}
}
}
}
推荐答案
); 如果(phoneNumber.IsMatch(value)==否) { 抛出新的Exception(无效"); } } } }
"); if (phoneNumber.IsMatch(value) == false) { throw new Exception("not valid"); } } } }
只需注释部分客户,并执行以下操作即可工作
1.转到您的* .dbml文件
2.右键单击客户"类,然后选择查看代码",然后将您的部分验证类放在此处,以下是我尝试过的示例验证
Just comment your partial class customer and do the followings and should work
1.Go to your *.dbml file
2.Right click on the "customer" class and select "View Code" and put your partial validation class there, following is the sample validation i have tried
namespace CSUTOMER_PHONE_NUMBER
{
partial class Customer
{
partial void OnPhoneChanging(string value)
{
if (value.Length ==1)
throw new System.Exception ("not valid");
}
}
}
而当您设置电话号码的值时,它会起作用
and when you set the value for the phone number it works
try
{
DataClasses1DataContext db = new DataClasses1DataContext();
Customer mycustumer = db.Customers.Single(c => c.CompanyName == "B''s Beverages");
mycustumer.Phone = "1";
db.SubmitChanges();
MessageBox.Show("OK");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
希望对您有帮助
Hope this helps
这篇关于找不到用于实现部分方法声明的定义声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!