using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using Xrm;


namespace ToolsCRM
{
    public partial class register : System.Web.UI.Page
    {
        public int check = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                var xrm = new XrmServiceContext("Xrm");
                var query =
                 (from u in xrm.new_uzytkownicySet
                  where u.new_name == TextBoxLogin.Text
                  select u).ToList();
                int result = query.Count();
                              
              
                if (result == 1)
                {
                    this.check = 1;
                   
                }
            }
        }

        protected void Rejestruj_Click(object sender, EventArgs e)
        {
            var xrm = new XrmServiceContext("Xrm");
              
            if (this.check == 0)
            {
                var uzytkownik = new Xrm.new_uzytkownicy
                {
                    new_imie = TextBoxImie.Text,
                    new_nazwisko = TextBoxNazw.Text,
                    new_name = TextBoxLogin.Text,
                    new_haslo = TextBoxPass.Text
                };
                try
                {
                    xrm.AddObject(uzytkownik);
                    xrm.SaveChanges();         
                    Response.Redirect("Index.aspx");
                }
                catch
                {
                    Response.Write("Error");
                }


            }
        }
    }
}