Search This Blog

Thursday, December 29, 2011

what is generics and why it is required ???

The generics are used to assign the data type at run time.


namespace Generics
{
    class Program
    {
        static void Main(string[] args)
        {
            Test<int> t1=new Test<int>();
            t1.Temp = 100;
            Console.WriteLine(t1.Temp);
            Test<string> t2 = new Test<string>();
            t2.Temp = "string";
            Console.WriteLine(t2.Temp);
            Test1<int, string> t = new Test1<int, string>(1028, "This is string");
            int i=t.method1();
            string s=t.method2();
            Console.WriteLine(" "+i+" "+s);
            Console.ReadLine();

        }
    }
    class Test<T>
    {
        T temp;

        public T Temp
        {
            get { return temp; }
            set { temp = value; }
        }


    }

    class Test1<T, TT>
    {
        T temp;
        TT temp1;
        public Test1(T t1,TT tt1)
        {
            temp=t1;
            temp1=tt1;
        }
        public T method1()
        {
            return temp;
        }
        public TT method2()
        {
            return temp1;
        }
    }
}






Wednesday, December 21, 2011

In C Sharp Operator Overloading .....

Operator is used to provide functionality or logic in between the data members or we can say it's a varibles of the class in c sharp.

But By using operator overloading concept we can provide functionality between the objects of the class.
For Example Addition of two variables which having the complex number.

Real and Imaginary part at that time we have to overload an operator and perform the addition of two complex number.


Example:- Console Application in Asp.net Visual Studio 2010.

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

namespace Operator_overloading
{
    class Program
    {
        static void Main(string[] args)
        {
            Test t1 = new Test(10,100);
            Console.WriteLine("\n\n\tThe information of t1");
            t1.show();

            Test t2 = new Test(11,111);
            Console.WriteLine("\n\n\tThe information of t2");
            t2.show();
            Test t3 = new Test();
            t3 = t1 + t2;
            Console.WriteLine("\n\n\tAfter Addition of t1 and t2");
            t3.show();
            Test t4 = new Test();
            t4=t3++;
            t4.show();
            Console.ReadLine();

        }
    }
    class Test
    {
        int a, b;
        public Test()
        {
            a = 0;
            b = 0;
        }
        public Test(int n, int m)
        {
            a = n;
            b = m;
        }
        public static Test operator +(Test t, Test tt)
        {
            Test temp = new Test();
            temp.a = t.a + tt.a;
            temp.b = t.b + tt.b;
            return temp;
        }
        public static Test operator ++(Test t)
        {
            t.a++;
            t.b++;
            return t;
        }
        public void show()
        {
            Console.WriteLine("\n\t\tThe values are "+this.a+"  and  "+this.b);
        }
   
    }
}


OutPut


 

how to create user control in asp.net with example ???

Steps To Create User Control

  1. First Step is Open solution explorer then right click on project -> add New Item -> Web User Control ->
  2. Assign Name to Page.ascx
  3. Add asp.net server controls there and create your own server control and write code of click event or any events code in .ascx.cs file
  4. Open solution explorer then right click on add New Item->Create name form.aspx
  5. Then drag and drop that page from solution explorer to .aspx Design Page
  6. Run the .aspx page.
the ascx file source

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<br />
&nbsp;
<asp:TextBox ID="txt_show" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btn_show" runat="server" Font-Bold="True"
    onclick="btn_show_Click" Text="Show Message" />

Design ascx file 



the .ascx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_show_Click(object sender, EventArgs e)
    {
        txt_show.Text = "User Control Example";
    }
}


The .aspx file 

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

<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>

<!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>
       
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
        <br />
        <br />
    </div>
    </form>
</body>
</html>

.aspx.cs File 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 UserControlexample : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox t = (TextBox)WebUserControl1.FindControl("txt_show");
        t.Text = "Hi,Hitesh";
    }

}


Friday, December 16, 2011

How to send eMail Using Asp.net Application???

Code :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.Net.Mail;
//using System.Web.Mail.SmtpMail;

public partial class SendEmail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_send_Click(object sender, EventArgs e)
    {
       
        MailMessage Msg = new MailMessage();      
        Msg.From = new MailAddress(txtfrom.Text);       
        Msg.To.Add(txtto.Text);
        Msg.Subject = txtsubject.Text;
        Msg.Body = txtbody.Text;       
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential(txtfrom.Text,txtpassword.Text);
        smtp.EnableSsl = true;
        smtp.Send(Msg);

    }

Thursday, December 15, 2011

asp.net image upload and show in the gridview


code: 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 Product_upload : System.Web.UI.Page
{
    static string path;
    SqlConnection con = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_upload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string name = FileUpload1.FileName;
            path = "~//images//" + FileUpload1.FileName;
            FileUpload1.SaveAs(Server.MapPath(path));
            Image1.ImageUrl = path;
            lblname.Text = txt_name.Text;
            lblmsg.Text = "File is successfully uploaded";
        }
        else
        {
            lblmsg.Text = "Please Browse the Image first";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("insert into tbl_image values(@name,@image)",con);
        cmd.Parameters.AddWithValue("@name",txt_name.Text);
        cmd.Parameters.AddWithValue("@image",path);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        fill_data();
    }
    public void fill_data()
    {
        SqlDataAdapter sda = new SqlDataAdapter("select * from tbl_image",con);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();           
    }

    protected void ImageButton1_Command(object sender, CommandEventArgs e)
    {
        int id = int.Parse(e.CommandArgument.ToString());
        Response.Redirect("Product_information.aspx?id="+id);
    }
}