Featured post

How to start a programming

Hello everyone! The objective of this post is how to get started with programming. But first I discuss why we learn programming. So, the...

Monday, 15 October 2018

Send an email using C# code

Hello All,

Today we learn how to send an email through C# application. It could be windows form application or web forms application. You need to use few classes and methods to send an email.

Where we need to send an email

Sometimes we need to add module to send an email during sign-in and forget password. There we use this code and get benefit.

Requirement

You need a server, for demo you may use your gmail server. For using gmail server you need to perform below steps before using it.

1. Go to Account
2. Select Sign In & Security
3. Drag the page and at the end you find Allow less secure apps: OFF
Turn this to ON

Now in your application add below code and attach an event with button or link (depends on which application you work on)

private void Send_btn_Click(object sender, EventArgs e)
        {
            try
            {
                // Check to is not empty
                if(!string.IsNullOrWhiteSpace(to_tb.Text))
                {
                    string emailFrom = "sender@gmail.com";
                    string emailTo = to_tb.Text;

                    // Now create instance of Mail
                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(emailFrom, emailTo);

                    // Make a body --> Subject, body message etc
                    message.Subject = "Send Email Demo Application by Salman Mushtaq";
                    message.Body = message_tb.Text;

                    // Set periority
                    message.Priority = System.Net.Mail.MailPriority.High;

                    // Another instance we need to create for SmtpClient
                    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
                    client.EnableSsl = true; // Gmail require SSL 

                    // Set credentials for email
                    client.Credentials = new System.Net.NetworkCredential(emailFrom, "sender_password");

                    //Finally send an email
                    client.Send(message);

                    MessageBox.Show("Email sent successfully");

                }
                else
                {
                    MessageBox.Show("Please write email address you need to send an email!", "Email Address", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"Exception",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

For this BLOG I develop Windows Desktop Application. Kindly edit sender email and password.

Happy coding :-)

Wednesday, 11 October 2017

How to use Ordered Dictionary C#

Ordered Dictionary

If we need to save data in key pair representation then Ordered Dictionary is best.
Ordered Dictionary represent collection of key pair value and it can be access through key and index.

How to use

1. Create console application in visual studio 2013
2. Use namespace

using System.Collections;
using System.Collections.Specialized

3. Create instance of Ordered Dictionary

OrderedDictionary order = new OrderedDictionary();

4. Add key pair values

order.Add("Name","Salman Mushtaq"); // first argument is key and second argument is value
order.Add("Age","27");
order.Add("Address","Rahim Yar Khan, Pakistan");

5. Add keys and values in Collection

ICollection keyCollections = order.keys;
ICollection valueCollections = order.Values;

6. Copy the keys and values to array so that it will accessable

String[] myKeys = new String[order.Count];
String[] myValues = new String[order.Count];

keyCollections.CopyTo(myKeys,0);
valueCollections.CopyTo(myValues,0);

7. Display keys and values

for(int i=0; i<order.Count; i++)
{
Console.WriteLine("Key: " + myKeys[i] + "   Value: " + myValues[i]);
}

Console.ReadKey();

8. The output is like


Video Demo



Thank you.











Sunday, 20 August 2017

Get Row Index From Data Grid View C#

How to get row index from data grid view in c# windows form application so that we are able to get data of whole row base on row index.

To achieve  we need
  1. Create Windows Application Project
  2. Drag DataGridView from tool box
  3. Drag a button to fill grid with dummy data
  4. Create Cell Double Click event
Code to fill Grid with dummy data

// Initialize number of columns
dataGridView1.ColumnCount = 3;

// named all 3 columns
dataGridView1.Columns[0].Name = "Name";
dataGridView1.Columns[1].Name = "Age";
dataGridView1.Columns[2].Name = "Salary";

// Create row and add dummy data
string[] row = new string[] {"Salman", "27", "5000"};
dataGridView1.Rows.Add(row);
row = new string[] {"Awais", "34", "9000"};
dataGridView1.Rows.Add(row);
 row = new string[] {"Muzamil", "30", "6000"};
dataGridView1.Rows.Add(row);

If you run the program and click on button you have result like


Code to create event with mouse double click

Now, go to properties of Grid View and assign method to CellDoubleClick = dataGridView1_CellDoubleClick

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            MessageBox.Show(e.RowIndex.ToString());
        }

Again run the program you get index of row.

For video demo please see : This Video





Thursday, 17 August 2017

Data Binding using Linq to Sql in C#

Linq to SQL is an API to access data from database using .NET 3.5, here in this article we learn


  1. How to connect with database
  2. How to get data from table
  3. How to display table data in DataGridView
For achieving this we need 

  1. Visual Studio 2013 Professional
  2. SQL Server
  3. SQL Management Studio
  4. .NET Framework
  5. Knowledge of programming in C#
So, first of all we need to know what is Data Binding

DATA BINDING:

Data binding is the process to connect application user interface with business logic. In simple words you get the data from database using linq to SQL and bind it as per requirement and display on user interface of application.

Now, lets do some programming to achieve the target

STEP-1:

Create new Windows Form Project and create new form named data.cs and then open toolbox (Ctrl+Alt+X) and search DataGridView drag this element to the window screen.

ASSUMPTIONS:

Before start coding we assume that
  1. We have DataConText Class named abcDataContext
  2. We have table named customers
  3. DataGridView named dataGridView1 

DATA FETCHING AND DATA BINDING:

In Linq to SQL data context object is responsible for establishing a connection with a data source (database).

//create datacontext object
abcDataConText dc = new abcDataContext();

// create object of table customers
customers customer = new customers();

// now assign data to this table customer using linq to sql query
var allcustomers = from c in dc.GetTable<customers>() select c;

// now bind data 
customerBindingSource.DataSource = allcustomers;

//now assign this data source to data grid view
dataGridView1.DataSource = customerBindingSource;

That's all, when you execute the source program you will get the result.

Thank you.












Sunday, 11 December 2016

How to start a programming

Hello everyone!

The objective of this post is how to get started with programming. But first I discuss why we learn programming.

So, the answer is that programming is everywhere. Even in engineering, mathematics, statistics, banking, economics classes there should be one class of programming.

Do you think why we learn programming? The basic purpose is you have idea about problem solving techniques.

Yes, programming will open you mind and then you think beyond the limits.

So, to become a developer you must have knowledge of below techniques.


  • HTML
  • CSS
  • JavaScript
These three technologies will open your mind and you are ready to learn C# .NET

These are basics of developing.

If you not have idea from where we learn just open google --> write learn HTML and you got a lot of sites from where you learn HTML. I suggest W3Schools and Tutorials Point.

Must remember one think. Follow the sequence to learn these technologies.

HTML --> CSS -- JavaScript

Then after it you must choose a platform or programming language for further learning, it might be C, C++, JAVA, PhP or C#.

As this platform is for C#, so we discuss C# and framework .NET in later posts.

So keep learning and start with HTML from today and remember if you can't write code you can't understand. If you write a code you should understand.

Thanks

Earn money with Payza