Thursday, July 3, 2014

Data List And Repeater

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="Default.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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <asp:Repeater ID="rpt" runat="server">
      <ItemTemplate>
<table>
            <tr>
              <td>
<asp:Label ID="lblfirstname" runat="server" Text='<%#Bind("Firstname") %>' Font-Bold="true"></asp:Label>
<asp:Label ID="lblLastname" runat="server" Text='<%#Bind("LastName") %>' Font-Bold="true"></asp:Label>
            
              </td>
            </tr>
            <tr>
              <td>
                <asp:Label ID="lbldesignation" runat="server" Text='<%#Bind("designation") %>' Font-Bold="true"></asp:Label>

              </td>
            
            </tr>
            <tr>
              <td>
                <asp:Label ID="lblsalary" runat="server" Text='<%#Bind("Salary") %>' Font-Bold="true"></asp:Label>

              </td>
            </tr>
          </table>
      </ItemTemplate>
    </asp:Repeater>
      <asp:DataList ID="dtl" runat="server" RepeatColumns="1" BorderColor="Gray" BorderStyle="Solid">
        <ItemTemplate>
          <table>
            <tr>
              <td>
<asp:Label ID="lblfirstname" runat="server" Text='<%#Bind("Firstname") %>' Font-Bold="true"></asp:Label>
<asp:Label ID="lblLastname" runat="server" Text='<%#Bind("LastName") %>' Font-Bold="true"></asp:Label>
            
              </td>
            </tr>
            <tr>
              <td>
                <asp:Label ID="lbldesignation" runat="server" Text='<%#Bind("designation") %>' Font-Bold="true"></asp:Label>

              </td>
            
            </tr>
            <tr>
              <td>
                <asp:Label ID="lblsalary" runat="server" Text='<%#Bind("Salary") %>' Font-Bold="true"></asp:Label>

              </td>
            </tr>
            <tr>
             <td>
               <asp:Button ID="btnsubmit" runat="server" Text="Submit" CommandArgument=<%#Bind("Salary") %> OnCommand="submit_commend" />
             </td>
            </tr>
          </table>
        </ItemTemplate>
      </asp:DataList>
    </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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        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.Rows.Add("Trinadh", "K", "Sr.developer", "100000");
        dt.Rows.Add("srinivas", "M", "Sr.developer", "1000000");
        dt.Rows.Add("Siva", "Master", "Sr.developer", "500000");
        dt.Rows.Add("Mahesh", "Agadu", "Sr.developer", "700000");
        dt.Rows.Add("Vishnu", "Agutadu", "Sr.developer", "800000");
        dt.Rows.Add("Rakesh", "Danush", "Sr.developer", "900000");

        dtl.DataSource = dt;
        dtl.DataBind();

        rpt.DataSource = dt;
        rpt.DataBind();



    }
    public void submit_commend(object sender, CommandEventArgs e)
  
    {
        string id = e.CommandArgument.ToString();
    }
}



Monday, June 30, 2014

multi Stored Procedure Binding DropDown list Selected Values To bing Gridview Using Sessions

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 drpgrid : System.Web.UI.Page
{
    adosession obj;
    protected void Page_Load(object sender, EventArgs e)
    {

        obj = new adosession();



        DataSet ds = obj.bindgrid(Convert.ToInt32(Session["id"]), "ById");


        DataTable dt = new DataTable();
        dt.Columns.Add("id", typeof(int));
        dt.Columns.Add("name", typeof(string));
        dt.Columns.Add("address", typeof(string));

        if (Session["dt"] == null)
        {
            dt.Rows.Add(ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][1].ToString(), ds.Tables[0].Rows[0][2].ToString());
            grd.DataSource = dt;
            grd.DataBind();
            Session["dt"] = dt;
        }
        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());
            Session["dt"] = dt2;
            grd.DataSource = dt2;
            grd.DataBind();
        }

    }
}

 -----------------------------------------------------------------------------------
Stored Procedure
USE [naresh]
GO

/****** Object:  StoredProcedure [dbo].[sp_getgridvalues]    Script Date: 06/30/2014 13:04:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[sp_getgridvalues] 
@id int,
@type varchar(50)

as
begin
if(@type='Byid')

select id,name,address from gridsession where id=@id
else
select * from gridsession
end

GO

Multi Stored Procedute

USE [naresh]
GO

/****** Object:  StoredProcedure [dbo].[sp_getgridvalues]    Script Date: 06/30/2014 13:04:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[sp_getgridvalues] 
@id int,
@type varchar(50)

as
begin
if(@type='Byid')

select id,name,address from gridsession where id=@id
else
select * from gridsession
end

GO

Sending Mail From Asp.net Page To Gmail Using SMTP. Gmail Port Number 587



<body>
    <form id="form1" runat="server">
    <div align="center">
    <h3>Send Mail To Your's Gmail Using ASp.net</h3>
    <table>
   
    <tr>
    <td>Gmail User</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtfrom" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>Gmail Password</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtpassword" TextMode="Password" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>Subject</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtsub" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>To</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtto" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>Body</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtbody" runat="server"  TextMode="MultiLine" Columns="30" Rows="10"></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
   
    <td></td><td></td>
    <td>
   
        <br />
   
    <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" />
    </td>
    </tr>
    </table>
   
    </div>
    </form>
</body>


ASPX.CS Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;


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

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
       MailMessage newmsg = new MailMessage();

       newmsg.From = new MailAddress(txtfrom.Text);
      
        newmsg.Subject = txtsub.Text;
        newmsg.To.Add(txtto.Text);
        newmsg.Body = txtbody.Text;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential(txtfrom.Text, txtpassword.Text);
         
        smtp.EnableSsl = true;
        smtp.Send(newmsg);
        Response.Write("msg is send");

    }
}


Sunday, June 29, 2014

Grid Add 10 records at a Time using sessions and Onrowdatabound



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

<!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="grdout" AutoGenerateColumns="false" runat="server" OnRowDataBound="nnn" DataKeyNames="Id">
      <Columns>
        <asp:TemplateField HeaderText="ID">
          <ItemTemplate>
            <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
          <ItemTemplate>
            <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Role">
          <ItemTemplate>
            <asp:TextBox ID="txt3" runat="server"></asp:TextBox>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Gender">
          <ItemTemplate>
           <asp:DropDownList ID="drp" runat="server"></asp:DropDownList>
          </ItemTemplate>
        </asp:TemplateField>

      </Columns>
   
    </asp:GridView>
   
        <br />
        <asp:Button ID="Button1" runat="server" Text="ADD NEW ROW"
            onclick="Button1_Click" />
   
    </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;
using System.Data.SqlClient;

public partial class Grid : System.Web.UI.Page
{
    //int count;
    protected void Page_Load(object sender, EventArgs e)
    {
        int count = 10;
        if (!IsPostBack)
        {
            Session.Clear();
            for (int i = 0; i <= count; i++)
            {

               
                naresh();

            }
         }
       
      
    }
    protected void nnn(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           
            var ddl = (DropDownList)e.Row.FindControl("drp");
          
            SqlConnection con = new SqlConnection("Data Source=KCLINK-45-PC\\SQLEXPRESS;Initial Catalog=naresh;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("select * from gender");
            cmd.Connection = con;
            con.Open();
            SqlDataAdapter dap = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            ddl.DataSource = ds;
            ddl.DataTextField = "gender";
            ddl.DataValueField = "id";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("--Select--", "0"));

          
        }

    
       
    }
    private void naresh()
    {
        DataTable dt = new DataTable();

        dt.Columns.Add("Id", typeof(string));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Role", typeof(string));
        dt.Columns.Add("Gender", typeof(string));
        if (Session["dt"] == null)
        {

            dt.Rows.Add("", "", "", "");
            Session["dt"] = dt;
            grdout.DataSource = dt;
            grdout.DataBind();

        }
        else
        {
            DataTable dt2 = (DataTable)Session["dt"];
            dt2.Rows.Add("", "", "", "");
            Session["dt"] = dt2;
            grdout.DataSource = dt2;
            grdout.DataBind();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dtt = new DataTable();

        dtt.Columns.Add("Id", typeof(string));
        dtt.Columns.Add("Name", typeof(string));
        dtt.Columns.Add("Role", typeof(string));
        dtt.Columns.Add("Gender", typeof(string));
        if (Session["dt"] == null)
        {

            dtt.Rows.Add("", "", "", "");
            Session["dt"] = dtt;
            grdout.DataSource = dtt;
            grdout.DataBind();

        }
        else
        {
            DataTable dt3 = (DataTable)Session["dt"];
            dt3.Rows.Add("", "", "", "");
            Session["dt"] = dt3;
            grdout.DataSource = dt3;
            grdout.DataBind();
        }
    }

}