using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Web.Services;
using Newtonsoft.Json;
using System.Web.Script.Serialization;

public partial class module_Registration4 : System.Web.UI.Page
{
    public static string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    public static MySqlConnection con = new MySqlConnection(constring);
    public static DateTime date1;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]
    public static string savedata(string uhid, string pfname, string pmname, string plname, string ppname, string pmoname, string pcountry, string pstate, string pcity, string paddress, string praddress, string ppaddress, string ppin, string pcastcate, string pbgroup, string pgender, string page, string pmob1, string pmob2, string pemail, string pclass, string pdivision, string pquali, string padhar, string pvoting, string pbplcard, string pscheme, string pcourse, string pstatus, string pisprovisional)
    {
        string data = "1";
        //if (dob != "")
        //{
        //    date1 = DateTime.Parse(dob);
        //}
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            string query = "Insert into spersonaldetail(uhid,pfname,pmname,plname,ppname,pmoname,pcountry,pstate,pcity,paddress,praddress,ppaddress,ppin,pcastcate,pbgroup,pgender,page,pmob1,pmob2,pemail,pclass,pdivision,pquali,padhar,pvoting,pbplcard,pscheme,pcourse,pstatus,pisprovisional) values(@uhid,@pfname,@pmname,@plname,@ppname,@pmoname,@pcountry,@pstate,@pcity,@paddress,@praddress,@ppaddress,@ppin,@pcastcate,@pbgroup,@pgender,@page,@pmob1,@pmob2,@pemail,@pclass,@pdivision,@pquali,@padhar,@pvoting,@pbplcard,@pscheme,@pcourse,@pstatus,@pisprovisional)";

            using (MySqlCommand cmd = new MySqlCommand(query, con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                cmd.Parameters.AddWithValue("@uhid", uhid);
                cmd.Parameters.AddWithValue("@pfname", pfname);
                cmd.Parameters.AddWithValue("@pmname", pmname);
                cmd.Parameters.AddWithValue("@plname", plname);
                cmd.Parameters.AddWithValue("@ppname", ppname);
                cmd.Parameters.AddWithValue("@pmoname", pmoname);
                cmd.Parameters.AddWithValue("@pcountry", pcountry);
                cmd.Parameters.AddWithValue("@pstate", pstate);
                cmd.Parameters.AddWithValue("@pcity", pcity);
                cmd.Parameters.AddWithValue("@paddress", paddress);
                cmd.Parameters.AddWithValue("@praddress", praddress);
                cmd.Parameters.AddWithValue("@ppaddress", ppaddress);
                cmd.Parameters.AddWithValue("@ppin", ppin);
                cmd.Parameters.AddWithValue("@pcastcate", pcastcate);
                cmd.Parameters.AddWithValue("@pbgroup", pbgroup);
                cmd.Parameters.AddWithValue("@pgender", pgender);
                //cmd.Parameters.AddWithValue("@dob", date1.ToString("yyyy/MM/dd"));

                cmd.Parameters.AddWithValue("@page", page);
                cmd.Parameters.AddWithValue("@pmob1", pmob1);
                cmd.Parameters.AddWithValue("@pmob2", pmob2);
                cmd.Parameters.AddWithValue("@pemail", pemail);
                cmd.Parameters.AddWithValue("@pclass", pclass);
                cmd.Parameters.AddWithValue("@pdivision", pdivision);
                cmd.Parameters.AddWithValue("@pquali", pquali);
                cmd.Parameters.AddWithValue("@padhar", padhar);
                cmd.Parameters.AddWithValue("@pvoting", pvoting);
                cmd.Parameters.AddWithValue("@pbplcard", pbplcard);
                cmd.Parameters.AddWithValue("@pscheme", pscheme);
                cmd.Parameters.AddWithValue("@pcourse", pcourse);
                cmd.Parameters.AddWithValue("@pstatus", pstatus);
                cmd.Parameters.AddWithValue("@pisprovisional", pisprovisional);
                cmd.ExecuteNonQuery();
                con.Close();
                con.Dispose();
            }
        }
        return data;
    }
    [WebMethod]
    public static object GetData()
    {
        string data = " ";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            using (MySqlCommand cmd = new MySqlCommand("select * from  spersonaldetail", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                DataSet ds = new DataSet();
                using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        data = JsonConvert.SerializeObject(ds.Tables[0]);
                    }
                    con.Close();
                    con.Dispose();
                    cmd.Dispose();
                }
            }
        }
        return data;
    }
    [WebMethod]
    public static object EditData(string id)
    {
        string data = "";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            using (MySqlCommand cmd = new MySqlCommand("select * from spersonaldetail where id='" + id + "'", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        data = JsonConvert.SerializeObject(ds.Tables[0]);
                    }
                    cmd.ExecuteNonQuery();
                    con.Close();
                    con.Dispose();
                    cmd.Dispose();

                }
            }
        }
        return data;
    }

    [WebMethod]
    public static object UpdateData(string id, string pfname, string pmname, string plname, string ppname, string pmoname, string pcountry, string pstate, string pcity, string paddress, string praddress, string ppaddress, string ppin, string pcastcate, string pbgroup, string pgender, string page, string pmob1, string pmob2, string pemail, string pclass, string pdivision, string pquali, string padhar, string pvoting, string pbplcard, string pscheme, string pcourse, string pstatus, string pisprovisional)
    {
        string data = "";
        //if (dob != "")
        //{
        //    date1 = DateTime.Parse(dob);
        //}
        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
        {
            using (MySqlCommand cmd = new MySqlCommand("update spersonaldetail set pfname= @pfname,pmname= @pmname,plname= @plname,ppname=@ppname,pmoname=@pmoname,pcountry=@pcountry,pstate=@pstate,pcity=@pcity,paddress=@paddress,ppaddress=@ppaddress,ppaddress=@ppaddress,ppin=@ppin,pcastcate=@pcastcate,pbgroup=@pbgroup,pgender=@pgender,page=@page,pmob1=@pmob1,pmob2=@pmob2,pemail=@pemail,pclass=@pclass,pdivision=@pdivision,pquali=@pquali,padhar=@padhar,pvoting=@pvoting,pbplcard=@pbplcard,pscheme=@pscheme,pcourse=@pcourse,pstatus=@pstatus,pisprovisional=@pisprovisional  where id=@id", con))

            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                cmd.Parameters.AddWithValue("@id", id);
                cmd.Parameters.AddWithValue("@pfname", pfname);
                cmd.Parameters.AddWithValue("@pmname", pmname);
                cmd.Parameters.AddWithValue("@plname", plname);
                cmd.Parameters.AddWithValue("@ppname", ppname);
                cmd.Parameters.AddWithValue("@pmoname", pmoname);
                cmd.Parameters.AddWithValue("@pcountry", pcountry);
                cmd.Parameters.AddWithValue("@pstate", pstate);
                cmd.Parameters.AddWithValue("@pcity", pcity);
                cmd.Parameters.AddWithValue("@paddress", paddress);
                cmd.Parameters.AddWithValue("@praddress", praddress);
                cmd.Parameters.AddWithValue("@ppaddress", ppaddress);
                cmd.Parameters.AddWithValue("@ppin", ppin);
                cmd.Parameters.AddWithValue("@pcastcate", pcastcate);
                cmd.Parameters.AddWithValue("@pbgroup", pbgroup);
                cmd.Parameters.AddWithValue("@pgender", pgender);
                //cmd.Parameters.AddWithValue("@dob", date1.ToString("yyyy/MM/dd"));

                cmd.Parameters.AddWithValue("@page", page);
                cmd.Parameters.AddWithValue("@pmob1", pmob1);
                cmd.Parameters.AddWithValue("@pmob2", pmob2);
                cmd.Parameters.AddWithValue("@pemail", pemail);
                cmd.Parameters.AddWithValue("@pclass", pclass);
                cmd.Parameters.AddWithValue("@pdivision", pdivision);
                cmd.Parameters.AddWithValue("@pquali", pquali);
                cmd.Parameters.AddWithValue("@padhar", padhar);
                cmd.Parameters.AddWithValue("@pvoting", pvoting);
                cmd.Parameters.AddWithValue("@pbplcard", pbplcard);
                cmd.Parameters.AddWithValue("@pscheme", pscheme);
                cmd.Parameters.AddWithValue("@pcourse", pcourse);
                cmd.Parameters.AddWithValue("@pstatus", pstatus);
                cmd.Parameters.AddWithValue("@pisprovisional", pisprovisional);
                cmd.ExecuteNonQuery();
                cmd.Connection = con;
                con.Close();
                con.Dispose();
                cmd.Dispose();
            }
        }
        return data;
    }
 
    [WebMethod]
    public static object DeleteData(string id)
    {
        string data = "";
        using (MySqlConnection con = new MySqlConnection(constring))
        {
            if (con.State != ConnectionState.Open) { con.Open(); }
            using (MySqlCommand cmd = new MySqlCommand("delete from spersonaldetail where id='" + id + "'", con))
            {

                cmd.ExecuteNonQuery();
                using (MySqlDataAdapter dr = new MySqlDataAdapter(cmd))
                {
                    con.Close();
                    con.Dispose();
                    cmd.Dispose();

                }
            }
        }
        return data;
    }
    [WebMethod]
    public static string GetEmpData()
    {
        string _data = "";
        string BookingID;
        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
        {
            string query = "select id from spersonaldetail order by id Desc";
            if (con.State != ConnectionState.Open) { con.Open(); }
            using (MySqlCommand cmd = new MySqlCommand(query, con))
            {
                MySqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    int uhid = Convert.ToInt32((dr[0].ToString())) + 1;
                    BookingID = uhid.ToString("000");

                }
                else if (Convert.IsDBNull(dr))
                {
                    BookingID = ("001");

                }
                else
                {

                    BookingID = ("001");
                }
                dr.Close();
                cmd.Dispose();
                con.Close();

                _data = JsonConvert.SerializeObject(BookingID);
            }
        }
        return _data;
    }
}