DATA CONTROL ( Gridview , Repeater , Formview , DataList , Detailsview , Listview )

  GRIDVIEW

SOURCE CODE :

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

            Height="279px" Width="610px">

            <Columns>

                <asp:TemplateField HeaderText="roll">

                <ItemTemplate>

                    <asp:TextBox ID="txtroll" runat="server" Text='<%# Eval("roll") %>'></asp:TextBox>

                </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="name">

                <ItemTemplate>

                    <asp:TextBox ID="txtname" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>

                </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="mobile">

                <ItemTemplate>

                    <asp:TextBox ID="txtmobile" runat="server" Text='<%# Eval("mobile") %>'></asp:TextBox>

                </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="city">

                <ItemTemplate>

                    <asp:TextBox ID="txtcity" runat="server" Text='<%# Eval("city") %>'></asp:TextBox>

                </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Submit">

                <ItemTemplate>

                    <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="submit1_click" />

                </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Update">

                <ItemTemplate>

                    <asp:Button ID="Button2" runat="server" Text="Update" OnClick="update1_click" />

                </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Delete">

                <ItemTemplate>

                    <asp:Button ID="Button3" runat="server" Text="Delete" OnClick="delete1_click" />

                </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>


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.OleDb;

using System.Data;   // microsoft access library


public partial class ZZZAccessinall_gridimpliment_ : System.Web.UI.Page

{

    OleDbConnection cn;

    OleDbCommand cm;

    OleDbDataReader dr;


    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            show();

        }

    }

    protected void show()

    {

        string connection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=E:/library.mdb";   // provider

        cn = new OleDbConnection(connection);

        cn.Open();


        string k = "select * from library";

        cm = new OleDbCommand(k, cn);

        dr = cm.ExecuteReader();

        GridView1.DataSource = dr;

        GridView1.DataBind();

        dr.Close();

    }

    protected void submit1_click(object sender, EventArgs e)

    {

        string connection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=E:/library.mdb";   // provider

        cn = new OleDbConnection(connection);

        cn.Open();


        GridViewRow r1 = ((Button)sender).NamingContainer as GridViewRow;


        TextBox txtro = (TextBox)r1.FindControl("txtroll");

        TextBox txtna = (TextBox)r1.FindControl("txtname");

        TextBox txtmob = (TextBox)r1.FindControl("txtmobile");

        TextBox txtci = (TextBox)r1.FindControl("txtcity");


        string k2 = "select count(roll)+1 from library";

        cm = new OleDbCommand(k2, cn);


        txtro.Text = cm.ExecuteScalar().ToString();


        string k = "insert into library(roll,name,mobile,city)values(@roll1,@name1,@mobile1,@city1)";

        cm = new OleDbCommand(k, cn);

        cm.Parameters.AddWithValue("roll1", txtro.Text);

        cm.Parameters.AddWithValue("name1", txtna.Text);

        cm.Parameters.AddWithValue("mobile1", txtmob.Text);

        cm.Parameters.AddWithValue("city1", txtci.Text);

        cm.ExecuteNonQuery();

        Response.Write("data inserted succesfully");

    }

    protected void update1_click(object sender, EventArgs e)

    {

        string connection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=E:/library.mdb";   // provider

        cn = new OleDbConnection(connection);

        cn.Open();


        GridViewRow r1 = ((Button)sender).NamingContainer as GridViewRow;

        TextBox txtro = (TextBox)r1.FindControl("txtroll");

        TextBox txtna = (TextBox)r1.FindControl("txtname");

        TextBox txtmob = (TextBox)r1.FindControl("txtmobile");

        TextBox txtci = (TextBox)r1.FindControl("txtcity");


        string k = "update library set name=@name1,mobile=@mobile1,city=@city1 where roll=@roll1";

        cm = new OleDbCommand(k, cn);

        cm.Parameters.AddWithValue("name1", txtna.Text);

        cm.Parameters.AddWithValue("mobile1", txtmob.Text);

        cm.Parameters.AddWithValue("city1", txtci.Text);

        cm.Parameters.AddWithValue("roll1", txtro.Text);

        cm.ExecuteNonQuery();

        Response.Write("data updated succesfully");

    }

    protected void delete1_click(object sender, EventArgs e)

    {

        string connection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=E:/library.mdb";   // provider

        cn = new OleDbConnection(connection);

        cn.Open();


        GridViewRow r1 = ((Button)sender).NamingContainer as GridViewRow;

        TextBox txtro = (TextBox)r1.FindControl("txtroll");


        string k = "delete from library where roll=@roll1";

        cm = new OleDbCommand(k, cn);

        cm.Parameters.AddWithValue("roll1", txtro.Text);

        cm.ExecuteNonQuery();

        Response.Write("data deleted succsfully");

    }

}


DATALIST CONTROLE

DESIGN CODE :

    <asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" OnItemCommand="datacrud" Height="393px" Width="712px">

                <ItemTemplate>

                    <table style="width:100%;">

                        <tr>

                            <td><b>Id</b></td>

                            <td> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("id") %>'></asp:TextBox></td>

                        </tr>

                        <tr>

                            <td><b>Name</b></td>

                            <td><asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("name") %>'></asp:TextBox></td>

                        </tr>

                        <tr>

                            <td><b>City</b></td>

                            <td> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("city") %>'></asp:TextBox></td>

                        </tr>

                        <tr>

                            <td><b>Operations</b></td>

                            <td><asp:Button ID="Button1" runat="server" Backcolor="YellowGreen" Text="Submit" CommandName="Submit1_Click" />

                                <asp:Button ID="Button2" runat="server" Backcolor="YellowGreen" Text="Update" CommandName="Update1_Click" />

                                <asp:Button ID="Button3" runat="server" Backcolor="YellowGreen" Text="Find" CommandName="Find1_Click" />

                                <asp:Button ID="Button4" runat="server" Backcolor="YellowGreen" Text="Delete" CommandName="Delete1_Click" />

                            </td>

                        </tr>

                    </table>

                </ItemTemplate>

            </asp:DataList>


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.SqlClient;

using System.Data;

using System.Configuration;

public partial class DataListCrud : System.Web.UI.Page

{

    SqlConnection cn;

    SqlCommand cm;

    SqlDataReader dr;

    protected void Page_Load(object sender, EventArgs e)

    {

        if(!IsPostBack)

        {

            show();

        }

    }

    protected void show()

    {

        string path = ConfigurationManager.AppSettings["db1"];

        cn = new SqlConnection(path);

        cn.Open();


        string k = "select * from emp";

        cm = new SqlCommand(k, cn);

        dr = cm.ExecuteReader();

        DataList1.DataSource = dr;

        DataList1.DataBind();

        dr.Close();

    }

    protected void datacrud(object sender ,DataListCommandEventArgs e)

    {

        string path = ConfigurationManager.AppSettings["db1"];

        cn = new SqlConnection(path);

        cn.Open();


        if(e.CommandName=="Submit1_Click")

        {

            TextBox txtid = (TextBox)e.Item.FindControl("TextBox1");

            TextBox txtname = (TextBox)e.Item.FindControl("TextBox2");

            TextBox txtcity = (TextBox)e.Item.FindControl("TextBox3");


            string k = "insert into emp (id,name,city)values(@id1,@name1,@city1)";

            cm = new SqlCommand(k, cn);

            cm.Parameters.AddWithValue("id1", txtid.Text);

            cm.Parameters.AddWithValue("name1", txtname.Text);

            cm.Parameters.AddWithValue("city1", txtcity.Text);

            cm.ExecuteNonQuery();

            Response.Write("<script>alert('Data Submited Succesfully')</script>");

        }

        if (e.CommandName == "Update1_Click")

        {

            TextBox txtid = (TextBox)e.Item.FindControl("TextBox1");

            TextBox txtname = (TextBox)e.Item.FindControl("TextBox2");

            TextBox txtcity = (TextBox)e.Item.FindControl("TextBox3");


            string k = "Update emp set name=@name1,city=@city1 where id=@id1";

            cm = new SqlCommand(k, cn);

            cm.Parameters.AddWithValue("id1", txtid.Text);

            cm.Parameters.AddWithValue("name1", txtname.Text);

            cm.Parameters.AddWithValue("city1", txtcity.Text);

            cm.ExecuteNonQuery();

            Response.Write("<script>alert('Data Updated Succesfully')</script>");

        }

        if (e.CommandName == "Find1_Click")

        {

            TextBox txtid = (TextBox)e.Item.FindControl("TextBox1");

            TextBox txtname = (TextBox)e.Item.FindControl("TextBox2");

            TextBox txtcity = (TextBox)e.Item.FindControl("TextBox3");


            string k = "select * from emp where id=@id1";

            cm = new SqlCommand(k, cn);

            cm.Parameters.AddWithValue("id1", txtid.Text);

            dr = cm.ExecuteReader();

            if(dr.Read())

            {

                txtname.Text = dr["name"].ToString();

                txtcity.Text = dr["city"].ToString();

            }

            dr.Close();

            Response.Write("<script>confirm('Find Data Succesfully')</script>");

        }

        if (e.CommandName == "Delete1_Click")

        {

            TextBox txtid = (TextBox)e.Item.FindControl("TextBox1");


            string k = "delete from emp where id=@id1";

            cm = new SqlCommand(k, cn);

            cm.Parameters.AddWithValue("id1", txtid.Text);

            cm.ExecuteNonQuery();

            Response.Write("<script>confirm( 'Data Deleted Succesfully')</script>");

        }

    }

}


DetailsView

SOURCE CODE :

 <asp:DetailsView ID="DetailsView1" runat="server" Height="225px" Width="445px" AutoGenerateRows="False" OnItemCommand="DetViewCrud">

                <Fields>

                    <asp:TemplateField HeaderText="Id">

                        <ItemTemplate>

                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("id") %>'></asp:TextBox>

                        </ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Pic">

                        <ItemTemplate>

                            <asp:FileUpload ID="FileUpload1" runat="server" BackColor="YellowGreen"/>

                            <asp:Image ID="Image1" runat="server" Height="30px" Width="30px" BorderColor="YellowGreen" />

                            <asp:Label ID="Label1" runat="server" Text="Label" BackColor="YellowGreen"></asp:Label>

                            <asp:Button ID="Button1" runat="server" Text="Upload" BackColor="YellowGreen" CommandName="UploadImage"/>

                        </ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Name">

                         <ItemTemplate>

                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>

                        </ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="City">

                         <ItemTemplate>

                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("city") %>'></asp:TextBox>

                        </ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Operations">

                        <ItemTemplate>

                            <asp:Button ID="Button2" runat="server" Text="Submit" BackColor="LightSeaGreen" CommandName="Submit1_Click"/>

                            <asp:Button ID="Button3" runat="server" Text="Update" BackColor="LightSeaGreen" CommandName="Update1_Click"/>

                            <asp:Button ID="Button4" runat="server" Text="Find" BackColor="LightSeaGreen" CommandName="Find1_Click"/>

                            <asp:Button ID="Button5" runat="server" Text="Delete" BackColor="LightSeaGreen" CommandName="Delete1_Click"/>

                        </ItemTemplate>

                    </asp:TemplateField>

                </Fields>

                <FooterTemplate>

                   

                </FooterTemplate>

            </asp:DetailsView>


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.SqlClient;

using System.Data;

using System.Configuration;


public partial class DetilView_Crud : System.Web.UI.Page

{

    SqlConnection cn;

    SqlCommand cm;

    SqlDataReader dr;

    protected void Page_Load(object sender, EventArgs e)

    {

        if(!IsPostBack)

        {

            show();

        }

    }

    protected void show()

    {

        string path = ConfigurationManager.AppSettings["db1"];

        cn = new SqlConnection(path);

        cn.Open();


        string k = "select * from emp";

        cm = new SqlCommand(k, cn);

        dr = cm.ExecuteReader();

        DetailsView1.DataSource = dr;

        DetailsView1.DataBind();

        dr.Close();

    }

    protected void DetViewCrud(object sender,DetailsViewCommandEventArgs e)

    {

        string path= ConfigurationManager.AppSettings["db1"];

        cn = new SqlConnection(path);

        cn.Open();


        if(e.CommandName== "UploadImage")

        {

            FileUpload FileUpload1 = (FileUpload)((DetailsView)sender).FindControl("FileUpload1");

            Image Image1 = (Image)((DetailsView)sender).FindControl("Image1");

            Label Label1 = (Label)((DetailsView)sender).FindControl("Label1");


            FileUpload1.SaveAs(Server.MapPath("~") + "/pic/" + FileUpload1.FileName); 

            Image1.ImageUrl = "~/pic/" + FileUpload1.FileName;

            Label1.Text = FileUpload1.FileName;

        }


        if(e.CommandName== "Submit1_Click")

        {

            Label lblpic = (Label)((DetailsView)sender).FindControl("Label1");

            TextBox txtid = (TextBox)((DetailsView)sender).FindControl("TextBox1");

            TextBox txtname = (TextBox)((DetailsView)sender).FindControl("TextBox2");

            TextBox txtcity = (TextBox)((DetailsView)sender).FindControl("TextBox3");


            string k = "insert into emp(id,name,city,pic)values(@id1,@name1,@city1,@pic1)";

            cm = new SqlCommand(k, cn);

            cm.Parameters.AddWithValue("id1", txtid.Text);

            cm.Parameters.AddWithValue("name1", txtname.Text);

            cm.Parameters.AddWithValue("city1", txtcity.Text);

            cm.Parameters.AddWithValue("pic1", lblpic.Text);

            cm.ExecuteNonQuery();

            Response.Write("<script>alert('Data Submited Succesfully')</script>");

        }


        if (e.CommandName == "Update1_Click")

        {

            Label lblpic = (Label)((DetailsView)sender).FindControl("Label1");

            TextBox txtid = (TextBox)((DetailsView)sender).FindControl("TextBox1");

            TextBox txtname = (TextBox)((DetailsView)sender).FindControl("TextBox2");

            TextBox txtcity = (TextBox)((DetailsView)sender).FindControl("TextBox3");


            string k = "update emp set name=@name1,city=@city1,pic=@pic1 where id=@id1";

            cm = new SqlCommand(k, cn);

            cm.Parameters.AddWithValue("id1", txtid.Text);

            cm.Parameters.AddWithValue("name1", txtname.Text);

            cm.Parameters.AddWithValue("city1", txtcity.Text);

            cm.Parameters.AddWithValue("pic1", lblpic.Text);

            cm.ExecuteNonQuery();

            Response.Write("<script>alert('Data Updated Succesfully')</script>");

        }


        if (e.CommandName == "Find1_Click")

        {

            Label lblpic = (Label)((DetailsView)sender).FindControl("Label1");

            Image Image1 = (Image)((DetailsView)sender).FindControl("Image1");

            TextBox txtid = (TextBox)((DetailsView)sender).FindControl("TextBox1");

            TextBox txtname = (TextBox)((DetailsView)sender).FindControl("TextBox2");

            TextBox txtcity = (TextBox)((DetailsView)sender).FindControl("TextBox3");


            string k = "select * from emp where id=@id1";

            cm = new SqlCommand(k, cn);

            cm.Parameters.AddWithValue("id1", txtid.Text);

            dr = cm.ExecuteReader();

            if(dr.Read())

            {

                txtname.Text = dr["name"].ToString();

                txtcity.Text = dr["city"].ToString();

                Image1.ImageUrl = "~/pic/" + dr["pic"].ToString();

                Response.Write("<script>alert('Find Data Succesfully')</script>");

            }

            else

            {

                Response.Write("<script>alert('Data Not Found')</script>");

            }

            dr.Close();

        }


        if (e.CommandName == "Delete1_Click")

        {

            TextBox txtid = (TextBox)((DetailsView)sender).FindControl("TextBox1");


            string k = "delete from emp  where id=@id1";

            cm = new SqlCommand(k, cn);

            cm.Parameters.AddWithValue("id1", txtid.Text);

            cm.ExecuteNonQuery();

            Response.Write("<script>alert('Data Deleted Succesfully')</script>");

        }

    }

}



Comments

Popular posts from this blog

Z 5 ) MVC (Model view Controller)

Z 8 ) MVC HTMLHELPER METHOD AND ACTIONLINK (how to use css ,atrributes,style in css)