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.Data;
public partial class _Default : System.Web.UI.Page 
{   

    string Constr = "Data Source=.;Initial Catalog=Web;Integrated Security=True;Pooling=False";
    private bool checkedfield1(int num)
    {
        bool noError = true;
        switch (num.ToString())
        {
            case "1":

                if (TxtFName.Text == "" || TxtPhone.Text == "" || TxtAddress.Text == "")
                {
                    Label1.Text = "فیلدهای مشخص شده را پر کنید";
                    noError = false;
                }
                break;
            case "2":

                if (TxtFName.Text == "")
                {
                    Label1.Text = "فیلد نام را پر کنید";
                    noError = false;
                }
                break;

            case "3":

                if (TxtId.Text == "")
                {
                    Label1.Text = "فیلد شماره شناسایی را پر کنید";
                    noError = false;
                }
                break;

            default:
                break;
        }

        return noError;
    }

    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        bool noError = checkedfield1(1);
        if (noError)
        {
            SqlConnection conn = new SqlConnection(Constr);
            if (TxtId.Text.Contains("'"))
            {
                Label1.Text = "کاراکتر ' نا معتبر است";
                return;
            }
            string query = "Insert Into WebPhone (id , Fname , LName , Phone , Mobile , Address) Values ('" + TxtId.Text.Trim() + "','" + TxtFName.Text.Trim() + "','" + TxtLName.Text.Trim() + "','" + TxtPhone.Text.Trim() + "','" + TxtMobile.Text.Trim() + "','" + TxtAddress.Text.Trim() + "')";
            conn.Open();
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            Label1.Text = "اطلاعات با موفقیت ثبت شد";
            TxtAddress.Text = "";
            TxtFName.Text = "";
            TxtLName.Text = "";
            TxtMobile.Text = "";
            TxtPhone.Text = "";
        }
    }
    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        bool noError = checkedfield1(2);
        if (noError)
        {
            SqlConnection conn = new SqlConnection(Constr);
            string query = "Select * from Webphone where fname = '" + TxtFName.Text.Trim() + "'";
            SqlCommand cmd = new SqlCommand(query, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            Response.Write("<table align = Center border = 2 >");
            Response.Write("<tr>");
            Response.Write("<td>" + "شماره شناسایی " + "</td>");
            Response.Write("<td>" + "نام " + "</td>");
            Response.Write("<td>" + "نام خانوادگی" + "</td>");
            Response.Write("<td>" + "تلفن ثابت" + "</td>");
            Response.Write("<td>" + "تلفن همراه" + "</td>");
            Response.Write("<td>" + "آدرس" + "</td>");
            Response.Write("</tr>");
            while (dr.Read())
            {
                Response.Write("<tr>");
                Response.Write("<td>" + dr["id"].ToString() + "</td>");
                Response.Write("<td>" + dr["fname"].ToString() + "</td>");
                Response.Write("<td>" + dr["lname"].ToString() + "</td>");
                Response.Write("<td>" + dr["phone"].ToString() + "</td>");
                Response.Write("<td>" + dr["Mobile"].ToString() + "</td>");
                Response.Write("<td>" + dr["Address"].ToString() + "</td>");
                Response.Write("</tr>");
            }
            Response.Write("</table>");
            Label1.Text = "اطلاعات با موفقیت بازیابی شد";
            conn.Close();
        }
        TxtLName.Text = "";
        TxtPhone.Text = "";
        TxtMobile.Text = "";
        TxtAddress.Text = "";
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        bool noError = checkedfield1(2);
        if (noError)
        {
            BtnSearch_Click(sender, e);
            SqlConnection conn = new SqlConnection(Constr);
            string query = "Update Webphone Set fname = '" + TxtFName.Text.Trim() + "', lname = '" + TxtLName.Text.Trim() + "', phone ='" + TxtPhone.Text.Trim() + "', Mobile ='" + TxtMobile.Text.Trim() + "', Address = '" + TxtAddress.Text.Trim() + "' where id = '" + TxtId.Text.Trim() + "'";
            conn.Open();
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.ExecuteNonQuery();
            Label1.Text = "ویرایش اطلاعات با موفقیت انجام شد";
            conn.Close();
        }
        TxtFName.Text = "";
        TxtLName.Text = "";
        TxtPhone.Text = "";
        TxtMobile.Text = "";
        TxtAddress.Text = "";
    }
    protected void BtnDelete_Click(object sender, EventArgs e)
    {
        bool noError = checkedfield1(3);
        if (noError)
        {
            SqlConnection conn = new SqlConnection(Constr);
            string query = "Delete from Webphone where id = '" + TxtId.Text.Trim() + "'";
            conn.Open();
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.ExecuteNonQuery();
            Label1.Text = "حذف اطلاعات با موفقیت انجام شد";
            conn.Close();
        }
        TxtFName.Text = "";
        TxtLName.Text = "";
        TxtPhone.Text = "";
        TxtMobile.Text = "";
        TxtAddress.Text = "";
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.PageIndex = GridView1.SelectedIndex;
        FormView1.PageIndex = GridView1.SelectedIndex;
    }
    protected void BtnList_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(Constr);
        string query = "Select * From Webphone";
        SqlDataAdapter da = new SqlDataAdapter(query, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView2.DataSource = ds.Tables[0];
        GridView2.DataBind();
        Label1.Text = "لیست کردن اطلاعات با موفقیت انجام شد";
        
    }
}