EDUDOTNET

Tuesday, May 31, 2016

Email Sending Using Gmail Account In C#

Email Sending Using Gmail Account In C#

- Following gmail smtp address is:-
- smtp.gmail.com and port:- 25, 587 - TLS
- same server and port:- 465 - SSL

- Log on using your gmail account use for sending mail and go to "Less secure apps" and turn on it. url: https://www.google.com/settings/security/lesssecureapps

- You need to enable allowing less secure apps otherwise you will get an authentication error.

- more details:- How to enable allowing less secure apps - you can follow steps mention here: https://support.google.com/accounts/answer/6010255?hl=en-GB



Below mention code for an example:

   MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("********.**@gmail.com");
            mail.To.Add("****.xyz@gmail.com");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail from GMAIL";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("********.**@gmail.com", "********");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);


Monday, March 14, 2016

How to get javascript variable to retain value after page refresh?

This is possible to using cookies, localStorage or sessionStorage etc.

We can use localStorage for it. we need to set a varibale that should be reflected in next pages or after reload the page.
 eg.:

var myval = "xyz";

localStorage.setItem("myval", myval);


after that we can get the stored value from localStorage by page reloaded/Refresh...

like:
var myval = localStorage.getItem("myval");


The complete list of alternatives of cookies is:


  • Standard HTTP cookies
  • Local Shared Objects (Flash cookies)
  • Silverlight Isolated Storage
  • Storing cookies in RGB values of auto-generated, force-cached PNGs using HTML5 Canvas ag to read pixels (cookies) back out
  • Storing cookies in Web history
  • Storing cookies in HTTP ETags
  • Storing cookies in Web cache
  • window.name caching
  • Internet Explorer userData storage
  • HTML5 Session Storage
  • HTML5 Local Storage
  • HTML5 Global Storage
  • HTML5 Database Storage via SQLite

C# Attributes Overview [What are attributes and why do we use it?]

C# Attributes Overview


Attributes are elements that allow you to add declarative information to your programs. This declarative information is used for various purposes during run-time and can be used at design time by application development tools.

Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time and used in any number of ways.(msdn)

=> It’s often necessary to associate information (metadata) with types and members, e.g.
           -Documentation URL for a class
           -Transaction context for a method
           -XML persistence mapping
           -COM ProgID for a class
=> Attributes allow you to decorate a code element (assembly, module, type, member, return value and parameter) with additional information


Attributes are generally applied physically in front of type and type member declarations. They're declared with square brackets, "[" and "]", surrounding the attribute such as the following ObsoleteAttribute attribute:

[ObsoleteAttribute]

The "Attribute" part of the attribute name is optional. So the following is equivalent to the attribute above:

[Obsolete]

Deprecated:
A deprecated class or method is like that. It is no longer important. It is so unimportant, in fact, that you should no longer use it, since it has been superseded and may cease to exist in the future.

Obsolete:
The Obsolete attribute marks a program entity as one that is no longer recommended for use. Each use of an entity marked obsolete will subsequently generate a warning or an error, depending on how the attribute is configured. 

[Obsolete] attribute is a pretty useful concept in the .NET Framework, designed to mark parts of our code that is planned to be phased out and its usage should be avoided. It makes a lot of sense to mark methods or types as obsolete, e.g. when we drop support for some parts of our API and the code is planned to be removed when all the customers are migrated, or when we've restructurized our code so that there's a newer approach for particular problem and we just had no time or possibility to upgrade all the usages, etc.



For example below is a simple class where “MyFirstMethod” is decorated by the “Obsolete” attribute. So when developer call this class they are alerted that “MyFirstMethod” is obsolete and MyFirstMethod is deprecated, please use "MyFirstListMethod" instead.

Code:

namespace DemoAttributes
{
    public class MyBasicAttributesDemo
    {
        [Obsolete("MyFirstMethod is deprecated, please use MyFirstListMethod' instead")]
        public void MyFirstMethod()
        {
            //Console.WriteLine("Obsolete Attribute...");
        }

        public void MyFirstListMethod()
        {
            //Console.WriteLine("New Method...");
        }
    }

}



namespace DemoAttributes
{
    public class Program
    {
        static void Main(string[] args)
        {
            MyBasicAttributesDemo obj = new MyBasicAttributesDemo();
            obj.MyFirstMethod();
        }
    }

}








Overview & Some Attributes Description

  • Attributes are superior to the alternatives
  • Modifying the source language
  • Using external files, e.g., .IDL, .DEF

  • Attributes are extensible
  • Attributes allow to you add information not supported by C# itself

  • Not limited to predefined information

  • Built into the .NET Framework, so they work across all .NET languages
  • Stored in assembly metadata

  • Some predefined .NET Framework attributes


  • Attributes can be
  • Attached to types and members

  • Examined at run-time using reflection

  • Completely extensible
  • Simply a class that inherits from System.Attribute

  • Type-safe
  • Arguments checked at compile-time

  • Extensive use in .NET Framework
  • XML, Web Services, security, serialization, component model, COM and P/Invoke interop, code configuration etc....

Attribute Name
Description
Browsable
Should a property or event be displayed in the property window
Serializable
Allows a class or struct to be serialized
Obsolete
Compiler will complain if target is used
ProgId
COM Prog ID
Transaction
Transactional characteristics of a class











Wednesday, February 3, 2016

Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies.

Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)


I having problem with NewtonSoft.json v4.5 when I added some package from nugget. To solve this, I ensured all my projects used the same version by running the following command and checking the results:
 try using this in web.config or app.config:

Update-Package –reinstall Newtonsoft.Json

OR

<dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json"
            publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>

</dependentAssembly>

Wednesday, December 16, 2015

How Design A SQL Query With Dynamic Pivot Columns in SQL Server

DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Id)
                      FROM RWX_Users FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') , 1, 1, '');

SELECT @query =
'SELECT *
FROM
(
  SELECT
    Users.Id
  FROM RWX_Users AS Users
) AS t
PIVOT 
(
  MAX(Id) 
  FOR Id IN( ' + @cols + ' )' +
') AS p ; ';

execute(@query);

Monday, December 14, 2015

Pivot table with single column in sql server

- Create Table

CREATE TABLE MyTable
([Id] int)
;

-Insert Data In to Table

INSERT INTO MyTable
([Id])
VALUES
(1),
(2),
(3),
(4),
(5)
;


-Design a query with Pivot 

select *
from
(
  select  CAST(Id AS VARCHAR(15)) +'Col' AS Name , Id AS UserId
  from MyTable
) d
pivot
(
     Min(UserId)
    FOR [Name] IN ([1Col], [2Col],[3Col],[4Col], [5Col])
) piv;


Sunday, August 23, 2015

AWESOME PRIZE - http://forum.edudotnet.com/

http://forum.edudotnet.com/