Saturday, May 5, 2012

Create text using StreamWriter


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
using System.Security;
using System.IO;


namespace Examples
{
    public partial class security : Form
    {
        public security()
        {
            InitializeComponent();
        }
     
        private void security_Load(object sender, EventArgs e)
        {

        }
        StreamWriter sw;
        private void button1_Click(object sender, EventArgs e)
        {
            sw = File.CreateText(@"E:\\demo.txt");
            sw.WriteLine("Hello");
            sw.Close();
        }
    }
}

View All Databases in TreeView using C#


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Examples
{
    public partial class Databases : Form
    {
        public Databases()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=jQuery;UID=sa;password=tiger123");
        SqlCommand cmd;
        private void Databases_Load(object sender, EventArgs e)
        {
            fillTreeView();
        }
        private void fillTreeView()
        {
            try
            {
                con.Open();
                cmd = new SqlCommand("sp_helpdb", con);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    treeView1.Nodes.Add(dr["name"].ToString());
                   
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}

Select SysObjects using C# and show in datagrid




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Examples
{
    public partial class SysObjects : Form
    {
        public SysObjects()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=jQuery;UID=sa;password=tiger123");
        SqlCommand cmd;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                cmd = new SqlCommand(richTextBox1.Text,con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    DataTable dt = new DataTable();
                    dt.Load(dr);
                    dataGrid1.DataSource = dt;                  

                }
            }
            catch (Exception ex)
            {
            }

        }
    }
}

XML Write using C#




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Examples
{
    public partial class XMLWrite : Form
    {
        public XMLWrite()
        {
            InitializeComponent();
        }

        XmlTextWriter w;
        private void XMLWrite_Load(object sender, EventArgs e)
        {
            w = new XmlTextWriter(@"E:\tutorials\asp.net+c#\demo\Examples\Examples\Student1.xml",null);
            w.WriteStartElement("Teacher");
            w.WriteStartElement("Name");
            w.WriteString("Student1");
            w.WriteEndElement();
            w.WriteEndElement();
            w.Close();
            MessageBox.Show(@"E:\tutorials\asp.net+c#\demo\Examples\Examples\Student1.xml written successfully");
            System.Diagnostics.Process.Start("iexplore", @"E:\tutorials\asp.net+c#\demo\Examples\Examples\Student1.xml");

        }
    }
}

ScreenSaver using C#





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Examples
{
    public partial class screensaver : Form
    {
        public screensaver()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random rnd = new Random();
            this.BackColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
        }

        private void screensaver_KeyPress(object sender, KeyPressEventArgs e)
        {
            Close();
        }
    }
}

XML show in datagrid


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Examples
{
    public partial class XMLRead : Form
    {
        public XMLRead()
        {
            InitializeComponent();
        }

        DataSet ds = new DataSet();
        private void XMLRead_Load(object sender, EventArgs e)
        {
            ds.ReadXml(@"E:\tutorials\asp.net+c#\demo\Examples\Examples\Student.xml");
            dataGrid1.DataSource = ds;
            System.Diagnostics.Process.Start("iexplore", @"E:\tutorials\asp.net+c#\demo\Examples\Examples\Student.xml");
        }
    }
}

String Manipulation

String is immutable
String Builder is mutable





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Examples
{
    public partial class @string : Form
    {
        public @string()
        {
            InitializeComponent();
        }

        private void @string_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" World ");
            MessageBox.Show(sb.ToString());
            sb.Insert(0, " Hello ");
            MessageBox.Show(sb.ToString());
            sb.Insert(10," how r u ");
            MessageBox.Show(sb.ToString());
            sb.Remove(5, 2);
            MessageBox.Show(sb.ToString());
        }
    }
}







Analog Clock





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Examples
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int iSec = 0;
        int iMin = 0;
        int iHr = 0;
        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            Graphics g = e.Graphics;
            g.DrawPie(Pens.Blue,50,50,200,200,0,90);
            g.DrawPie(Pens.Blue, 50, 50, 200, 200, 90, 90);
            g.DrawPie(Pens.Blue, 50, 50, 200, 200, 180, 90);
            g.DrawPie(Pens.Blue, 50, 50, 200, 200, 270, 90);
            g.DrawPie(Pens.Green, 50, 50, 200, 200, 90, iSec);
            iSec += 4;

            if (iSec == 360)
            {
                iMin += 6;
                iSec = 0;
            }
            g.DrawPie(Pens.Red, 50, 50, 200, 200, 90, iMin);

            if (iMin == 360)
            {
                iHr += 6;
                iMin = 0;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}

ImageCrop


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace WindowsFormsApplication1
{
    public partial class imagecrop : Form
    {
        public imagecrop()
        {
            InitializeComponent();
        }

        private void btncrop_Click(object sender, EventArgs e)
        {
            Bitmap mybitmap;
            Bitmap mybitmapcropped;
            Graphics mygraphics;
            Rectangle rec = new Rectangle(0, 0, 250, 300);
            mybitmap=new Bitmap(@"E:\1.jpg");
            mybitmapcropped = new Bitmap(200, 200);
            mygraphics = Graphics.FromImage(mybitmapcropped);
            mygraphics.DrawImage(mybitmap, new Rectangle(0, 0, mybitmapcropped.Width, mybitmapcropped.Height),rec.Left,rec.Top,rec.Width,rec.Height,GraphicsUnit.Pixel);
            mybitmap.Dispose();
            mybitmapcropped.Save(@"E:\2.jpg",ImageFormat.Jpeg);




        }
    }
}

Inheritance


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

namespace WindowsFormsApplication1
{
    class inheritanc
    {
        private int mibalance;
        public int balance
        {
            get
            {
                return mibalance;

            }
            set
            {
                mibalance = value;
            }
        }
    }

    class inher : inheritanc
    {
        public void deposit(int amt)
        {
            balance = balance + amt;
        }
        public void withdraw(int amt)
        {
            balance = balance - amt;
        }
    }
}

Windows form file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class inheritance : Form
    {
        public inheritance()
        {
            InitializeComponent();
        }

     

        private void inheritance_Load(object sender, EventArgs e)
        {
            inheritanc kk=new inheritanc();
            MessageBox.Show(kk.balance.ToString());
        }
    }
}

aspx page creation


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class createaspx : Form
    {
        public createaspx()
        {
            InitializeComponent();
        }
        StreamWriter sw;

        private void create_Click(object sender, EventArgs e)
        {
            sw = File.CreateText(@"E:\hello.aspx");
            sw.WriteLine("<html>");
            sw.WriteLine("<form runat=\"server\">");
            sw.WriteLine("<asp:button text=\"hello\" runat=\"server\"></asp:button>");
            sw.WriteLine("</form>");
            sw.WriteLine("</html>");
            sw.Close();
            System.Diagnostics.Process.Start(@"E:\hello.aspx");
        }
    }
}

Binary file creation





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class binaryfile : Form
    {
        public binaryfile()
        {
            InitializeComponent();
        }
        BinaryWriter bw;
        FileStream fs;
        BinaryReader br;
        private void button1_Click(object sender, EventArgs e)
        {
           
            fs = new FileStream(@"E:\demo.data", FileMode.Create);
            bw = new BinaryWriter(fs);
            for (int i = 0; i < 5; i++)
            {
                bw.Write(i);
               
            }
            bw.Close();
            MessageBox.Show("Binary file created successfully");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            fs = new FileStream(@"E:\demo.data", FileMode.Open);
            br = new BinaryReader(fs);
            int l = (int)fs.Length;
            for (int i = 0; i < l / 4; i++)
            {
                listBox1.Items.Add(br.ReadInt32());
            }
        }
    }
}

File watcher





Properties->path->(C:\)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class filewatcher : Form
    {
        public filewatcher()
        {
            InitializeComponent();
        }

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            MessageBox.Show(e.Name);
        }

      
    }
}

String Format





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class string_format : Form
    {
        public string_format()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("I am {0} specializing in {1}","sunitha",".net"));
            MessageBox.Show(string.Format("I am {0} specializing in {1} based at {2}","sunitha",".net","asp"));
        }
    }
}

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...