Insert Data in database and bind to gridview

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 System.Data.SqlClient;
using System.Configuration;

namespace Prartice1
{
    public partial class Registration : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                GetEmpDetails();
            }
        }

        private void GetEmpDetails()
        {
            DataTable dt = new DataTable();

            try
            {
                if (con.State != ConnectionState.Open)
                    con.Open();
                using (SqlCommand cmd = new SqlCommand(@"Sp_BindEmpData", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    if(dt.Rows.Count>0)
                    {
                        gvEmpData.DataSource = dt;
                        gvEmpData.DataBind();
                    }
                }
            }
            catch (Exception ex)
            {
                lblerror.Text = "Error at GetEmpDetails()" + ex.Message;
            }
            finally
            {
                if (con.State != ConnectionState.Open)
                    con.Open();
            }
        }
        protected void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                if (con.State != ConnectionState.Open)
                    con.Open();
                using (SqlCommand cmd = new SqlCommand(@"Sp_SaveEmployeeData", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Flag", "INSERT");
                    cmd.Parameters.AddWithValue("@EmpName",txtName.Text);
                    cmd.Parameters.AddWithValue("@Surname", txtSurname.Text);
                    cmd.Parameters.AddWithValue("@BirthDate",Convert.ToDateTime(txtbirthdate.Text));
                    cmd.Parameters.AddWithValue("@Emailid", txtEmailid.Text);
                    cmd.Parameters.AddWithValue("@Gender", rdgender.SelectedValue);
                 
                    if(chkisprobation.Checked==true)
                    {
                        cmd.Parameters.AddWithValue("@Isprobation",1);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@Isprobation",0);
                    }

                    int i=cmd.ExecuteNonQuery();
                    if(i>0)
                    {
                        lblsuccesfull.Text = "Save Successfull";
                    }
                }
            }
            catch (Exception ex)
            {
                lblerror.Text = "Error At btnSave_Click" + ex.Message;
            }
            finally
            {
                if (con.State != ConnectionState.Closed)
                    con.Close();
            }
        }
    }
}

Comments

Popular posts from this blog

String Program in C#

CSV using XmlNode

Extension Method & Partial Class