Thursday, October 31, 2013

sessions

login page:

 protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\codem\Documents\Visual Studio 2012\WebSites\WebSite12\App_Data\Database.mdf;Integrated Security=True");
        string s="select * from login where mobile='"+txtMobile.Text+"'and password='"+txtPassword.Text+"'";


        SqlCommand cmd = new SqlCommand(s,con);
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        if (dt.Rows.Count>0)
        {

            Session["a"] = "gmail";
            Response.Redirect("home.aspx");

        }
        else
        {
            Response.Write("fail");
        }
     



        con.Close();
     
    }




Home page.aspx
 protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["a"] != null)
        {

         
        }
        else
        {
            Response.Redirect("home.aspx");
        }


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session.Remove("a");

        Response.Redirect("login.aspx");
       
    }

printing many images at a time

using System.IO;public partial class _Default : System.Web.UI.Page
{

public  string strFileName { get; set; }protected void Page_Load(object
sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
         if(FileUpload1.HasFile)
        {
             string fileName=FileUpload1.FileName;
        FileUpload1.SaveAs(Server.MapPath("images/") +
FileUpload1.FileName);
         }
        foreach(string strFileName in
Directory.GetFiles(Server.MapPath("images/")))
        {
            ImageButton imageButton=new ImageButton();
            FileInfo fileInfo=new FileInfo(strFileName);
            imageButton.ImageUrl="images/"+fileInfo.Name;
           imageButton.Click +=new
ImageClickEventHandler(imageButton_click);
            Panel1.Controls.Add(imageButton);
        }
    }

    private void imageButton_click(object sender, ImageClickEventArgs e)
    {
        throw new NotImplementedException();
    }




    }

Login Page 2

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection cn = new
SqlConnection("uid=sa;password=123;Database=raj2;server=codem-PC");
        cn.Open();
        SqlCommand cmd = new SqlCommand("select*from hai where name='" +
TextBox1.Text + "' and id='" + TextBox2.Text + "'", cn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            Label3.Text = "ss";
        }
        else
        {
            Label3.Text = "ff";
        }
    }
}

Wednesday, October 30, 2013

coockies 2

try
        {
            HttpCookie anvesh = new HttpCookie("anvesh");
            anvesh.Value = TextBox1.Text;
            anvesh.Expires = DateTime.Now.AddMinutes(0.1);
            Response.Cookies.Add(anvesh);
       

        }
        catch
        {

        }










TextBox1.Text = "";
            try
        {
            TextBox1.Text = Request.Cookies["anvesh"].Value;
        }
        catch
        {
            Response.Write("coockie expired");
        }

simple cookie

 Response.Cookies["StudentCookies"].Value = TextBox1.Text;
         Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddMinutes(1);





TextBox1.Text = Request.Cookies["StudentCookies"].Value;

Tuesday, October 29, 2013

web.config

 <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
  </appSettings>

login page

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(@"Data Source=CODEM-PC;Initial Catalog=technology;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
        SqlCommand cmd = new SqlCommand("select * from anvesh where username=@u and password=@p", con);
        cmd.Parameters.AddWithValue("@u", TextBox1.Text);
        cmd.Parameters.AddWithValue("@p", TextBox2.Text);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Response.Redirect("retrive.aspx");
        }
        else
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
        }

    }
}

file upload

private void button1_Click(object sender, EventArgs e)
        {
            DialogResult r = openFileDialog1.ShowDialog();

            textBox1.Text = openFileDialog1.FileName.ToString();

        }

data retrieving 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\codem\documents\visual studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=True");
            SqlCommand cmd;
            con.Open();
            cmd = new SqlCommand("select * from kkkk", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            foreach(DataRow dr in dt.Rows)
            {
                int i =Convert.ToInt32(txtrow.Text) ;
                if(dt.Rows[i]!=null)
                {
                    label1.Text = dt.Rows[i]["name1"].ToString();
                    label2.Text = dt.Rows[i]["name2"].ToString();
                }
                break;

            }
            cmd.ExecuteNonQuery();
           
            con.Close();
           
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

data retriving from sql

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=CODEM-PC;Initial Catalog=technology;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
        SqlCommand cmd = new SqlCommand(("select *  from anvesh"), con);
        con.Open();
      
       
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                if (dr.Read())
                {
                    fn.Text = dr["firstName"].ToString();
                    ln.Text = dr["lastName"].ToString();
                    un.Text = dr["username"].ToString();
                    p.Text = dr["password"].ToString();
                    dob.Text = dr["dob"].ToString();
                    g.Text = dr["gender"].ToString();
                    m.Text = dr["mobile"].ToString();
                    Label1.Text = dr["email"].ToString();
                }
            }
        }
        
        


        cmd.ExecuteNonQuery();
        con.Close();

    }
}

new sql connnection

 SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\codem\documents\visual studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=True");
            SqlCommand cmd;
            con.Open();
            cmd=new SqlCommand("insert into kkk values('"+textBox1.Text+"','"+textBox2.Text+"')",con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("inserted");
            con.Close();

data transfer betwen forms

Printing

<html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script> 
<script type="text/javascript">

    function PrintElem(elem)
    {
        Popup($(elem).html());
    }

    function Popup(data) 
    {
        var mywindow = window.open('', 'my div', 'height=400,width=600');
        mywindow.document.write('<html><head><title>my div</title>');
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');

        mywindow.print();
        mywindow.close();

        return true;
    }

</script>
</head>
<body>

<div id="mydiv">
    This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante. 
</div>

<div>
    This will not be printed.
</div>

<div id="anotherdiv">
    Nor will this.
</div>

<input type="button" value="Print Div" onclick="PrintElem('#mydiv')" />

</body>
</html>

FIle Upload ANd preview

FileUpload1.SaveAs(Server.MapPath("images/") + FileUpload1.FileName);
Image2.ImageUrl = "images/" + FileUpload1.FileName;