C#

Mail coding C#
MailMessage Mail = new MailMessage();
        MailAddress ma = new MailAddress("testingfakeidmail@gmail.com", "MLSUweb");
        Mail.From = ma;
        Mail.To.Add(txtEmail.Text);
        //pswrd = pass.ToString().Trim();
        Mail.Subject = "Your password";
        Mail.Body = "Your user name is:" + username + "Your password is:" + pass;
        SmtpClient smtpmailobj = new SmtpClient();
        smtpmailobj.Host = "smtp.gmail.com";
        smtpmailobj.Port = 587;
        smtpmailobj.EnableSsl = true;
        smtpmailobj.Credentials = new System.Net.NetworkCredential("testingfakeidmail@gmail.com", "qazxswedc123");
        smtpmailobj.Send(Mail);        



Log out Button Code and prevent to redirect  using back button to previous page (Sample Code):-

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
            Response.Redirect("~/Login.aspx?err=1");
        else
            lblLogged.Text = Session["user"].ToString();
        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Expires = 0;
        Response.Cache.SetNoStore();
        Response.AppendHeader("Pragma", "no-cache");
    }
    protected void Lnklogout_Click(object sender, EventArgs e)
    {
        .Expires = 0;
        Response.Cache.SetNoStore();
        ResSession["user"] = null;
        Session.Abandon();
        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Responseponse.AppendHeader("Pragma", "no-cache");       
        Response.Redirect("~/Login.aspx?err=null");
    }
 





Calculator Code WPF C#


<Window x:Class="calculator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="408" Width="530" Loaded="Window_Loaded">
    <Grid Width="351" Height="346">
        <TextBox Height="23" HorizontalAlignment="Left" Margin="32,12,0,0" Name="txtNumber" VerticalAlignment="Top" Width="279" />
        <Button Content="9" Height="23" HorizontalAlignment="Left" Margin="236,151,0,0" Name="btn9" VerticalAlignment="Top" Width="75" Click="digitAdd" />
        <Button Content="+" Height="23" HorizontalAlignment="Left" Margin="236,198,0,0" Name="btnAdd" VerticalAlignment="Top" Width="75" Click="calculate" />
        <Button Content="0" Height="23" HorizontalAlignment="Left" Margin="130,198,0,0" Name="btn0" VerticalAlignment="Top" Width="75" Click="digitAdd" />
        <Button Content="8" Height="23" HorizontalAlignment="Left" Margin="130,151,0,0" Name="btn8" VerticalAlignment="Top" Width="75" Click="digitAdd" />
        <Button Content="5" Height="23" HorizontalAlignment="Left" Margin="130,103,0,0" Name="btn5" VerticalAlignment="Top" Width="75" Click="digitAdd" />
        <Button Content="C" Height="23" HorizontalAlignment="Left" Margin="32,198,0,0" Name="btnC" VerticalAlignment="Top" Width="75" Click="btnC_Click" />
        <Button Content="7" Height="23" HorizontalAlignment="Left" Margin="32,151,0,0" Name="btn7" VerticalAlignment="Top" Width="75" Click="digitAdd" />
        <Button Content="4" Height="23" HorizontalAlignment="Left" Margin="32,103,0,0" Name="btn4" VerticalAlignment="Top" Width="75" Click="digitAdd" />
        <Button Content="*" Height="23" HorizontalAlignment="Left" Margin="236,247,0,0" Name="btnMul" VerticalAlignment="Top" Width="75" Click="calculate" />
        <Button Content="=" Height="23" HorizontalAlignment="Left" Margin="130,247,0,0" Name="btnEqual" VerticalAlignment="Top" Width="75" Click="btnEqual_Click" />
        <Button Content="-" Height="23" HorizontalAlignment="Left" Margin="32,247,0,0" Name="btnMinus" VerticalAlignment="Top" Width="75" Click="calculate" />
        <Button Content="3" Height="23" HorizontalAlignment="Left" Margin="236,52,0,0" Name="btn3" VerticalAlignment="Top" Width="75" Click="digitAdd" />
        <Button Content="2" Height="23" HorizontalAlignment="Right" Margin="0,52,146,0" Name="btn2" VerticalAlignment="Top" Width="75" Click="digitAdd" />
        <Button Content="1" Height="23" HorizontalAlignment="Left" Margin="32,52,0,0" Name="btn1" VerticalAlignment="Top" Width="75" Click="digitAdd" />
        <Button Content="6" Height="23" HorizontalAlignment="Left" Margin="236,103,0,0" Name="btn6" VerticalAlignment="Top" Width="75" Click="digitAdd" />
        <Button Content="/" Height="23" HorizontalAlignment="Left" Margin="32,295,0,0" Name="btnDiv" VerticalAlignment="Top" Width="279" Click="calculate" />
    </Grid>
</Window>
 

 C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace calculator
{
    public partial class MainWindow : Window
    {
        static double first = 0.0;
        static double second = 0.0;
        string lastClicked = "";
        static bool calculated = false;
        static bool fromEqual = false;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        public void digitAdd(object sender, RoutedEventArgs e)
        {
            Button dgtButton = (Button)sender;
            txtNumber.Text += dgtButton.Content;
            second = double.Parse(txtNumber.Text);

            fromEqual = false ;
        }

        public void calculate(object sender, RoutedEventArgs e)
        {
            Button optButton = (Button)sender;
            switch(optButton.Content.ToString())
            {
                case "+":
                    if (!fromEqual)
                        first =first+ second;
                    lastClicked = "+";
                    txtNumber.Text = "";
                break;
                case "-":
                    if (!fromEqual)
                        first = first - second;
                    lastClicked = "-";
                    txtNumber.Text = "";
                break;
                case "*":
                    if (!calculated)
                    {
                        calculated = true;
                        first = 1.0;
                    }
                    if(!fromEqual)
                        first *= second;
                    lastClicked = "*";
                    txtNumber.Text = "";
                break;
                case "/":
                if (!calculated)
                {
                    calculated = true;
                    first = second;
                }
                if (!fromEqual && !calculated)
                    first /= second;
                lastClicked = "/";
                txtNumber.Text = "";
                break;
            }
        }

        private void btnEqual_Click(object sender, RoutedEventArgs e)
        {
            switch (lastClicked)
            {
                case "+":
                    first += second;
                    second = 0.0;
                    fromEqual = true;
                    calculated = true;
                break;
                case "-":
                    first -= second;
                    second = 0.0;
                    fromEqual = true;
                    calculated = true;
                break;
                case "*":
                    first *= second;
                    fromEqual = true;
                break;
                case "/":
                    first /= second;
                    fromEqual = true;
                break;

            }           
            txtNumber.Text = first.ToString();          
        }

        private void btnC_Click(object sender, RoutedEventArgs e)
        {
            first = 0.0;
            second = 0.0;
            lastClicked = "";
            calculated = false;
            fromEqual = false;
        }
    }
}


Making Your own bar chart (new idea):-
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            double[] array = {40.1,20.2,59.3,11.20,45.25};
            for(int i=0;i<array.Length;i++)
            {               
                Label l = new Label();
                l.Margin = new Thickness(68+(i)*45,4,0,0);
                l.Height = array[i]*5;
                l.Width = 45;
                l.Content = i.ToString();
                if(i%2==0)
                    l.Background = Brushes.Red;
                else
                    l.Background = Brushes.Blue;
                l.BorderBrush = Brushes.Gray;
                l.Content = array[i];
               
                l.BorderThickness = new Thickness(2, 2, 2, 2);
                l.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
                l.HorizontalAlignment = HorizontalAlignment.Left;
                outergrid.Children.Add(l);
            }
        }




To get database name and tables in particular database:-
SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand();
        string str="Data Source=.\\sqlexpress; Integrated Security=True;";
        SqlConnection con = new SqlConnection(str);
        con.Open();
        DataTable dt = con.GetSchema("DataBases");
        foreach (DataRow row in dt.Rows)
        {
            Response.Write("<br>Database: " + row["database_name"]);
        }
        con.Close();
        str = "Data Source=.\\sqlexpress;Initial Catalog=testing;Integrated Security=True";
        con = new SqlConnection(str);
        con.Open();
        dt = con.GetSchema("Tables");
        foreach (DataRow row in dt.Rows)
        {
            Response.Write("<br>Table: " + row["table_name"]);
        }
        con.Close();

Making dynamic  table using C# where ever you want :-

Place the Label on the page where you want to place table. Make its ID lblForTable.

C# code:-

lblForTable.Text="<table border=1>";
for(int i=0;i<noOfRows;i++)
{
  lblForTable.Text+="<tr>";
  for(int j=0;j<noOfCols;j++)
    lblForTable.Text+="<td>"+//Place your Dynamic content here,say (i+j) +"</td>";
  lblForTable.Text+="</tr>";
}
lblForTable.Text+="</table>";


 Making Your own Slide Show displaying images of a folder

<form id="form1" runat="server">
    <div>
   
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <div style="text-align:center;background-color:Black;vertical-align:middle">
           
                <asp:ImageButton ID="ImageButton1" runat="server" Height="400px" ImageUrl="~/Images/left.jpg" onclick="ImageButton1_Click" Width="116px" />
                <asp:Image ID="Image1" runat="server" Height="400px" ImageUrl="~/Photos/1.png"
                    Width="429px" />
                <asp:ImageButton ID="ImageButton3" runat="server" Height="400px"
                    ImageUrl="~/Images/right.jpg" onclick="ImageButton2_Click" Width="121px" />
                <asp:Timer ID="Timer1" runat="server" Interval="2000" ontick="Timer1_Tick">
                </asp:Timer>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
   
    </div>
  
    </form>

C# code:-
using System.IO;

public partial class SlideShow : System.Web.UI.Page
{
    static long i = 0;
    static string[] files;
    static long tot;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            tot = 1;       
    }
    protected string fetchFiles()
    {
        files = Directory.GetFiles(Server.MapPath("~/Photos/"));
        int j = files[i].ToString().LastIndexOf(@"\");
        int len = files[i].ToString().Length;
        string file = files[i].ToString().Substring(j + 1, len - j - 1);
        tot = files.LongLength;
        return file;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string file = fetchFiles();
        if (i == 0)
            i = tot;
        else
            i--;
        Image1.ImageUrl = "~/Photos/" + file;
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        string file = fetchFiles();
        i = (i + 1) % tot;
        Image1.ImageUrl = "~/Photos/" + file;
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        string file = fetchFiles();
        i = (i + 1) % tot;
        Image1.ImageUrl = "~/Photos/" + file;
    }
}




DataBase values to Excel in C#
DataTable dt=dbc.selectquery("select * from info");
string s="";
StreamWriter sw=new StreamWriter(Server.MapPath("~/ABC.csv"));
for (int i=0;i<dt.Rows.Count;i++)
{
s= "";
for(int j=0;j<dt.Columns.Count;j++)
s+=dt.Rows[i][j].ToString()+",";
s.Remove(s.LastIndexOf(","),1);
sw.WriteLine(s+"\n");
}
sw.Close();

  
Dynamic TabControl in WPF C#
TabControl tb = new TabControl();
            TabItem ti1 = new TabItem();
            Grid grd = new Grid();
            ComboBox cb = new ComboBox();
            cb.Items.Add("Udaipur");
            cb.Items.Add("Jaipur");
            cb.Height = 23;
            cb.Width = 120;
            grd.Children.Add(cb);
            ti1.Header = "inner tab";
            ti1.Content = grd;
            tb.Items.Add(ti1);
            tabItem2.Content = tb;
            tabItem1.Header = "Sandwitches";           
            Grid g = new Grid();
            t = new TextBox[2];
            t[0] = new TextBox();
            t[0].Name = "text1";
            t[0].Height = 23;
            t[0].Width = 120;
            t[0].Text = "1";
            t[0].TextChanged += myFun;
            t[0].Margin = new Thickness(80, 27, 0, 0);
            t[0].VerticalAlignment = System.Windows.VerticalAlignment.Top;
            t[0].HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            //g.Children.Add(t);
            t[1] = new TextBox();
            t[1].Name = "text2";
            t[1].Height = 23;
            t[1].Width = 120;
            t[1].Margin = new Thickness(80, 76, 0, 0);
            t[1].Text = "2";
            t[1].TextChanged += myFun;
            t[1].VerticalAlignment = System.Windows.VerticalAlignment.Top;
            t[1].HorizontalAlignment = System.Windows.HorizontalAlignment.Left;                   
            g.Children.Add(t[0]);
            g.Children.Add(t[1]);
            tabItem1.Content = g;
            TabItem ti = new TabItem();
            ti.Header = "New Tab Item";
            Grid gi = new Grid();
            TextBox t3 = new TextBox();
            t3.Name = "text3";
            t3.Height = 23;
            t3.Width = 120;
            t3.Margin = new Thickness(80, 27, 0, 0);
            t3.Text = "text3";
            t3.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            t3.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            //t3.TextChanged += myFun;
            gi.Children.Add(t3);
          
            tabControl1.Items.Add(ti);
            btn = new Button();
            btn.CommandParameter = "abc";
            gi.Children.Add(btn);
            ti.Content = gi;


Dynamic Events to Dynamically generated Button in WPF C#
 private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            btn = new Button();
            btn.Click += dynamicBtn_click;                      
        }

        private void dynamicBtn_click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("hi");
        }


Finding sub class dynamically in C# 

//SUPER CLASS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace inheritencewithreflection
{
    class MySuperClass
    {
        private int i;
        protected int j;
        public MySuperClass()
        {
            i = 0;
            j = 1;
        }
        public void getChild()
        {
            string t = this.GetType().ToString();
            string t1 = t.Substring(t.IndexOf(".")+1);
            //Assembly o = Assembly.Load(t1);
            //Object []args={};
            Type type = Type.GetType(t);
            ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
            object obj = ci.Invoke(new object[]{});
            MethodInfo mi = type.GetMethod("subMethod");
            object retObj=mi.Invoke(obj,new object[]{});
            Console.WriteLine("MySubClass.subMethod() returned:{0}",retObj);
            Console.WriteLine("\n\n\n{0}",t1);
        }

    }
}
 //SUB CLASS

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

namespace inheritencewithreflection
{
    class MySubClass : MySuperClass
    {
        public MySubClass()
        {
        }

        public void callToSuper()
        {
            getChild();
        }
        public String subMethod()
        {
            return "All the best";
        }
    }
}
 

1 comment:

  1. Mind Blowing Idea of Drawing Graphs in C# with the help of simple Label Component.toooooooo gooooood....

    Vaibhav

    ReplyDelete