How to make gridview editable in asp.net c#

Today, I am going to show you "how to create editable gridview in asp.net" and we will implement editable column ingridview in asp.net using c#. To make editable gridview you need to add <EditItemTemplate>  under <asp:TemplateField>.
Also we see  step by step insert delete update in gridview using c#. All right let's get start.

How to make gridview editable in asp.net using c#


First of you need add asp.net webform , here I am going to add a webform with the of emp.aspx.

Step 1: Configure the connectionstring in web.cofig file.


<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
<connectionStrings>
  <add name="Mycon" connectionString="initial catalog=jqueryclass; integrated security=true; data source=SANJAY\SQL2012;"/>
 
</connectionStrings>
    <system.web>
      <compilation debug="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
    </system.web>
</configuration>


Also Read: Top 10 Web Config Interview Question and Answers


Step 2: Add asp.net webform with name emp.aspx.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="emp.aspx.cs" Inherits="emp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
           
            <asp:GridView ID="grdview" runat="server"
                OnRowEditing="grdview_RowEditing" DataKeyNames="eid" ShowFooter="true" OnRowUpdating="grdview_RowUpdating"
                OnRowDeleting="grdview_RowDeleting" OnRowCancelingEdit="grdview_RowCancelingEdit"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="Sr No.">
                        <ItemTemplate>
                            <%# grdview.Rows.Count + 1  %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField Visible="false">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblid" Text=' <%#Bind("eid") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>Emp name</HeaderTemplate>
                        <ItemTemplate><%#Eval("ename") %></ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="ftxtname" runat="server"></asp:TextBox>
                        </FooterTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtnameft" runat="server" Text='<%#Eval("ename") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>Emp Address</HeaderTemplate>
                        <ItemTemplate>
                            <%#Eval("address") %>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="ftxtaddress" runat="server"></asp:TextBox>
                        </FooterTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtaddressft" runat="server" Text='<%#Eval("address") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>Emp Age</HeaderTemplate>
                        <ItemTemplate>
                            <%#Eval("age") %>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="ftxtage" runat="server"></asp:TextBox>
                        </FooterTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtageft" runat="server" Text='<%#Eval("age") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>Status</HeaderTemplate>
                        <ItemTemplate>
                            <%# Eval("IsActive").ToString()=="1"? "Active":"No Active" %>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:CheckBox ID="ftxtisactive" runat="server"></asp:CheckBox>
                        </FooterTemplate>
                        <EditItemTemplate>
                            <asp:CheckBox ID="txtisactiveft" runat="server" Text='<%# Eval("IsActive").ToString()=="1"?"Active":"No Active" %>'></asp:CheckBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>operation</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Button runat="server" ID="btnedit" Text="edit" CommandName="edit" />
                            <asp:Button runat="server" ID="btndelete" Text="delete" CommandName="delete" />
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Button ID="btninsert" runat="server" Text="insert" OnClick="btninsert_Click" />
                        </FooterTemplate>
                        <EditItemTemplate>
                            <asp:Button runat="server" ID="btnupdate" Text="update" CommandName="update" />
                            <asp:Button runat="server" ID="btncancel" Text="cancel" CommandName="cancel" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>No Record Available Insert Record</EmptyDataTemplate>
            </asp:GridView>
        </div>
    </form>
</body>
</html>
Step 2: Copy and paste below code in your codebehind page(emp.aspx.cs)


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;

public partial class emp : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString);
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
  
    public void BindData()
    {
        con.Open();
        cmd = new SqlCommand("getday1", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grdview.DataSource = ds;
        grdview.DataBind();
        con.Close();
    }
    public void EditData()
    {
        con.Open();
        cmd = new SqlCommand("getday1", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grdview.DataSource = ds;
        grdview.DataBind();
        con.Close();
    }
    protected void grdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from emp where eid='" + Convert.ToInt32(grdview.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {

            Response.Write("<script> alert('Deleted'); </script>");
        }
        con.Close();
        BindData();
    }
    protected void btninsert_Click(object sender, EventArgs e)
    {
        int p;
        TextBox txt_name = grdview.FooterRow.FindControl("ftxtname") as TextBox;
        TextBox txt_adress = grdview.FooterRow.FindControl("ftxtaddress") as TextBox;
        TextBox txt_age = grdview.FooterRow.FindControl("ftxtage") as TextBox;
        CheckBox txt_isactive = grdview.FooterRow.FindControl("ftxtisactive") as CheckBox;

        if (txt_isactive.Checked == true)
        {
            p = 1;
        }
        else
        {
            p = 0;
        }

        con.Open();
        SqlCommand cmd = new SqlCommand("day1emp", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("ename", txt_name.Text);
        cmd.Parameters.AddWithValue("address", txt_adress.Text);
        cmd.Parameters.AddWithValue("age", txt_age.Text);
        cmd.Parameters.AddWithValue("IsActive", p);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Response.Write("<script> alert('Inserted'); </script>");
        }
        con.Close();
        BindData();

    }
    protected void grdview_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdview.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void grdview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdview.EditIndex = -1;
        BindData();
    }

    protected void grdview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lbldlt = grdview.Rows[e.RowIndex].FindControl("lblid") as Label;
        TextBox txt_fname = grdview.Rows[e.RowIndex].FindControl("txtnameft") as TextBox;
        TextBox txt_fadress = grdview.Rows[e.RowIndex].FindControl("txtaddressft") as TextBox;
        TextBox txt_fage = grdview.Rows[e.RowIndex].FindControl("txtageft") as TextBox;
        CheckBox txt_fisactive = grdview.Rows[e.RowIndex].FindControl("txtisactiveft") as CheckBox;
        int isactive;

        if (txt_fisactive.Checked == true)
        {
            isactive = 1;
        }
        else
        {
            isactive = 0;
        }

        con.Open();
        SqlCommand cmd = new SqlCommand("day1emp_update", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@eid", lbldlt.Text);
        cmd.Parameters.AddWithValue("@ename", txt_fname.Text);
        cmd.Parameters.AddWithValue("@address", txt_fadress.Text);
        cmd.Parameters.AddWithValue("@age", txt_fage.Text);
        cmd.Parameters.AddWithValue("@IsActive", isactive);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {

            Response.Write("<script> alert('Updated'); </script>");
        }
        con.Close();
        BindData();

    }

}

TAG:how to create editable gridview in asp.net
Read More

[Source Code ] Asp.net code to send email (send mail with cc in asp.net)

Introduction

Asp.net code to send email


In this tutorial, I will show "Asp.net code to send email". The whole process will be done in the following steps. We will also see "how to send mail with CC in asp.net c#".


how to send mail with cc in asp.net, how to send simple mail in asp.net
Asp.net code to send email


1. Create an object of MailAddress class(For CC).
2. Create an object of MailMessage class(For Message).
3. Create an object of SmtpClient class.


how to send mail with CC in asp.net c#
send mail with CC in asp.net c#


.Aspx Page

<table>
        <tr>
            <td>From Email ID :</td>
            <td><asp:TextBox ID="txtfromemail" runat="server" Width="250px"></asp:TextBox></td>
        </tr>
          <tr>
            <td>Password :</td>
            <td><asp:TextBox ID="txtpassword" runat="server" Width="250px" TextMode="Password"></asp:TextBox></td>
        </tr>
        <tr>
            <td>To Email ID :</td>
            <td><asp:TextBox ID="txttoemail" runat="server" Width="250px"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Subject :</td>
            <td><asp:TextBox ID="txtsubject" runat="server" Width="250px"></asp:TextBox></td>
        </tr>
        <tr>
            <td style="vertical-align:top">Body :</td>
            <td><asp:TextBox ID="txtbody" runat="server" TextMode="MultiLine" Rows="8" Columns="50"></asp:TextBox> </td>
        </tr>
      
        <tr>
            <td></td>
            <td><asp:Button ID="btn_SendMail" Text="Mail Send" runat="server" OnClick="btn_SendMail_Click" /></td>
        </tr>
    </table>


.Cs page Source Code


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.IO;
using System.Configuration;
using System.Net;
using System.Net.Mail;

namespace DotNet
{
    public partial class Email_Send : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_SendMail_Click(object sender, EventArgs e)
        {
            MailAddress bcc = new MailAddress("[email protected]");
            using (MailMessage mm = new MailMessage(txtfromemail.Text, txttoemail.Text))
            {
                mm.Subject = txtsubject.Text;
                mm.Body = txtbody.Text;
                mm.CC.Add(bcc);
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(txtfromemail.Text, txtpassword.Text);
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 25;
                smtp.Send(mm);
                ClientScript.RegisterStartupScript(GetType(), "alert""alert('Email sent.');"true);
            }
        }
    }
}


After done all these steps, may you face a problem like this "Authentication the Server Response was 5.5.1 Authentication required in Gmail"? In this case, follow the given steps.


1. Login to your Gmail account.
2. Visit this page https://accounts.google.com/DisplayUnlockCaptcha and click on button to allow access.
3. Visit this page https://www.google.com/settings/security/lesssecureapps and enable access for less secure apps.


Read our previous post



Read More