Sunday, 2 September 2018

C# Coding Standards Series – Naming Conventions

Terminology and Definitions for Naming Conventions:

Camel Case (camelCase): The first letter of the word is lower case and then each first letter of the part of the word is upper case;

Pascal Case (PascalCase): The first letter of the word is upper case and then each first letter of the part of the word is upper case;

Underscode Prefix (_underscore): The word begins with underscore char and for the rest of the word use camelCase rule;

General Rules

1. Use Pascal Case for Class names:
Ex: public class HelloWorld { ... };

2. Use Pascal Case for Method names:
Ex: public void SayHello(string name) { ... };

3. Use Camel Case for variables and method parameters:
Ex: int totalCount = 0;
void SayHello(string name)
{
     string fullMessage = "Hello " + name;
     //...
}

4. Avoid all upper case or all lower case names;

5. Do not use Hungarian notation:
Ex: string m_sName; (the prefix m_ means that is a member variable and s means that is a string data type);

6. Avoid abbreviations longer than 5 characters;

7. Avoid using abbreviations unless the full name is excessive:
Ex: Good: string address;           
    Not good: string addr;

8. Use meaningfull, descriptive words for naming variables;

9. Try to prefix Boolean variables with “Can”, “Is” or “Has”;

10. Do not use Underscore Prefix for local variables names;

11. All member variables must use Underscore Prefix so that they can be identified from other local variables names;

12. Avoid naming conflicts with existing .NET Framework namespaces or types;

13. Do not include the parent class name within a property name:
Ex: Good: Customer.Name;             
    Not good: Customer.CustomerName;

14. When defining a root namespace, use a Product, a Company or a Developer name as the root:
Ex: NorthwindApplication.Utilities;

15. Use Pascal Case for file names;

16. Method name should tell you what it does;

17. A method should do only “one job”. Do not combine multiple jobs in one method even if those jobs have very few lines of code.
Ex:
protected void SaveCustomerName(string customerName)
{
   //code here...
}


Naming Conventions for ASP.NET Controls

In general, naming ASP.NET controls is made using Camel Case naming convention, where the prefix of the name is the abbreviation of the control type name.

AbbreviationASP.NET Control
STANDARD CONTROLS
btnButton
cbCheckBox
cblCheckBoxList
ddlDropDownList
fuFileUpload
hdnHiddenField
lnkHyperlink
imgImage
ibtn(btn)ImageButton
lblLabel
lbtn(btn)LinkButton
lbListBox
litLiteral
mvMultiView
pnlPanel
phPlaceHolder
rbRadioButton
rblRadioButtonList
tblTable
txtTextBox
vView
DATA CONTROLS
dtlDataList
dpDataPager
dtvDetailsView
etsEntityDataSource
fvFormView
gvGridView
ldsLinqDataSource
lvListView
odsObjectDataSource
qeQueryExtender
rptRepeater
smdSiteMapDataSource
sdsSqlDataSource
xdsXmlDataSource
VALIDATION CONTROLS
cpvCompareValidator
ctvCustomValidator
rvRangeValidator
revRegularExpressionValidator
rfvRequiredFieldValidator
vsValidationSummary

Saturday, 1 September 2018

Auto Increment Id or Number Using Asp.net C#


Automatically increasing Id or Number and save the data in the Database using Asp.net C#


HTML Coding

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Auto Increment Id or Number</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
       <table><tr><td>
           <asp:Label ID="Label1" runat="server" Text="ID"></asp:Label>
           </td><td>
               <asp:Label ID="lblID" runat="server" ForeColor="#FF3399"></asp:Label>
           </td></tr>
           <tr><td>
               <asp:Label ID="Label2" runat="server" Text="Course"></asp:Label>
               </td><td>
                   <asp:TextBox ID="txtCourse" runat="server"></asp:TextBox>
               </td></tr>
           <tr><td></td><td>
               <asp:Button ID="Button1" runat="server" Text="Register" OnClick="Button1_Click" />
               </td></tr>
       </table></div>
    </form>
</body>

</html>

C# Coding

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 _Default : System.Web.UI.Page
{    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getid();
        }
    }
    protected void getid()
    {
        SqlConnection  con = new SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());       
        con.Open();
        SqlCommand cmd = new SqlCommand("select MAX (CAST( Id as INT)) from Reg", con);
        SqlDataReader rd = cmd.ExecuteReader();
        if (rd.Read())
        {
            string Value = rd[0].ToString();
            if (Value == "")
            {
                lblID.Text = "1";
            }
            else
            {              
                lblID.Text =rd[0].ToString();
                lblID.Text = (Convert.ToInt64(lblID.Text) + 1).ToString();            
            }       
        }
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Reg values('"+lblID.Text+"','"+txtCourse.Text+"')", con);
        cmd.ExecuteNonQuery();
        txtCourse.Text = "";
        getid();
    }
}

Create the table




Friday, 31 August 2018

MailCoding-WithAttachment


Add the namespace

using System.Net;
using System.Net.Mail;


Add this code in your button click

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("athi.litztech@gmail.com", "MailCoding-Server");//mail sending from
        msg.To.Add("athirarajinesh@gmail.com");//mail sending to. If you want to add multiple to address separate with comma.
        msg.Subject = "MailCoding-Server";
        msg.Body = "Mail coding for sending mail in server";
        Attachment data = new Attachment(Server.MapPath("~/TechWorld - @h!.html")); // your path may look like Server.MapPath("~/Filepath.pdf") 
        msg.Attachments.Add(data);
        msg.Priority = MailPriority.High;//to set the priority of mail
        msg.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net", 25);
        smtp.Send(msg);

Then you will be able to send the mail from your account and can be recieved.



Thursday, 30 August 2018

MailCoding-Server


Add the namespace

using System.Net;
using System.Net.Mail;


Add this code in your button click

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("athi.litztech@gmail.com", "MailCoding-Server");//mail sending from
        msg.To.Add("athirarajinesh@gmail.com");//mail sending to. If you want to add multiple to address separate with comma.
        msg.Subject = "MailCoding-Server";
        msg.Body = "Mail coding for sending mail in server";
        msg.Priority = MailPriority.High;//to set the priority of mail
        msg.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net", 25);
        smtp.Send(msg);

Then you will be able to send the mail from your account and can be recieved.


ComparisonOfString


Add this code in your button click
       
        string string1 = Convert.ToString(txtString1.Text);//assign the value in the textbox to a string variable.
        string string2 = Convert.ToString(txtString2.Text);
        if(string1 == string2)//compare the string you enter
        {
            Response.Write("<script> alert ('String 1 and 2 are Same')</script>");
        }
        else
        {
            Response.Write("<script> alert ('String 1 and 2 are not Same')</script>");
        }
        txtString1.Text = "";
        txtString2.Text = "";

Tuesday, 28 August 2018

MailCoding-Local


Add the Namespace

using System.Net;
using System.Net.Mail;


Add this code in your button click

           var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential("athirarajinesh@gmail.com", "password")//your mail id and password.
         
        };
                MailMessage m = new MailMessage();
                m.IsBodyHtml = true;
                m.To.Add("athi.litztech@gmail.com");//mail sending to. If you want to add multiple to address separate with comma.
                m.Bcc.Add("bcc.litztech@gmail.com");//attach a bcc copy
                m.CC.Add("cc.litztech@gmail.com");//attach a cc copy
                m.From = new MailAddress("athirarajinesh@gmail.com");//mail sending from
                m.Subject = "MailCoding-Local";
                m.Body = "Mail coding for sending mail in local host";
                m.IsBodyHtml = true;
                smtp.Send(m);

If you are using your mail for the first time then it will show an SMTP error.


You can solve this in your gmail settings.

To help keep Google Accounts through work, school, or other groups more secure, we block some less secure apps from using them. If you have this kind of account, you’ll see a "Password incorrect" error when trying to sign in. If so, you have two options:

  • Option 1: Install a more secure app that uses stronger security measures. All Google products, like Gmail, use the latest security measures.
  • Option 2: Change your settings to allow less secure apps into your account. We don't recommend this option because it can make it easier for someone to break into your account. If you want to allow access anyway, follow these steps:

  1. Go to the Less secure apps section of your Google Account.
  2. Turn on Allow less secure apps. If you don't see this setting, your administrator might have turned off less secure app account access.

Then you will be able to send the mail from your account and can be recieved.