Tuesday, August 7, 2012

On Row Data Bound Grid View(Insert , Modify)






Add three Pages with names AddPage.aspx, ModifyPage.aspx, and a Default.aspx.

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

<script type="text/javascript" language="javascript">

    var lastRowSelected;

    var originalColor;

    var EmpId = "";

    var deleno = "";

   function GridView_selectRow(row, eno, ename, eid) {

        EmpId = eid;

       if (lastRowSelected != row) {

            if (lastRowSelected != null) {

                lastRowSelected.style.backgroundColor = originalColor;

                lastRowSelected.style.color = 'Black'

                lastRowSelected.style.fontWeight = 'normal';

            }

            originalColor = row.style.backgroundColor

            row.style.backgroundColor = 'Blue'

            row.style.color = 'White'

            row.style.fontWeight = 'normal'

            lastRowSelected = row;

        }

    }

    function GridView_mouseHover(row) {

        row.style.cursor = 'hand';

    }

    function updatEmp() {

        location.replace("ModifyPage.aspx?eno=" + EmpId);

  }

  


    </script>  

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <table align="center">

    <tr>

    <td align="center" colspan="2">

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

            onrowdatabound="GridView1_RowDataBound" >

        </asp:GridView>

    </td>

    </tr>

    <tr>

    <td>

        &nbsp;</td>

    </tr>

    <tr>

    <td>

        <asp:Button ID="Button1" runat="server" Text="Add" onclick="Button1_Click" />

    </td>

    <td>

        &nbsp;</td>

    <td>

        <input id="btnModify" type="button" value="Modify" onclick="updatEmp();" />

    </td>

    </tr>

    </table>

       </div>

    </form>

</body>

</html>


Default.aspx.cs


using System.Data;

using System.Data.SqlClient;

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");

            SqlCommand cmd = new SqlCommand("select *from employee", con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();

            da.Fill(ds);

            GridView1.DataSource = ds;

            GridView1.DataBind();

        }

        

    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            e.Row.ID = e.Row.Cells[0].Text;

            e.Row.Attributes.Add("onclick", "GridView_selectRow(this,'" + e.Row.Cells[0].Text + "','" + DataBinder.Eval(e.Row.DataItem, "ename") + "','" + DataBinder.Eval(e.Row.DataItem, "eno") + "')");

            e.Row.Attributes.Add("onmouseover", "GridView_mouseHover(this)");

            //e.Row.Attributes.Add("onclick", "reloadPage(this)");

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Server.Transfer("AddPage.aspx");

    }

}

ScreenShot of Default.aspx





<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
        <asp:Label ID="Label1" runat="server" Text="Eno"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEno" runat="server"></asp:TextBox>
    </td>
    </tr>
        <tr>
    <td>
        <asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEname" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
     <asp:Label ID="Label3" runat="server" Text="Designation"></asp:Label>
    </td>
    <td>
     <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
        <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>


AddPage.aspx.cs


using System.Web.UI.WebControls;

using System.Data.SqlClient;

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

{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into employee values(" + txtEno.Text + ",'" + txtEname.Text + "','" + txtDesignation.Text + "')", con);
        int i=cmd.ExecuteNonQuery();
        if(i>0)
        {
            Response.Redirect("Default.aspx");
        }
        con.Close();

    }

}

ScreenShot of AddPage.aspx(Click on the Image to View





















ModifyPage.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
        <asp:Label ID="Label1" runat="server" Text="Eno"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEno" runat="server" ReadOnly="true"></asp:TextBox>
    </td>
    </tr>
    <tr>
       <td>
        <asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEname" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
           <td>
        <asp:Label ID="Label3" runat="server" Text="Designation"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
        <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click"/>
    </td>
   </tr>
    </table>
    </div>
    </form>
</body>
</html>

ModifyPage.aspx.cs

using System.Data;
using System.Data.SqlClient;

public partial class ModifyPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            int employeeno = Convert.ToInt32(Context.Request.QueryString["eno"]);
            SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
            con.Open();
            SqlCommand cmd = new SqlCommand("select *from employee where eno=" + employeeno, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                txtEno.Text = Convert.ToString(dr[0]);
                txtEname.Text = Convert.ToString(dr[1]);
                txtDesignation.Text = Convert.ToString(dr[2]);
            }
            con.Close();
        }
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        int employeeno = Convert.ToInt32(Context.Request.QueryString["eno"]);
         SqlCommand cmd = new SqlCommand("update employee set ename='" + txtEname.Text + "',designation='" + txtDesignation.Text + "' where eno="+employeeno, con);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Response.Redirect("Default.aspx");
        }
        con.Close();
    }
}



Friday, August 3, 2012

Grid View Insert Update using OnRowDataBound

Design the Table




Add three Pages with names AddPage.aspx, ModifyPage.aspx, and a Default.aspx.

AddPage.aspx


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
        <asp:Label ID="Label1" runat="server" Text="Eno"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEno" runat="server"></asp:TextBox>
    </td>
    </tr>
        <tr>
    <td>
        <asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEname" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
     <asp:Label ID="Label3" runat="server" Text="Designation"></asp:Label>
    </td>
    <td>
     <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
        <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>


AddPage.aspx.cs


using System.Web.UI.WebControls;

using System.Data.SqlClient;

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

{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into employee values(" + txtEno.Text + ",'" + txtEname.Text + "','" + txtDesignation.Text + "')", con);
        int i=cmd.ExecuteNonQuery();
        if(i>0)
        {
            Response.Redirect("Default.aspx");
        }
        con.Close();

    }

}

ScreenShot of AddPage.aspx(Click on the Image to View)




Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script type="text/javascript" language="javascript">
   var oldgridSelectedColor;
var eno;
function setMouseOverColor(element)
{
    oldgridSelectedColor = element.style.backgroundColor;
    element.style.backgroundColor = 'yellow';
    element.style.cursor = 'hand';
    element.style.textDecoration = 'underline';
}

function setMouseOutColor(element) {
    eno = element;
    element.style.backgroundColor = oldgridSelectedColor;
    element.style.textDecoration = 'none';
}
function onRowSelect(employeeno, element) {
   element.style.backgroundColor = 'Red';
   eno = employeeno;
   window.location.href = "ModifyPage.aspx?employeeno="+eno;
}
</script>
  </head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center">
    <tr>
    <td align="center">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
            onrowdatabound="GridView1_RowDataBound">
      <Columns>
      <asp:BoundField DataField="eno" HeaderText="Eno" />
      <asp:BoundField DataField="ename" HeaderText="Ename" />
      <asp:BoundField DataField="designation" HeaderText="Designation" />
      </Columns>
        </asp:GridView>
    </td>
    </tr>
    <tr>
    <td>
        <asp:Button ID="Button1" runat="server" Text="Add" onclick="Button1_Click" />
      </td>
    </tr>
    </table>
      </div>
    </form>
</body>
</html>


Default.aspx.cs

using System.Data;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        SqlCommand cmd = new SqlCommand("select *from employee", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();  
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddPage.aspx");
    }
   
   
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] =
                "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] =
                "javascript:setMouseOutColor(this);";
          e.Row.Attributes.Add("onclick", "onRowSelect('" + DataBinder.Eval(e.Row.DataItem, "eno") + "',this)");

        }
    }
}






To modify the Record click on the record in the GridView.

ModifyPage.aspx

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
        <asp:Label ID="Label1" runat="server" Text="Eno"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEno" runat="server" ReadOnly="true"></asp:TextBox>
    </td>
    </tr>
    <tr>
       <td>
        <asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEname" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
           <td>
        <asp:Label ID="Label3" runat="server" Text="Designation"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
        <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click"/>
    </td>
     </tr>
    </table>
    </div>
    </form>
</body>
</html>


ModifyPage.aspx.cs

using System.Data;
using System.Data.SqlClient;

public partial class ModifyPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int employeeno = Convert.ToInt32(Context.Request.QueryString["employeeno"]);
            SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
            con.Open();
            SqlCommand cmd = new SqlCommand("select *from employee where eno=" + employeeno, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                txtEno.Text = Convert.ToString(dr[0]);
                txtEname.Text = Convert.ToString(dr[1]);
                txtDesignation.Text = Convert.ToString(dr[2]);
            }
            con.Close();
        }
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        int employeeno = Convert.ToInt32(Context.Request.QueryString["employeeno"]);
        SqlCommand cmd = new SqlCommand("update employee set ename='" + txtEname.Text + "',designation='" + txtDesignation.Text + "' where eno="+employeeno, con);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Response.Redirect("Default.aspx");
        }
        con.Close();
    }
}


How To Fill ListBox using DataBase

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
    </div>
    </form>
</body>
</html>

Default.aspx.cs


using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

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

{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        SqlCommand cmd = new SqlCommand("select *from customers", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        for (int i = 0; i < ds.Tables[0].Rows.Count - 1; i++)
        {
         ListBox1.Items.Add(new ListItem(ds.Tables[0].Rows[i]["customername"].ToString(), ds.Tables[0].Rows[i]["customerid"].ToString()));
        }
    }
}



Thursday, August 2, 2012

How to Fill the Drop Down Values From DataBase

Design the DataTable





Default.aspx

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
    
    </div>
    </form>
</body>
</html>


using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        SqlCommand cmd = new SqlCommand("select *from customers", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "customername";
        DropDownList1.DataValueField = "customerid";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0,new ListItem("--SelectItem--","0"));
    }
}







Javascript Pop UP

<head runat="server">
    <title></title>
    
<script type="text/javascript" language="javascript">
        function Add() {
            var left, top;
            var pos = new Array;
            pos = Positions(700, 500);
            left = pos[0];
            top = pos[1];
            var position = "channelmode=no,scrollbars=yes, width=700,height=500, left=" + left + " , top=" + top;
            popup = window.open("Default4.aspx","hi",position);
        }
      function Positions(windowWidth,windowHeight)
    {
var position = new Array;
position[0]=0;
position[1]=0;

if (windowWidth>0)
  position[0]=(screen.width- windowWidth) /2 

if (windowHeight>0)
  position[1]= (screen.height- windowHeight) /2
  
return position
}
    </script>


</head>

<body>
    <form id="form1" runat="server">
    <div>
       <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:Add();"/>
    </div>
    </form>
    </body>
</html>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:Add();"/>
    </div>
    </form>
    </body>
</html>


Click on the Image




Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...