Tuesday, 25 June 2013

Registration and Login page

Registration

.aspx:

<body>
    <form id="form1" runat="server">
    <div>
     <asp:Panel ID="pnlLogin" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
        Style="margin-top: 14px; margin-left: 40px; height: 400px;">
        <h1>
            Register Here</h1>
        <table>
            <tr>
                <td>
                    <asp:Label ID="uname" runat="server" Text="username"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtuname" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:FileUpload ID="filup" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblpsw" runat="server" Text="password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtpsw" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblname" runat="server" Text="Name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblgen" runat="server" Text="Gender"></asp:Label>
                </td>
                <td>
                    <asp:RadioButtonList ID="rblst" runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem Value="0">Male</asp:ListItem>
                        <asp:ListItem Value="1">Female</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lbladd" runat="server" Text="Address"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtadd" runat="server" TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblstate" runat="server" Text="State"></asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlst" runat="server" Enabled="true">
                        <asp:ListItem Value="0">Select state</asp:ListItem>
                        <asp:ListItem Value="1">Ap</asp:ListItem>
                        <asp:ListItem Value="2">karnataka</asp:ListItem>
                        <asp:ListItem Value="3">maha</asp:ListItem>
                        <asp:ListItem Value="4">tamil</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblcon" runat="server" Text="Country"></asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlcou" runat="server" Enabled="true">
                        <asp:ListItem Value="0">Select Country</asp:ListItem>
                        <asp:ListItem Value="1">India</asp:ListItem>
                        <asp:ListItem Value="2">USA</asp:ListItem>
                        <asp:ListItem Value="3">England</asp:ListItem>
                        <asp:ListItem Value="4">China</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblnum" runat="server" Text="PhNum"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtph" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnsub" runat="server" Text="Submit" OnClick="btnsub_click" />
                </td>
                <td>
                    <asp:Button ID="btnReset" runat="server" Text="Reset" OnClick="btnReset_click" />
                </td>
            </tr>
        </table>
        <br />
        <asp:Label ID="lblmsg" runat="server" ForeColor="Green"></asp:Label>
         <asp:Label ID="lblerr" runat="server" ForeColor="red"></asp:Label>
       
    </asp:Panel>
    </div>
    </form>
</body>


Registration.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 _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["vamsi"].ToString());
    SqlCommand cmd;
    SqlDataAdapter da;
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnsub_click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            string query = "insert into tb_reg([Username],[password] ,[name],[gender],[address],[state],[country],[phonenum],[images])values('" + txtuname.Text + "','" + txtpsw.Text + "','" + txtuname.Text + "','" + rblst.SelectedItem.Text + "','" + txtadd.Text + "','" + ddlst.SelectedItem.Text + "','" + ddlcou.SelectedItem.Text + "','" + txtph.Text + "','"+filup.PostedFile.FileName+"')";
            cmd = new SqlCommand(query,con);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            string saveimage = Server.MapPath("~\\"+"images") +"\\"+filup.PostedFile;
            con.Close();
            Reset();
            lblmsg.Text="inserted successfully";

           
        }
        catch(Exception ex)
        {
            lblmsg.Text = ex.Message;
        }
     
    }

    private void Reset()
    {
        txtuname.Text = "";
        txtpsw.Text = "";
        txtph.Text = "";
        txtname.Text = "";
        txtadd.Text = "";
        ddlst.SelectedIndex = -1;
        ddlcou.SelectedIndex = -1;
        rblst.SelectedIndex = -1;


    }
    protected void btnReset_click(object sender, EventArgs e)
    {
        Reset();
    }
}



login.aspx 

<body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="pnlLogin" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
        Style="margin-top: 14px; margin-left: 40px; height: 300px;">
        <h1>
            Login Here</h1>
        <table style="margin-top: 30px; margin-left: 50px; height: 105px;">
            <tr>
                <td>
                    <asp:Label ID="lblUsername" runat="server" Text="User name :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblPassword" runat="server" Text="Password :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnLogin" runat="server" Text="Login"
                        onclick="btnLogin_Click" />
                </td>
                <td>
                    <asp:Button ID="btnReset" runat="server" Text="Reset" onclick="btnReset_Click"  />
                </td>
            </tr>
        </table>
        <br />
        <asp:Label id="lblmsg" runat="server"></asp:Label>
        <asp:Label ID="lblerr" runat="server"></asp:Label>
    </asp:Panel>
    </div>
    </form>
</body>
login.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 login : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["vamsi"].ToString());
    SqlCommand cmd;
    SqlDataAdapter da;
    SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            String query = "select Username , password from tb_reg where Username='" + txtUsername.Text + "' and password='" + txtPassword.Text + "'";
            cmd = new SqlCommand(query, con);
            cmd.CommandType = CommandType.Text;
            dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                Response.Redirect("~/welcome.aspx");
            }

            else
            {
                lblerr.Text = "longin failed";
            }
           
            con.Close();
            Resetall();
        }
        catch(Exception ex)
        {
            lblerr.Text = ex.Message;
        }
    }

    private void Resetall()
    {
        txtUsername.Text = "";
        txtPassword.Text = "";
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Resetall();
    }
}
 Welcome.aspx 


<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lab1" runat="server" Text="Welcome to User"  ForeColor="BlueViolet" Font-Size="XX-Large"></asp:Label>
    </div>
    </form>
</body>

 DATABASE:
create database vamsi

create table  tb_reg

(Username varchar(50),password varchar(50),
name varchar(50), gender varchar(50),
address varchar(50),
state varchar(10),country varchar(50),phonenum varchar(50),images varchar(50))

 WEB CONFIG
<connectionStrings>
        <add name="vamsi" connectionString="Data Source=MAIN;Initial Catalog=vamsi;User Id=sa;Password=123; Persist Security Info=true;" providerName="System.Data.SqlClient"/>
       
    </connectionStrings>




 

Monday, 24 June 2013

Registration form in asp.net using stored procedures

Introduction
Here I will explain how to insert data into database tables using Stored Procedure.

Database: 

Create Database:-

create Database sp_exp

create Table:-

create table student(id int identity(1000,1) primary key,name varchar(200),cell varchar(200),email varchar(200))

create Stored Procedure:-

create procedure sp_student_insert(@name varchar(200),@cell varchar(200),@email varchar(200))as
begin
insert into student(name,cell,email)values(@name,@cell,@email)
end

Aspx Page: 

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Registration Form in asp.net using stored procedures.....!
    </h2>
    <div>
        <table>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="mobile"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtmob" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="email"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_Click" />
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Label ID="lblerror" runat="server" ForeColor="Green"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
</asp:Content>

C#.aspx code:-

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 _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void submit_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open(); 
            string sql = "sp_student_insert";  
            SqlParameter[] par = new SqlParameter[3];  
            par[0] = new SqlParameter("@name", txtname.Text);
            par[1] = new SqlParameter("@cell", txtmob.Text); 
            par[2] = new SqlParameter("@email", txtemail.Text);   
            SqlCommand cmd = new SqlCommand(sql, con);  
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter a in par)
            {
                cmd.Parameters.Add(a);
            }
            string UserId = Convert.ToString(cmd.ExecuteNonQuery());
            if (UserId.Length > 0)
            {
                lblerror.Text = "Data inserted successfully...!";
                txtemail.Text = string.Empty;
                txtmob.Text = string.Empty;
                txtname.Text = string.Empty;
            }
            else
            {
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = ex.Message;
        }
        finally
        {
            con.Close();
        }
    }


}

Web.config:-

<connectionStrings>
        <add name="con" connectionString="Data Source=MAIN;Initial Catalog=sp_exp;Persist Security    Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
 </connectionStrings> 



output screen:-

 


In sql server:

Sunday, 23 June 2013

How to print multiple pages from data grid with header and footer

i used this code to print datagridview but it is working for single page only.  i need to print multiple pages

 private void print_Click(object sender, EventArgs e)
       {
           PrintDialog printDialog1 = new PrintDialog();
           printDialog1.Document = printDocument1;
           DialogResult result = printDialog1.ShowDialog();

           if (result == DialogResult.OK)
           {
               printDocument1.Print();
           }

       }
private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
       {
           Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
           this.dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
           e.Graphics.DrawImage(bm, 0, 0);
       }

2) And one more is how to add Header and Footer to datagridview

3) How to apply Style Sheet to forms in windows application?

I want all these things in windows c# application

 

Saturday, 8 June 2013

 3 TIER ARCHITECTURE 

aspx code
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <table>
        <tr>
            <td>
                <asp:Label ID="l1" runat="server" Text="Id"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtid" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="Address"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server" Text="Email"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtemai" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label4" runat="server" Text="Cell"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtcell" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Label ID="lblerror" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
</asp:Content>





c# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string sql = "insert into tbl_student([Name],[Address],[Email],[Mobile])values('" + txtname.Text + "','" + txtaddress.Text + "','" + txtemai.Text + "','" + txtcell.Text + "')";
            int id=bal.insert(sql);
            if (id == 1)
            {
                lblerror.Text = "data inserted";
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = ex.Message;
        }
    }
}

Add App_code in that add  Two folders BAL and DAL add C# .cs files in both folders with the name BAL.cs,DAL.cs insert the following cde in BAL

BAL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for BAL
/// </summary>
public class BAL
{
    DAL dal = new DAL();
public BAL()
{
//
// TODO: Add constructor logic here
//
}
    public int insert(string sql)
    {
        try
        {
            int id=dal.insert(sql);
            return id;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

}


DAL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["class"].ToString());
public DAL()
{
//
// TODO: Add constructor logic here
//
}
    public int insert(string sql)
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            int id=cmd.ExecuteNonQuery();
            return id;

        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

}


DATABASE
create table admin ( id int identity(1000,1),Name varchar(200),Address varchar(200),Email varchar(200),Mobile varchar(200))

Webconfig

  <add name="con" connectionString="Data Source=.;Initial Catalog=3tirer;Integrated Security=true;"/>

OUTPUT



1 TIER ARCHITECTURE 

 .aspx code

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

    CodeFile="Default.aspx.cs" Inherits="_Default" %>


<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <table>
        <tr>
            <td>
                <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server" Text="Email"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtemai" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label4" runat="server" Text="Cell"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtcell" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="btnsubmit" runat="server" Text="Submit" 
                    onclick="btnsubmit_Click" style="height: 26px"  />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Label ID="lblerror" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
</asp:Content>

c# code

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 _Default : System.Web.UI.Page
{
    SqlConnection cat = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        try
        {
            cat.Open();
            string sql = "insert into admin([name],[cell],[email])values('" + txtname.Text + "','" + txtcell.Text + "','" + txtemai.Text + "')";
            SqlCommand cmd = new SqlCommand(sql, cat);
            int id=(cmd.ExecuteNonQuery());
            if (id == 1)
            {
                lblerror.Text = "data inserted";
            }
            else
            {
                lblerror.Text = "try again";
            }

        }
        catch (Exception ex)
        {
            lblerror.Text = ex.Message;
        }
        finally
        {
            cat.Close();
        }
    }
}
DATABASE
create table admin ( id int identity(1000,1),name varchar(200),cell varchar(200),email varchar(200))

Webconfig
  <add name="con" connectionString="Data Source=.;Initial Catalog=1tirer;Integrated Security=true;"/>

OUTPUT





Receive All Free Updates Via Facebook.