How can we learn MVC easily?

How can we learn MVC easily?

“How can we learn MVC? “ Is the major problem for those Web Developers who want to move from ASP.Net to the MVC, Today I want to introduce one of the good sources for learning MVC,http://mvcmusicstore.codeplex.com/

Music-store is an open source project with a learning PDF which you can download it here. I hope it can help you if you want learn MVC.

 

Advertisement

How can work with Dictionaries through VB.Net & C#?

ms_visual_studio

What is Dictionaries ?

Dictionaries are collections that are meant to store lists of key/value pairs to allow lookup of values based on a key.

For example, assume that you needed to map student code to the full name of some students. For this purpose you can use the Hashtable.

[VB.Net]

 Private Sub CreateHashTable()
        Dim StudentCodes As New Hashtable()
        'The first method. 
        StudentCodes.Add("MN145098", "Mehran,Jn")
        'The second method.
        StudentCodes("MN18454") = "Arman,Nas"
    End Sub

[C#]

private void CreateHashTable()
{
    Hashtable StudentCodes = new Hashtable();
    //The first method. 
    StudentCodes.Add("MN145098", "Mehran,Jn");
    //The second method.
    StudentCodes["MN18454"] = "Arman,Nas";
}

In this way you can read HashTable

[VB.Net]

  For Each Item As DictionaryEntry In StudentCodes
            Console.WriteLine(Item.Value)
        Next

[C#]

foreach (DictionaryEntry Item in StudentCodes) {
    Console.WriteLine(Item.Value);
}

How Can write a DataSet as XML through VB.Net & C#?

XMLToday I am going to talk about XML again, today we learn how to make XML from a Dataset.

There are 3 different methods that you can chose, but before producing a XML file or XML Content we need to produce a Dataset.

[VB.Net]

  Private Sub MakeDataSet()
        Dim ds As New DataSet()
        Dim OConnString As String = "Server=CodingTips;Database=_
                                   Library;" + _
                                   "Integrated Security=true;"

        Using conn As New SqlConnection(OConnString)
            Using cmd As SqlCommand = conn.CreateCommand()

                cmd.CommandText = "SELECT * FROM Books;"
                Dim Oda As New SqlDataAdapter(cmd)
                Oda.Fill(ds)
            End Using
        End Using
    End Sub

[C#]

private void MakeDataSet()
{
    DataSet ds = new DataSet();
    string OConnString = "Server=CodingTips;Database=Library;" + _
"Integrated Security=true;";

    using (SqlConnection conn = new SqlConnection(OConnString)) {
        using (SqlCommand cmd = conn.CreateCommand()) {

            cmd.CommandText = "SELECT * FROM Books;";
            SqlDataAdapter Oda = new SqlDataAdapter(cmd);
            Oda.Fill(ds);
        }
    }
}

Now you are ready to produce XML File,follow these three different methods.
1. Write XML directly in the film

[VB.Net]

ds.WriteXml("c:/CodingTips.xml")

[C#]

ds.WriteXml("c:/CodingTips.xml")

2.The second method is GetXml , the result of this method is String but it does not support any of the advanced features of the XML serialization

[VB.Net]

Dim strxml as string = ds.GetXml()

[C#]

string strxml = ds.GetXml();

3.Sometimes you want to use all advanced features of the XML serialization but you don’t want to save the file, in this case you can use this solution.

[VB.Net]

Dim strm As New MemoryStream()
ds.WriteXml(strm)

[C#]

MemoryStream strm = new MemoryStream();
ds.WriteXml(strm);

How can use XML Serialization in VB.Net and C#?

Xml-tool-iconAs you know there are different types of machines and OS in the world that they have to connect to each other and transfer data but each system has own structure and may cannot understand the data that are coming from other system(s), but technology has solved it for them! One of the solutions is XML

What is XML?

XML  (Extensible Markup Language) is a format that was designed to transport and store data.Both machine and human can understand XML.

If you want to learn more about the XML I suggest you to go this link.

Before start you must import these classes:

[VB.Net]

Imports System.IO
Imports System.Xml.Serialization

[C#]

using System.IO;
using System.Xml.Serialization;

[VB.Net]

 Private Sub CreateXML()
        Dim OFileStream As FileStream = _
New FileStream("C:/CodingTips.XML", _
        FileMode.Create)
        Dim Oxmlsr As XmlSerializer = _ 
New XmlSerializer(GetType(DateTime))
        Oxmlsr.Serialize(OFileStream, System.DateTime.Now.ToString)
        OFileStream.Close()
    End Sub

[C#]

private void CreateXML()
{
    FileStream OFileStream = new FileStream("C:/CodingTips.XML",_
 FileMode.Create);
    XmlSerializer Oxmlsr = new XmlSerializer(typeof(DateTime));
    Oxmlsr.Serialize(OFileStream, System.DateTime.Now.ToString());
    OFileStream.Close();
}

How can make SAH1 through VB.Net & C#

Security

What is SHA1?

This part has been copied directly form Wikipedia

SHA-1 is a cryptographic hash function designed by the United States National Security Agency and published by the United States NIST as a U.S. Federal Information Processing Standard. SHA stands for “secure hash algorithm”. For further information click here

Why you need SHA1?

If you want to make your sensitive information secure, one of the best way, is that make them encrypt. There are different ways which  you can use them to make your information encrypt but  in most of cases the encrypted information can be decrypted easily , So these methods  cannot be  good choice  for encrypting  data same as Base64, But SHA1 is a one way street , it means after encrypt  nobody can decrypt  it . Awesome!

If there is no way to decrypt SHA1, how we can use it?

It is common question about SHA1! Somebody still looking for a way to decrypt the SHA1 but I suggest them to stop searching because they cannot find anything.

Now I will explain you how SHA1 can work for you. For example you make a SAH1 from the password that you want to save on the database, when user wants to login to the system you have to make another SHA1 form the string that user has entered, then compare it with SHA1 that you have been saved before on the database if they are same user is eligible to login to the system.

To make a SAH1 you need to pass 3 steps:

  1. Make byte stream from the string that you want to encrypt.
  2. Make SHA1 form the byte.
  3. Make string from the SHA1 that you have produced.

I have mention these three steps in the code below:

[VB.Net]

 Private Sub EncryptData()
    Dim strToHash As String = "Please Encrypt me !"
    Dim Result As String = ""
    Dim OSha1 As New _
    System.Security.Cryptography.SHA1CryptoServiceProvider

    'Step 1
    Dim bytesToHash() As Byte _
     = System.Text.Encoding.ASCII.GetBytes(strToHash)

    'Step 2
    bytesToHash = OSha1.ComputeHash(bytesToHash)

    'Step 3
     For Each item As Byte In bytesToHash
          Result += item.ToString("x2")
     Next
    End Sub

[C#]

private void EncryptData()
{
    string strToHash = "Please Encrypt me !";
    string Result = "";
    System.Security.Cryptography.SHA1CryptoServiceProvider OSha1 = _
    new System.Security.Cryptography.SHA1CryptoServiceProvider();

    //Step 1
    byte[] bytesToHash = _
    System.Text.Encoding.ASCII.GetBytes(strToHash);

    //Step 2
    bytesToHash =_
    OSha1.ComputeHash(bytesToHash);

    //Step 3
    foreach (byte item in bytesToHash) {
        Result += item.ToString("x2");
    }
}

How To Write File Through VB.Net & C#

ms_visual_studio

As I have promised ,today I am going to show you how to create a file, you can write (create) file in different ways same as reading file.

Before you start, have to import System.IO.

[VB.Net]

Imports System.IO

[C#]

using System.IO;

Writing File

Let met to start with the easiest way,

1.WriteAllText

With this function you can create and write file at the same time!

[VB.Net]

File.WriteAllText("c:\CodingTips.txt", "coding is easy !")

[C#]

File.WriteAllText("c:\\CodingTips.txt", "coding is easy !")

2.StreamWriter:

This Pattern is much like reading file.

[VB.Net]

Dim writer As StreamWriter =File.CreateText("c:\CodingTips.txt") 
writer.WriteLine("coding is easy") 
writer.Close()

[C#]

StreamWriter writer = File.CreateText("c:\\CodingTips.txt");
writer.WriteLine("coding is easy");
writer.Close();

3.MemoryStream:

Sometimes you will need to create a stream (the string that you want to write to the file) before you really need to store it somewhere (like in a file). for example you have 5 functions in your code, and each function has a specific result and you want to store the results of these functions in one file, you cannot use the methods that I explained above, in this case, first you need to prepare the stream and then write it to the file, for this purpose MemoryStream would be a good choice,

[VB.Net]

Dim memoryStrm As New MemoryStream()
Dim writer As New StreamWriter(memoryStrm) 

'This part can to be repeated anywhere in your code 
that you want to you want to append text to the memoryStream.
writer.WriteLine("Function1") 
writer.WriteLine("Result1")

[C#]

MemoryStream memoryStrm= new MemoryStream();
StreamWriter writer = new StreamWriter(memoryStrm);

//This part can to be repeated anywhere in your code 
that you want to you want to append text to the memoryStream.
writer.WriteLine("Function1");
writer.WriteLine("Result1");

After you prepared your MemoryStream you can write it to the file:

[VB.Net]

' Here you must ask the writer to push the data into the 
' underlying stream 
writer.Flush() 
' the create a file stream 
Dim MyFile As FileStream = File.Create("c:\CodingTips.txt") 
' now Write the MemoryStream to the file
memoryStrm .WriteTo(theFile) 
' the you have to Clean up the objects 
writer.Close() 
theFile.Close() 
memoryStrm .Close()

[C#]

// Here you must ask the writer to push the data into the 
// underlying stream 

{
    writer.Flush();
    // the create a file stream 
    FileStream MyFile = File.Create("c:\\CodingTips.txt");
    // now Write the MemoryStream to the file
    memoryStrm.WriteTo(theFile);
    // the you have to Clean up the objects 
    writer.Close();
    theFile.Close();
    memoryStrm.Close();
}