Monday, 30 June 2014

Method Overloading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    public class Program : abc              \\Main class
    {
        public static void Main(string[] args)
        {
            System.Console.WriteLine("I m main class");
            abc x = new abc();
            x.add(7, 9);
            x.add("hello ", "world");
        }
    }
    public class abc                                   \\ inheritted class for Method overloading
    {
        public void add(int a, int b)
        {
            int c = a + b;
            System.Console.WriteLine(c);
        }
        public void add(string a, string b)         \\ Overloaded method(add())
        {
            string c = a + b;
            System.Console.WriteLine(c);
        }
    }
 

Multi Level Inheritance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class xyz                                             \\ First class
{
    public void demo()
    {
        System.Console.WriteLine("First class");
    }

}
public class abc : xyz                                       \\ second class which inherit the first class
{
    public void demo()
    {
        System.Console.WriteLine("Second class abc");
    }
}
        public class Program:abc                              \\ Main class
        {
        public static void Main(string []args)
        {
            System.Console.WriteLine("I m main class");
            abc k = new abc();                                   \\ creating object
            k.demo();
            xyz p = new xyz();
            p.demo();
        }
        }
     

Single Inheritance

\\ Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

        public class Program : abc     \\ Main Class
        {
        public static void Main(string []args)       \\ Main Function
        {
            System.Console.WriteLine("I m main class");
            Program x = new Program();
            x.demo();
            abc k = new abc();                           \\ creating object of inherited class
            k.demo();
        }
        public void demo()                                \\Function in class
        {
            System.Console.WriteLine("function in main class");
        }
        }
        public class abc                                        \\base class or inherited class
        {
            public void demo()
            {
                System.Console.WriteLine("class abc");
            }
        }

Sunday, 29 June 2014

Check the IP Address is valid or not.

// First we add Namespaces
 
using System.Data;
using System.Net;


protected void Button5_Click(object sender, EventArgs e)
{
        try
        {
            IPAddress address = IPAddress.Parse(TextBox1.Text);        //Check the IP address is Valid or Not
            Reponse.Write(" Valid IP address ");
        }
        catch(Exception ex)
        {
            Response.Write("incorrect IP address");
        }
}

Thursday, 26 June 2014

Mailing System(Sending confirmation mail for Complaints or vallidation messge sending vai mail)

//First we add namespaces
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;

  protected void SendMail()      // Function for sending confirmation mail to customers
    {
   try
        {

            MailMessage msg = new MailMessage("Email address of sender", Email address of receiver, "Subject", " Message for receiver");
            msg.IsBodyHtml = true;
            SmtpClient clnt = new SmtpClient();
            clnt.Host = "smtp.gmail.com";
            clnt.Port = 587;
            clnt.EnableSsl = true;
            NetworkCredential crd = new NetworkCredential("Email address of sender", "Password of sender");
            clnt.Credentials = crd;
            clnt.Send(msg);
            Label4.Text = "Successfully Sent";
}
        catch (Exception)
        {
            Label4.Text = "There is some error plzzz try again later";
        }
 
    }

Wednesday, 25 June 2014

Griedview (EDIT ,UPDATE ,DELETE) operations

// First we add namespaces.
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;

public partial class admin_registered : System.Web.UI.Page
{
// create connection
    SqlConnection con = new SqlConnection("Data Source=RAWAT-PC;Initial Catalog=mobile;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            show();
        }
    }
    void show()         // function for display data of gridview
    {
        con.Open();
        string s = "select * from customer";
        SqlCommand cmd = new SqlCommand(s, con);
        cmd.ExecuteNonQuery();
        GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
        con.Close();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

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

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
    }
    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {

    }

// Code for Update or Edit details with the help of finding the Controls
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox t1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("textbox1");  \\Finds controls
        TextBox t2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("textbox2");
        TextBox t3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("textbox3");
        TextBox t4 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("textbox4");
        TextBox t5 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("textbox5");

        Label lb = (Label)GridView1.Rows[e.RowIndex].FindControl("label3");
        con.Open();
        string s = "update customer set name='" + t1.Text + "',dob='" + t2.Text + "',mob='" + t3.Text + "',username='" + t4.Text + "',password='"+t5.Text+"' where email='" + lb.Text + "'";
        SqlCommand cmd = new SqlCommand(s, con);
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        con.Close();
        show();

    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }

//Code for delete details or row
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lb = (Label)GridView1.Rows[e.RowIndex].FindControl("label3");
        con.Open();
        string s = "delete from customer where email='" + lb.Text + "'";
        SqlCommand cmd = new SqlCommand(s, con);
        cmd.ExecuteNonQuery();
        con.Close();
        show();
    }
}

Timmer for Page Redirecting using javascript

<script type="text/javascript">

    function Redirect() {
        window.location = "home.aspx";
    }
    document.write("");
    setTimeout('Redirect()', 1000);

</script>

Validation Web form Using JAVASCRIPT

<script type="text/javascript">
        function Getname() {

            var param = document.getElementById('<%=DropDownList1.ClientID%>').value;
            var param1 = document.getElementById('<%=TextBox1.ClientID%>').value;
            var param2 = document.getElementById('<%=TextBox3.ClientID  %>').value;
            var param3 = document.getElementById('<%= TextBox4.ClientID %>').value;
            var param4 = document.getElementById('<%= TextBox5.ClientID %>').value;
            var param5 = document.getElementById('<%=TextBox6.ClientID%>').value;
            var param6 = document.getElementById('<%=TextBox7.ClientID%>').value;
            var param7 = document.getElementById('<%=DropDownList2.ClientID%>').value;
            if (param == '') {
                alert("Please select Category");
                return false;
            }
            if (param1 == '') {
                alert("Please enter Ad name");
                return false;
            }
            if (param2 == '') {
                alert("Please enter Description");
                return false;
            }
            if (param3 == '') {
                alert("Please enter  Price");
                return false;
            }
            if (param4 == '') {
                alert("Please enter Contact name");
                return false;
            }
            if (param5 == '') {
                alert("Please enter Email address");
                return false;
            }
            if (param6 == '' ) {
                alert("Please enter Phone no.");
                return false;
            }
            if (param7 == '') {
                alert("Please select City");
                return false;
            }
        }
</script>

Image Upload Code



 if (FileUpload1.HasFile)
  {
       string filename = FileUpload1.FileName;
       FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Data/") + filename);   // Save image in Data folder
       string path = "~/Data/" + filename.ToString();
       var i = bobj.bad( path);
       Response.Redirect("post_ad2.aspx");
        }

Login or Logout process with the help of session

First check session is create or not
if (Session["email"] == null)
        {
            LinkButton2.Text = "Login";
        }
        else
        {
            LinkButton2.Text = "Logout";
            Label1.Text = "Welcome "+Session["email"].ToString();
        }

 Now delete all session if create(at the time of click on Logout button)
if (Session["email"] == null)         
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            HttpContext.Current.Session["username"] = null;        //desable session
            HttpContext.Current.Session["password"] = null;
            HttpContext.Current.Session.Abandon();
            Response.Redirect("home.aspx");
        }

Generate Random no.(PASSWORD)

       string v = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        Random ran = new Random();
        int length = 5;
        string res = "";
        while (0 < length--)
        {
            res += v[ran.Next(v.Length)];
        }
        TextBox6.Text = res;

File handling(CREATE,READ,WRITE,APPEND,OPEN)

//CREATE AND WRITE A FILE
        string x=TextBox1.Text;
        string fileloc = @"C:\Users\abc\Documents\Visual Studio 2010\WebSites\filehandling\" + x + ".txt";
        FileStream fs ;
        fs = File.Create(fileloc);
        fs.Close();

  // WRITE IN A FILE
        StreamWriter sw = new StreamWriter(fileloc);
        sw.WriteLine(TextBox2.Text);
        sw.Close();

//APPEND A FILE
        string x = TextBox1.Text;
         string fileloc = @"C:\Users\abc\Documents\Visual Studio 2010\WebSites\filehandling\" + x + ".txt";
        StreamWriter sw = File.AppendText(fileloc);
        sw.WriteLine(TextBox2.Text);
        sw.Close();

// READ A FILE
string x = TextBox1.Text;
         string fileloc = @"C:\Users\abc\Documents\Visual Studio 2010\WebSites\filehandling\" + x + ".txt";
         StreamReader sw=new StreamReader(fileloc);
         TextBox2.Text = sw.ReadToEnd();
         sw.Close();

// OPEN A FILE AS A FILE
string x = TextBox1.Text;
        string fileloc = @"C:\Users\abc\Documents\Visual Studio 2010\WebSites\filehandling\" + x + ".txt";
        System.Diagnostics.Process.Start(fileloc);

Check wheather the tabel is exist or not in database and if not then create

public void check()           //check table exist or not
    {
            string x = TextBox1.Text;
            int i = 0;
            con.Open();
            string s = "SELECT * FROM "+x+" WHERE 1 = 0";
            SqlCommand cmd = new SqlCommand(s, con);
            cmd.ExecuteNonQuery();
            con.Close();
            i = 1;
            if (i == 1)
            {
                Response.write("table doesn't exist");
            }
            else
            {
                Response.Redirect("Default2.aspx");
            }
    }


// NOW CREATE A TABLE


        string x = TextBox1.Text;
        con.Open();
        string s = "create table " + x + " (" + "name nvarchar(50)," + "gender nvarchar(50)," + "email nvarchar(50)," + "mobile nvarchar(50))";
        SqlCommand cmd = new SqlCommand(s, con);
        cmd.ExecuteNonQuery();
        Response.Redirect("Default.aspx");
    }