Sunday, 17 March 2013

Form Authentication Example



  mylogin.aspx page

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

<!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>
   
        Enter the user name:&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
        <br />
        <br />
        Enter the password:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtpassword" runat="server"></asp:TextBox>
   
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Login" />
    </form>
</body>
</html>

----------------------------------------------------------------
mylogin.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
    {
        if (FormsAuthentication.Authenticate(txtusername.Text, txtpassword.Text))
            FormsAuthentication.RedirectFromLoginPage(txtusername.Text, false);//login page display before running any page  //becuase of false cookies not store




    }


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

web.config code

<system.web>


<authentication mode="Forms">
   
                <forms loginUrl="mylogin.aspx" name="abc">
                    <credentials passwordFormat="Clear">
                          <user name="john" password="jim" />
                          <user name="sunita" password="sim" />
                    </credentials>
              </forms>
    </authentication>
    <authorization>
      <allow users="sunita" />
      <allow roles="hr"/>
      <deny users="*" />
    </authorization>

  </system.web>

---------------------------------------------
  <deny users="*" /> expect sunita all user denied

Relational tables output example for gridview


Create table Customer(custid int primary key, custname varchar(100))
insert into Customer values(1,'swapnil')
insert into Customer values(2,'Anil')
insert into Customer values(3,'jaydeep')


create table custtransactions( amount int, custid int constraint fk1 foreign key references Customer(custid))

insert into custtransactions values(1,1000)
insert into custtransactions values(1,1200)



 protected void Page_Load(object sender, EventArgs e)
    {
    cmd=  new SqlCommandBuilder(da);
    da.SelectCommand = new SqlCommand();

    string str = WebConfigurationManager.ConnectionStrings["constr"].ToString();
    Response.Write(str);
     da.SelectCommand.Connection = new SqlConnection(str) ;
    da.SelectCommand.CommandText = "select * from customers";
    da.Fill(ds, "customers");
    da.SelectCommand.CommandText = "select * from custtransactions";
    da.Fill(ds, "custtransactions");

        }




  protected void Button3_Click(object sender, EventArgs e)
    {
        DataRelation mycust =ds.Relations.Add("custtrans",ds.Tables[0].Columns["custid"], ds.Tables[1].Columns["custid"]);
        foreach (DataRow custRow in ds.Tables[0].Rows)
        {
            Response.Write("<p style='color:blue;font-size:2em'>");
            Response.Write(custRow["custid"].ToString());
           
             foreach (DataRow trans in custRow.GetChildRows(mycust))
            {
                Response.Write("<p style='color:green;font-size:2em'>");
                Response.Write(trans["amount"].ToString());
                Response.Write("<br/>");
                Response.Write(trans["transactiondate"].ToString());
                Response.Write("</p>");
            }
             Response.Write("<hr/>");
        }
   

    }

Repeater control Alternative colour for datagrid row


<body>
 
    <form id="form1" runat="server">
 
    <div style="height: 280px">
 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:hrdemoConnectionString %>"
            SelectCommand="SELECT * FROM [emp]"></asp:SqlDataSource>
 
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
        <HeaderTemplate>
<h1> Mahalaxmi : Annet</h1>      
        </HeaderTemplate>
        <ItemTemplate>
        <p style="background-color:Yellow"> The id of <%# DataBinder.Eval(Container,"DataItem.name")%> is <%# DataBinder.Eval(Container,"DataItem.id")%> </p>
        </ItemTemplate>
        <AlternatingItemTemplate>
         <p style="background-color:Green"> The id of <%# DataBinder.Eval(Container,"DataItem.name")%> is <%# DataBinder.Eval(Container,"DataItem.id")%> </p>
     
        </AlternatingItemTemplate>
        <FooterTemplate>
        <hr />
        </FooterTemplate>
        </asp:Repeater>
 
    </div>
 
    </form>

Saturday, 16 March 2013

Template field for gridview

1) right click gridview click addnew column
2)choose field type TemplateField give header text name clik on ok
3)after that right click gridview click on edit template link
4)drag label to itemtemplate field
5)click on label edit databinding link
6)Enter Code expression textbox XPath("productname")  click ok button
XPath use for xml file data.

store xml data to dataset


DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/products.xml"));
//Readxml read xml file and convert into datatable
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();

parameterise grid display


   SqlConnection con = new SqlConnection("Data Source=ANKITA-USER18\\SQL2008R2;Integrated Security=True;Initial Catalog=hrdemo");

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = new SqlCommand();
        //SqlCommandBuilder c = new SqlCommandBuilder(da);
        da.SelectCommand.CommandText = "select * from emp where id=@p1";
        da.SelectCommand.Connection = con;
        da.SelectCommand.Parameters.Add("@p1", SqlDbType.Int);
        da.SelectCommand.Parameters[0].Value = 1;
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

insert textbox values to the table


  SqlConnection con = new SqlConnection("Data Source=ANKITA-USER18\\SQL2008R2;Integrated Security=True;Initial Catalog=hrdemo");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into emp values(@p1,@p2)", con);
        cmd.Parameters.Add("@p1", SqlDbType.Int);
        cmd.Parameters.Add("@p2", SqlDbType.VarChar,40);
        cmd.Parameters["@p1"].Value = System.Convert.ToInt32(TextBox1.Text);
        cmd.Parameters[1].Value = TextBox2.Text;
        int ans = cmd.ExecuteNonQuery();
        Response.Write(ans.ToString() + "records inserted");

gridview bind


 SqlConnection con = new SqlConnection("Data Source=ANKITA-USER18\\SQL2008R2;Integrated Security=True;Initial Catalog=hrdemo") ;
    SqlDataAdapter da = new SqlDataAdapter("select * from emp", con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
   

dropdownlist bind


 SqlConnection con = new SqlConnection("Data Source=ANKITA-USER18\\SQL2008R2;Integrated Security=True;Initial Catalog=hrdemo");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from emp", con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            String str = dr[0].ToString() + "  :" + dr[1].ToString();
            DropDownList1.Items.Add(str);
        }