Tuesday, July 22, 2014

Update Image in GridView in Asp.Net.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="True"
            OnRowCancelingEdit="GridView1_RowCancelingEdit" DataKeyNames="ID" CellPadding="4"
            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333">
            <Columns>
                <asp:TemplateField HeaderStyle-Width="150px">
                    <ItemTemplate>
                        <asp:LinkButton ID="LkB1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="LkB2" runat="server" CommandName="Update">Update</asp:LinkButton>
                        <asp:LinkButton ID="LkB3" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name" HeaderStyle-Width="200px">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_Name" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Image" HeaderStyle-Width="200px">
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Image") %>' Height="80px"
                            Width="100px" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Image ID="img_user" runat="server" ImageUrl='<%# Eval("Image") %>' Height="80px"
                            Width="100px" /><br />
                        <asp:FileUpload ID="FileUpload1" runat="server" />
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 =================================================================
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 Default3 : System.Web.UI.Page
{
    // sql connection
    SqlConnection con = new SqlConnection(@"Data Source=KCLINK-PC;Initial Catalog=Naresh;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }
    //method for binding GridView
    protected void BindGridView()
    {
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("Select ID, Name,Branch,City,Image from tbl_student"
                                                                                                                 , con);
        con.Open();
        da.Fill(dt);
        con.Close();

        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

    // edit event
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView();

    }
    // update event
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //find student id of edit row
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        // find values for update
        TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Name");

        FileUpload FileUpload1 = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1");

        string path = "~/Images/UserImage/";
        if (FileUpload1.HasFile)
        {
            path += FileUpload1.FileName;
            //save image in folder
            FileUpload1.SaveAs(MapPath(path));
        }
        else
        {
            // use previous user image if new image is not changed
            Image img = (Image)GridView1.Rows[e.RowIndex].FindControl("img_user");
            path = img.ImageUrl;
        }

        SqlCommand cmd = new SqlCommand("update tbl_student set Name='" + name.Text + "',image='"+path+"'  where ID=" + id + "", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

        GridView1.EditIndex = -1;
        BindGridView();
    }
    // cancel edit event
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGridView();
    }
}

Grid view row values in text boxs


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
            AutoGenerateSelectButton="True">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="Branch" HeaderText="Branch" />
                <asp:BoundField DataField="City" HeaderText="City" />
            </Columns>
        </asp:GridView>
        <table>
            <tr>
                <td>
                    Name
                </td>
                <td>
                    <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Branch
                </td>
                <td>
                    <asp:TextBox ID="txt_branch" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    City
                </td>
                <td>
                    <asp:TextBox ID="txt_city" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

==========================================================================
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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }
    //method for binding GridView
    protected void BindGridView()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(@"Data Source=KCLINK-PC;Initial Catalog=Naresh;Integrated Security=True");
        SqlDataAdapter da = new SqlDataAdapter("Select name,branch,city from tbl_student", con);
        con.Open();
        da.Fill(dt);
        con.Close();

        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        txt_name.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
        txt_branch.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
        txt_city.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
    }
}
=================================================================
CREATE TABLE [dbo].[tbl_student](
      [Id] [int] NULL,
      [Name] [varchar](50) NULL,
      [Branch] [varchar](50) NULL,
      [City] [varchar](50) NULL)

How to delete xml in C#

Example 1
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Students>
  <Student>
    <Name>Jitesh kumar</Name>
    <Email>Jiteshkumar@asphelps.com</Email>
    <City>Buxar, Dumraon</City>
  </Student>
</Students>
        XDocument document = XDocument.Load(@"D:\Student.xml");

        document.Element("Students").Elements("Student").Remove();

        document.Save(@"D:\Student.xml");
Example 2
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Students>
  <Student Id="1">
    <Name>Amit</Name>
    <Email>Amit@asphelps.com</Email>
    <City>Dumraon</City>
  </Student>
</Students>
XDocument document1 = XDocument.Load(@"D:\Student1.xml");

document.Element("Students").Elements("Student").Where(i=>i.Attribute("Id").Value == "1").Remove();

document.Save(@"D:\Student1.xml");
Example 3
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Students>
  <Student Id="1" Name="Jitesh Byahut" Email="jiteshbyahut@asphelps.com" City="Dumraon, Buxar" />
  <Student Id="2" Name="Amit" Email="amit@asphelps.com" City="Dumraon" />
</Students>
  XDocument document = XDocument.Load(@"D:\Student3.xml");

  document.Element("Students").Elements("Student").Where(i=>i.Attribute("Id").Value=="1").Remove();

  document.Save(@"D:\Student3.xml");
Example 4
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Students>
  <Student>
    <Name>Jitesh Byahut</Name>
    <Email>jiteshbyahut@asphelps.com</Email>
    <City>Dumraon, Buxar</City>
  </Student>
  <Education>
    <Degree>Graduation</Degree>
    <Branch>Electronics &amp; Communication</Branch>
    <University>Rajasthan Technical University</University>
  </Education>
  <Contact>
    <Mobile>8560839802</Mobile>
    <Email>jiteshbyahut@asphelps.com</Email>
  </Contact>
  <Contact>
    <Mobile>8431214496</Mobile>
    <Email>jitesh123.2008@gmail.com</Email>
  </Contact>
</Students>
        XDocument document = XDocument.Load(@"D:\Student3.xml");
        // If you want to delete Student section
        document.Element("Students").Elements("Student").Remove();
        // If you want to delete Education section
        document.Element("Students").Elements("Education").Remove();
        // If you want to delete Contact section
        document.Element("Students").Elements("Contact").Remove();

        document.Save(@"D:\Student3.xml");
Example 5
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Students>
  <Student>
    <Name>Jitesh Byahut</Name>
    <Email>jiteshbyahut@asphelps.com</Email>
    <City>Dumraon, Buxar</City>
    <Education>
      <Degree>Graduation</Degree>
      <Branch>Electronics &amp; Communication</Branch>
      <University>Rajasthan Technical University</University>
    </Education>
  </Student>
</Students>
        XDocument document = XDocument.Load(@"D:\Student4.xml");
        // If you want to delete Student section
        document.Element("Students").Elements("Student").Remove();
        // If you want to delete Education section
        document.Element("Students").Elements("Student").Elements("Education").Remove();


        document.Save(@"D:\Student4.xml");