Wednesday, June 18, 2014

Ms Office 2007 Oledb Read file Excel Code and 2010 code

2007................
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='E://Naresh.xlsx';Extended Properties=\'Excel 8.0';");

     
        OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]");
        cmd.Connection = con;
        OleDbDataAdapter dap = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        return (ds);
2010..................
OleDbConnection con = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source='E://Naresh.xlsx';Extended Properties=\'Excel 8.0';");

     
        OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]");
        cmd.Connection = con;
        OleDbDataAdapter dap = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        return (ds);

Gridview Searching textBox using OLEDB Connection Excel

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

<!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:TextBox ID="txtsearch" runat="server"></asp:TextBox>
    <asp:Button ID="btnsearch" runat="server" Text="Search" onclick="btnsearch_Click" />

    <asp:GridView ID="grd" runat="server" AutoGenerateColumns="False" BackColor="White"
            BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
            CellSpacing="1" GridLines="None">
        <Columns>
            <asp:TemplateField HeaderText="empid">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# bind("empid") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField >
            <asp:TemplateField HeaderText="empname">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# bind("empname") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="salary">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# bind("salary") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
 
        <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
        <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#594B9C" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#33276A" />
 
    </asp:GridView>
 
    </div>
    </form>
</body>

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

public partial class Excel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session.Clear();
            grd.DataSource = getData();
            grd.DataBind();
        }
    }
    public DataSet getData()
        {
        OleDbConnection con = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='D://Naresh.xlsx';Extended Properties=\'Excel 8.0;'");
        OleDbCommand cmd = new OleDbCommand("select * from [emp$]");
        cmd.Connection = con;
        OleDbDataAdapter dap = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        return (ds);
    }
    protected void btnsearch_Click(object sender, EventArgs e)
    {



        //grd.DataSource = getValues(txtsearch.Text);
        //grd.DataBind();

        foreach (GridViewRow objgrd in grd.Rows)
        {

            Label lblname = (Label)objgrd.FindControl("Label2");
            Label lblid = (Label)objgrd.FindControl("Label1");
            Label lblsalary = (Label)objgrd.FindControl("Label3");
            if (txtsearch.Text == lblname.Text)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("empid", typeof(string));
                dt.Columns.Add("empname", typeof(string));
                dt.Columns.Add("salary", typeof(string));
                dt.Rows.Add(lblid.Text, lblname.Text, lblsalary.Text);
                grd.DataSource = dt;
                grd.DataBind();
            }
        }
        //}
        ////    //DataSet ds =
        ////    grd.DataSource = getValues(lblsalary.Text);
        ////    grd.DataBind();
        ////    //DataTable dt = new DataTable();
        ////    //dt.Columns.Add("empid", typeof(string));
        ////    //dt.Columns.Add("empname", typeof(string));
        ////    //dt.Columns.Add("salary", typeof(string));
        ////    //if (Session["dt"] == null)
        ////    //{
        ////    //    dt.Rows.Add(ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][2].ToString(), ds.Tables[0].Rows[0][2].ToString());
        ////    //    Session["dt"] = dt;
        ////    //    grd.DataSource = dt;
        ////    //    grd.DataBind();

        ////    //}
        ////    //else
        ////    //{
        ////    //    DataTable dt2 = (DataTable)Session["dt"];
        ////    //    dt2.Rows.Add(ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][2].ToString(), ds.Tables[0].Rows[0][2].ToString());
        ////    //    Session["dt"] = dt2;

        ////    //    grd.DataSource = dt2;
        ////    //    grd.DataBind();

        ////    //}


        ////}
     

    }
    public DataSet getValues(string name)
    {
        OleDbConnection con = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='D://Naresh.xlsx';Extended Properties=\'Excel 8.0';");
        OleDbCommand cmd = new OleDbCommand("select * from [emp$] Where empname like '" + name + "%'");
        cmd.Connection = con;
     
        OleDbDataAdapter dap = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        return (ds);
    }


}

Monday, June 16, 2014

Datalist Grid Cart using Sessions

ASPX.... Laps.aspx

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

<!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:DataList ID="dllap" runat="server" BackColor="LightGoldenrodYellow"
            BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black">
            <AlternatingItemStyle BackColor="PaleGoldenrod" />
            <FooterStyle BackColor="Tan" />
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <ItemTemplate>
                <table>
                    <tr>
                        <td>
                            <asp:CheckBox ID="chklap" runat="server" />
                        </td>
                        <asp:Label ID="lbllapid" runat="server" Text='<%#bind("lapid") %>' Visible="false"></asp:Label>
                        <td>
                            <asp:Label ID="lbllapname" runat="server" Text='<%#bind("lapname") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Image ID="lapimg" runat="server" ImageUrl='<%#bind("lapimg") %>' Height="100px" />
                        </td>
                        <td>
                            <asp:Label ID="lbllapprice" runat="server" Text='<%#bind("lapprice") %>'></asp:Label>
                        </td>
                     
                    </tr>
                </table>
            </ItemTemplate>
            <SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        </asp:DataList>
        <asp:Button ID="btnok" runat="server" Text="ok" OnClick="btnok_Click" />
        <asp:Button ID="btnlogout" runat="server" Text="logout"
            onclick="btnlogout_Click" />
    </div>
    </form>
</body>
</html>
-----------------------------------------------------------------------------------------------
laps.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;


public partial class _Default : System.Web.UI.Page
{
    Class1 obj = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

             
            //Label lbll=(Label)dl.FindControl("lbllapid");
                dllap.DataSource = obj.totaaps();
                dllap.DataBind();
                //Session.Clear(); 
          
        }
    }

    protected void btnok_Click(object sender, EventArgs e)
    {
        foreach (DataListItem dll in dllap.Items)
        {
            //finding the control in container.
            CheckBox chk = (CheckBox)dll.FindControl("chklap");
            
            if (chk.Checked)
            {
                Label lbll = (Label)dll.FindControl("lbllapid");
                Label lblitemname = (Label)dll.FindControl("lbllapname");
                Image lblimg = (Image)dll.FindControl("lapimg");
                Label lblprice = (Label)dll.FindControl("lbllapprice");
                
                //DataSet ds = obj.nnn(Convert.ToInt32(lbll.Text));
                DataTable dt = new DataTable();
                dt.Columns.Add("lapid", typeof(string));
                dt.Columns.Add("lapname", typeof(string));
                dt.Columns.Add("lapimg", typeof(string));
                dt.Columns.Add("lapprice", typeof(string));
                dt.Columns.Add("txtqty", typeof(string));
                dt.Columns.Add("txtlaptotal", typeof(string));

                if (Session["dt"] == null)
                {
                    dt.Rows.Add(lbll.Text,lblitemname.Text, lblimg.ImageUrl,lblprice.Text,"","");
                    Session["dt"] = dt;

                }
                else
                {
                    DataTable dttt = (DataTable)Session["dt"];
                    dttt.Rows.Add(lbll.Text, lblitemname.Text, lblimg.ImageUrl, lblprice.Text,"","");
                    Session["dt"] = dttt;
                }
            }
            
        }
       
        //foreach (DataListItem dl in dllap.Items)
        //{
        //    CheckBox chk = (CheckBox)dl.FindControl("chklap");

        //    if (chk.Checked)
        //    {
        //        Label lbl_id = (Label)dl.FindControl("lbllapid");
        //        Label lbl_name = (Label)dl.FindControl("lbllapname");
        //        Image img = (Image)dl.FindControl("lapimg");
        //        Label lbl_price = (Label)dl.FindControl("lbllapprice");
        //        dt.Rows.Add(lbl_id.Text, lbl_name.Text, img.ImageUrl, lbl_price.Text);

        //    }
           // Session["lapid"] = dt;


        //}
        Response.Redirect("selectedlap.aspx");
    }
    protected void btnlogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
    }
}
----------------------------------------------------------------------------------------------
Selected ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="selectedlap.aspx.cs" Inherits="selectedlap" %>

<!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:Button ID="btnlogout" runat="server" Text="Logout" onclick="btnlogout_Click" />
        <asp:GridView ID="gridlap" runat="server" AutoGenerateColumns="False" 
            OnSelectedIndexChanged="txtqtyChange" BackColor="#DEBA84" BorderColor="#DEBA84" 
            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
            <Columns>
                <asp:TemplateField HeaderText="id">
                    <ItemTemplate>
                        <asp:Label ID="lbllapid" runat="server" Text='<%#bind("lapid") %>'></asp:Label></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="name">
                    <ItemTemplate>
                        <asp:Label ID="lbllapname" runat="server" Text='<%#bind("lapname") %>'></asp:Label></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="image" ControlStyle-Height="50px" ControlStyle-Width="50px">
                    <ItemTemplate>
                        <asp:Image ID="lapimg" runat="server" ImageUrl='<%#bind("lapimg") %>' />
                    </ItemTemplate>

<ControlStyle Height="50px" Width="50px"></ControlStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="quantity">
                    <ItemTemplate>
                        <asp:TextBox ID="txtqty" runat="server" Text='<%#Bind("txtqty") %>' OnTextChanged="txtqtyChange" AutoPostBack="true"></asp:TextBox></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="price">
                    <ItemTemplate>
                        <asp:Label ID="lbllapprice" runat="server" Text='<% #bind("lapprice")%>'></asp:Label></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="total">
                    <ItemTemplate>
                        <asp:TextBox ID="txtlaptotal" runat="server" Text='<%#Bind("txtlaptotal") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
        total:
        <asp:TextBox ID="totaltxt" runat="server"></asp:TextBox>
        <asp:Button ID="btnbacklap" runat="server" Text="back" 
            OnClick="btnbacklap_Click" style="height: 26px" />
    </div>
    </form>
</body>
</html>
------------------------------------------------------------------------------------------------------
selected.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;

public partial class selectedlap : System.Web.UI.Page
{
    Class1 obj1 = new Class1();
    int total = 0;
    DataTable dt2;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            


            DataTable dtr = (DataTable)Session["dt"];
            
            gridlap.DataSource = dtr;
            gridlap.DataBind();


        }

    }
    public void txtqtyChange(object sender, EventArgs e)
    {
        foreach (GridViewRow gd in gridlap.Rows)
        {
            TextBox txttotal = (TextBox)gd.FindControl("txtlaptotal");
            TextBox txt = (TextBox)gd.FindControl("txtqty");
            Label lbl = (Label)gd.FindControl("lbllapprice");
            if (txt.Text == string.Empty)
            {
            }
            else
            {
                txttotal.Text = Convert.ToString(Convert.ToInt32(txt.Text) * Convert.ToDecimal(lbl.Text));
                total += Convert.ToInt32(txttotal.Text);
            }


        }
        totaltxt.Text = total.ToString();
    }

    protected void btnbacklap_Click(object sender, EventArgs e)
    {
        Session.Clear();

        foreach (GridViewRow objrow in gridlap.Rows)
        {

            Label lblid = (Label)objrow.FindControl("lbllapid");
            Label lbname = (Label)objrow.FindControl("lbllapname");
            Image img = (Image)objrow.FindControl("lapimg");
            TextBox txtqtyy = (TextBox)objrow.FindControl("txtqty");
            Label lblprice = (Label)objrow.FindControl("lbllapprice");
            TextBox txttotl = (TextBox)objrow.FindControl("txtlaptotal");

            if (Session["dt"] != null)
            {
                
                DataTable dt1 = (DataTable)Session["dt"];
                dt1.Rows.Add(lblid.Text, lbname.Text, img.ImageUrl, lblprice.Text,txtqtyy.Text, txttotl.Text);
                Session["dt"] = dt1;
            }
            else
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("lapid", typeof(string));
                dt.Columns.Add("lapname", typeof(string));
                dt.Columns.Add("lapimg", typeof(string));
                dt.Columns.Add("lapprice", typeof(string));
                dt.Columns.Add("txtqty", typeof(string));
                dt.Columns.Add("txtlaptotal", typeof(string));

                dt.Rows.Add(lblid.Text, lbname.Text, img.ImageUrl, lblprice.Text, txtqtyy.Text, txttotl.Text);
                Session["dt"] = dt;
            }
           


        }



        Response.Redirect("laps.aspx");

    }
    // Response.Redirect("laps.aspx");
    protected void btnlogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
    }
}

----------------------------------------------------------------------------------------------------
.Class.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
    public string cn = ConfigurationManager.ConnectionStrings["con"].ToString();
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    public Class1()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public DataSet totaaps()
    {
        con = new SqlConnection(cn);
        cmd = new SqlCommand("select * from lap", con);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
    public DataSet nnn(int lapid)
    {
        con = new SqlConnection(cn);
        cmd = new SqlCommand("select * from lap where lapid='"+lapid+"'", con);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
}
---------------------------------------------------------------------------------------------------


Grid PopUp Extender Ajax

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

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderColor="#336699"
            BorderStyle="Solid" BorderWidth="1px" CellPadding="3" Font-Names="Verdana" Font-Size="10pt"
            OnRowCreated="GridView1_RowCreated">
            <Columns>
                <asp:BoundField DataField="ProductID" HeaderText="Product ID" />
                <asp:BoundField DataField="ProductName" HeaderText="Product Name" />
                <asp:TemplateField ItemStyle-Width="40" ItemStyle-HorizontalAlign="Right">
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" ImageUrl="Images/image.jpg" />
                        <asp:PopupControlExtender id="PopupControlExtender1" runat="server" PopupControlID="Panel1"
                            TargetControlID="Image1" DynamicContextKey='<%# Eval("ProductID") %>' DynamicControlID="Panel1"
                            DynamicServiceMethod="GetDynamicContent" Position="Bottom">
</asp:popupcontrolextender>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="#336699" ForeColor="White" />
        </asp:GridView>
        <asp:Panel ID="Panel1" runat="server"> </asp:Panel>
    </div>
    </form>
</body>
</html>


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

public partial class Grid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        string constr = "Data Source=KCLINK-45-PC\\SQLEXPRESS;Initial Catalog=naresh;Integrated Security=True";
        string query = "SELECT ProductID, ProductName FROM aaaa";
        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();
        da.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            PopupControlExtender pce = e.Row.FindControl("PopupControlExtender1") as PopupControlExtender;
            string behaviorID = "pce_" + e.Row.RowIndex;
            //pce.BehaviorID = behaviorID;
            Image img = (Image)e.Row.FindControl("Image1");
            string OnMouseOverScript = string.Format("$find('{0}').showPopup();", behaviorID);
            string OnMouseOutScript = string.Format("$find('{0}').hidePopup();", behaviorID);
            img.Attributes.Add("onmouseover", OnMouseOverScript);
            img.Attributes.Add("onmouseout", OnMouseOutScript);
        }
    }


    [System.Web.Services.WebMethodAttribute(),
    System.Web.Script.Services.ScriptMethodAttribute()]
    public static string GetDynamicContent(string contextKey)
    {
        string constr = "Data Source=KCLINK-45-PC\\SQLEXPRESS;Initial Catalog=naresh;Integrated Security=True";
        string query = "SELECT UnitPrice,Description FROM aaa WHERE ProductID = " + contextKey;
        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();
        da.Fill(table);
        StringBuilder b = new StringBuilder();
        b.Append("<table style='background-color:#f3f3f3; border: #336699 3px solid; ");
        b.Append("width:350px; font-size:10pt; font-family:Verdana;' cellspacing='0' cellpadding='3'>");
        b.Append("<tr><td colspan='3' style='background-color:#336699; color:white;'>");
        b.Append("<b>Product Details</b>"); b.Append("</td></tr>");
        b.Append("<tr><td style='width:80px;'><b>Unit Price</b></td>");
        //b.Append("<td style='width:80px;'><b>Stock</b></td>");
        b.Append("<td><b>Description</b></td></tr>");
        b.Append("<tr>");
        b.Append("<td>" + table.Rows[0]["UnitPrice"].ToString() + "</td>");
        //b.Append("<td>" + table.Rows[0]["UnitsInStock"].ToString() + "</td>");
        b.Append("<td>" + table.Rows[0]["Description"].ToString() + "</td>");
        b.Append("</tr>");
        b.Append("</table>");
        return b.ToString();
    }
}


----------------------------------------------------------------------------------------------------

Grid Edit And Update Dynamic

ASPX


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

<!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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="drp" runat="server" AutoPostBack="true" AppendDataBoundItems="true"
            OnSelectedIndexChanged="drp_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="Id">
                    <ItemTemplate>
                        <asp:Label ID="lblid" runat="server" Text='<%#bind("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:Label ID="lblfirstname" runat="server" Text='<%#bind("firstname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name">
                    <ItemTemplate>
                        <asp:Label ID="ibllastname" runat="server" Text='<%#bind("lastname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Designation">
                    <ItemTemplate>
                        <asp:Label ID="lbldesignation" runat="server" Text='<%#bind("designation") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Salary">
                    <ItemTemplate>
                        <asp:Label ID="lblsalary" runat="server" Text='<%#bind("salary") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mobile Number">
                    <ItemTemplate>
                        <asp:Label ID="iblmobilenumber" runat="server" Text='<%#bind("mobilenumber") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:TextBox ID="txtbox" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Edit">
                    <ItemTemplate>
                        <asp:LinkButton ID="linkbtn" runat="server" OnCommand="edit" Text="Edit" CommandArgument='<%#bind("id") %>'
                            CommandName='<%#bind("salary") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="btn" runat="server" Text="update" OnClick="btn_Click" />
    </div>
    </form>
</body>
</html>



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

public partial class GridDynamic : System.Web.UI.Page
{
    Class1 obj;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            Session.Clear();
            obj = new Class1();
            drp.Items.Clear();
            drp.Items.Add("--Select--");
            drp.DataSource = obj.drpbind();
            drp.DataTextField = "Firstname";
            drp.DataValueField = "id";
            drp.DataBind();

        }
    }
    protected void drp_SelectedIndexChanged(object sender, EventArgs e)
    {
        obj = new Class1();
        if (Convert.ToInt32(drp.SelectedIndex) == 0)
        {
            Response.Write("select one");
        }
        else
        {
            DataSet ds = obj.grdbind(Convert.ToInt32(drp.SelectedValue));

            if (Session["dt"] == null)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Id", typeof(string));
                dt.Columns.Add("FirstName", typeof(string));
                dt.Columns.Add("LastName", typeof(string));
                dt.Columns.Add("Designation", typeof(string));
                dt.Columns.Add("Salary", typeof(string));
                dt.Columns.Add("MobileNumber", typeof(string));
                dt.Rows.Add(ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][1].ToString(), ds.Tables[0].Rows[0][2].ToString(), ds.Tables[0].Rows[0][3].ToString(), ds.Tables[0].Rows[0][4].ToString(), ds.Tables[0].Rows[0][5].ToString());
                Session["dt"] = dt;
                grd.DataSource = dt;
                grd.DataBind();

            }
            else
            {
                DataTable dt2 = (DataTable)Session["dt"];
                dt2.Rows.Add(ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][1].ToString(), ds.Tables[0].Rows[0][2].ToString(), ds.Tables[0].Rows[0][3].ToString(), ds.Tables[0].Rows[0][4].ToString(), ds.Tables[0].Rows[0][5].ToString());
                Session["dt"] = dt2;
                BindGrid(dt2);


            }



            foreach (GridViewRow objgrd in grd.Rows)
            {

                TextBox txt = (TextBox)objgrd.FindControl("txtbox");
                txt.Visible = false;

            }
        }
    }

    private void BindGrid(DataTable dt2)
    {
        grd.DataSource = dt2;
        grd.DataBind();
    }
    protected void edit(object sender, CommandEventArgs e)
    {

        string id = e.CommandArgument.ToString();
        string salary = e.CommandName.ToString();
        foreach (GridViewRow objgrd in grd.Rows)
        {
            TextBox txt = (TextBox)objgrd.FindControl("txtbox");
            txt.Visible = true;

        }



    }


    protected void btn_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow objgrd in grd.Rows)
        {
            obj = new Class1();
            Label lblid = (Label)objgrd.FindControl("lblid");
            Label lblsalary = (Label)objgrd.FindControl("lblsalary");
            TextBox txt = (TextBox)objgrd.FindControl("txtbox");
            //txt.Visible = true;
            if (txt.Text == "")
            {
                obj.salaryupdate(Convert.ToInt32(lblid.Text), lblsalary.Text);
            }
            else
            {
                obj.salaryupdate(Convert.ToInt32(lblid.Text), txt.Text);

            }
            DataTable dt2 = new DataTable();
            BindGrid(dt2);


        }

    }
}


------------------------------------------------------------------------------------------