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());
        }
    }
}







Using Authorization with Swagger in ASP.NET Core

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