Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
           



        }
    }
}




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");
            }

            
        }
    }
}
Posted
Updated 13-Oct-11 2:52am
v2

1 solution

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

C#
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
 
Share this answer
 
v2
Comments
masoud_sedighy 14-Oct-11 12:27pm    
Hello

Thanks, your solution works. Also I had mistake in "customer" class. Because when I renamed that to "Customer" that worked for me. It seems caps lock "c" is important .

Thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900