Friday, 14 November 2014

File upload also in database in asp.net

File upload also in database in asp.net
File Upload:-
1.Create a webform fileupload (you can give any name of webform)
2.Take a Fileupload Tool.
3.Also take a button.
4.right click on solution explorer and create a folder name of Image
It will look like this
















Double Click on button.
And write this code .
 if (FileUpload1.HasFile)
        {
           
         
                string file = FileUpload1.FileName;
                string path = (Server.MapPath("Image"));
                string fullpath = path + "\\" + FileUpload1.FileName;
                FileUpload1.SaveAs(fullpath);
             
            }
It will also store in data base just follow few steps create a database and take a field id,and image
..

Insert, update,delete Using 3 tier in asp.net c#

Insert, update,delete Using 3 tier in asp.net c#

step 1. Create a new website ...
new default page..

step 2.Go to solution explorer add new item. Add class and give name DAL  it will ask for create new app_code click on yes it will automatic add a new  folder.





 It will look like this after click on yes


Step :3 Repeat Step 2 And Give the Name BAL.

Step:4.Design Your Page



Step 5: In DAL File
Write that code You can difine your all logic here and also connection string

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
{

    SqlCommand cmd;
    SqlConnection con;
    SqlDataAdapter da;
    DataSet ds;

 public dal()
 {
        con = new SqlConnection(@"Data Source=TOPS-222\SQLEXPRESS;Initial Catalog=mytestdb;Integrated Security=True");
 }

    public DataSet disp()
    {
        cmd = new SqlCommand("disp",con);
        cmd.CommandType = CommandType.StoredProcedure;
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
    public void insert(bal bl)
    {
        cmd = new SqlCommand("inserts", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("name",bl.name);
        cmd.Parameters.AddWithValue("age", bl.age);
        cmd.Parameters.AddWithValue("city", bl.city);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

    public void update(bal bl)
    {
        cmd = new SqlCommand("updates", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("id",bl.id);
        cmd.Parameters.AddWithValue("name", bl.name);
        cmd.Parameters.AddWithValue("age", bl.age);
        cmd.Parameters.AddWithValue("city", bl.city);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

    public void delete(int a)
    {
        cmd = new SqlCommand("deletes", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("id", a);    
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}


step 6:-In BAL File Create property and the create the DAL Object here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for bal
/// </summary>
public class bal
{

    public int id { get; set; }
    public string name { get; set; }
    public int age { get; set; }
    public string city { get; set; }

    dal dl = new dal();

    public DataSet disp1()
    {
        return dl.disp();

    }
    public void insert1(bal bl)
    {
        dl.insert(bl);
    }
    public void update1(bal bl)
    {
        dl.update(bl);
    }
    public void delete1(int a)
    {
        dl.delete(a);
    }
}

Step:7
.Default.aspx.cs Code File Write That 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 bl = new bal();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            disp();
        }
    }
    public void disp()
    {
        GridView1.DataSource = bl.disp1();
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        bl.name = TextBox1.Text;
        bl.age = int.Parse(TextBox2.Text);
        bl.city = TextBox3.Text;
        bl.insert1(bl);
        disp();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int a = int.Parse(((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text);
        bl.delete1(a);
        disp();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        disp();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        disp();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        bl.id = int.Parse(((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text);
        bl.name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
        bl.age = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text);
        bl.city = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4")).Text;

        bl.update1(bl);
        GridView1.EditIndex = -1;
        disp();
    }
}

...


If you have any query then contact me

Login Page using 3 tier asp.net with c#

Login Page using 3 tier asp.net with c#

See my previous post insert,update,and delete with 3 tier in asp .net
how to create tree tire.


you must have craate database

field...  id (int) autoincreament
            email_id varchar(100),
            pass varchar(100)

I used store procedure in this example.....for login

step 1.Design new page User name and Password..Like this.





Step:2-In DAL Class Write this code

 public int login(bal bl)
    {
        con.Open();
        cmd = new SqlCommand("login", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("email_id", bl.email_id);
        cmd.Parameters.AddWithValue("pass", bl.pass);
        bl.id = Convert.ToInt32(cmd.ExecuteScalar());
        con.Close();
        return bl.id;

    }

Login is store procedure you can write direct query

select * from login where email_id=@email_id and pass=@pass



Step:3-In Bal Class Write This Code

define propert.....
public class bal
{
dal dl= new dal;
public int email_id { get; set; }
public int pass { get; set; }
}

  public int login1(bal bl)
    {
        return dl.login(bl);
    }

Step:4- On butten Click Event Write This Code

 protected void Button1_Click(object sender, EventArgs e)
    {  
       public int id { set; get; }
        bl.email_id = TextBox3.Text;
        bl.pass = TextBox4.Text;
        int id=  bl.adminlo(bl);
    if (id> 0)
    {
     
        Session["user"] = TextBox3.Text;
        Session["pass"] = TextBox4.Text;
        Response.Redirect("Default.aspx");
    }
    else
    {
        Label2.Text = "Invalid User name or password";

    }
}

enjoy.........


if any query contact me

Constructor in C#

Constructors in C#

A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
A constructor will have exact same name as the class and it does not have any return type. Following example explains the concept of constructor:
using System;
namespace LineApplication
{
   class Line
   {
      private double length;   // Length of a line
      public Line()
      {
         Console.WriteLine("Object is being created");
      }

      public void setLength( double len )
      {
         length = len;
      }
      public double getLength()
      {
         return length;
      }

      static void Main(string[] args)
      {
         Line line = new Line();  
         // set line length
         line.setLength(6.0);
         Console.WriteLine("Length of line : {0}", line.getLength());
         Console.ReadKey();
      }
   }
}

Destructors in C#


Destructors in C#
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.
Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc. Destructors cannot be inherited or overloaded.
Following example explains the concept of destructor:
using System;
namespace LineApplication
{
   class Line
   {
      private double length;   // Length of a line
      public Line()  // constructor
      {
         Console.WriteLine("Object is being created");
      }
      ~Line() //destructor
      {
         Console.WriteLine("Object is being deleted");
      }

      public void setLength( double len )
      {
         length = len;
      }
      public double getLength()
      {
         return length;
      }

      static void Main(string[] args)
      {
         Line line = new Line();
         // set line length
         line.setLength(6.0);
         Console.WriteLine("Length of line : {0}", line.getLength());          
      }
   }
}

OOPS Concept

OOPS Concept

OOPS....

Opes is not a programming language it is just a way to manage your code and application
it is a concept not a language.

Class......

Class is a collection of data member and member function.

Object......

It is instance of class.

Abstraction.....

It is focus on what the object does instead of how it does ?
to represent the essential features without representing the back ground details.

It means putting all the variables and methods in a class that are necessary.

Encapsulation.....

Wrapping up a data member and member function into a single unit (like class)
is called encapsulation.
for ex..
it means like your bag in which you can keep your pen,book it means it is the property of encapsulation data member and function.

Inheritance.......

It means relationship between two or more class class is called inheritance.
when a class include the property in another class is known as inheritance.

Polimorphism......

It means one name in multiple form.
many form of the single unit is called the Polimorphism.
for ex.
A teacher behave students
a teacher behave his/her senior.

Generic......

It provide the type safety to your class at the compile time.
while creating the data structure you never specify the data type at the time of declaration.

Method Overloading.........

The method have same name but different signature.
at the compile time compiler should decide that which one will call.
A method can overload on basis of following properties.

1. Different number of parameter.
2. Same number of parameter but different type.
3. Same number of parameter and type but different order.

Method Overriding.......

It is able to change the behavior of the method.
the method have same name and same signature.

Delegates....

Delegates object hold the reference of the method it is like class that hod the reference and called when necessary.
It is possible that delegate holds the multiple object references is called multicast delegates.

Constructor.......

It is special method of the class which is call automatically when instance of class is created.

Interface.........

The interface has no implementation on it's own because it contains only definition of method without any method body.