Friday, 30 September 2016

How to Pass large text in Ajax and C#

Pass large text in Ajax and C#

Some times we  came to some situation that we need to insert large text without posting the form.
That is form will not be submitted , but the data will be saved.Here is an example.First i am giving the Html & script

 ----------------------------------------------------------------------------------------------------------------------------

 <script type="text/javascript" src="http://code.jquery.com/jquery.min.js" ></script>
<textarea name="textarea" class="textarea_class" ></textarea>


<input type=text class="text _class" />


<button id="btnSave" value="Save" onclick="Save()" ></button>

<script type="text/javascript">
    function Save() 
        {
        var Xhtml = $('.textarea_class').val();
        var encodedHTML = escape(Xhtml);         //here encoding Html.
        var Title = $('.jqte-test').val();;       
        $.ajax({
            type: "POST",
            url: "BlogWrite/SaveData",
            data: "{Title:'" + Title + "',Html:'" + encodedHTML + "'}",
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
                alert('02');
               
            },
            error: function (data) {
                alert('023');
            }
        });
    }
</script> 

 
here , i am using jqery axaj direct from cdn .We will put a large text on the textarea.Call ajax

-----------------------------------------------------------------------------------------------------------------
Here is the MVC controlle code
 
public string SaveData(string Title, string Html)
        {
            SqlConnection Con = new SqlConnection(@"your connection string");
            Con.Open();            
            try
            {
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Con;
                Cmd.CommandText = "sp_blog_topics";
                Cmd.CommandType = CommandType.StoredProcedure;
                Cmd.Parameters.AddWithValue("@in_mode", 1);
                Cmd.Parameters.AddWithValue("@in_title", Title);
                Cmd.Parameters.AddWithValue("@in_html", Html);
                Cmd.ExecuteNonQuery();
                /*
                SqlDataAdapter adpt = new SqlDataAdapter(Cmd);
                DataSet ds = new DataSet();
                adpt.Fill(ds);
                 * */
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("{0} First exception caught.", e);
            }
            finally
            {
                Con.Close();
            }
            string retMsg = "Saved";
            return retMsg;
        }
The title data and html is send to stored procedure and then save
 ------------------------------------------------------------------------------
Here is the stored procedure


CREATE PROCEDURE dbo.sp_blog_topics
(
       @in_mode INT,
       @in_title VARCHAR(MAX),
       @in_html VARCHAR(MAX)
)     
AS    
       IF (@in_mode=1)
       BEGIN
       INSERT INTO tbl_topic(Title,Html)
       SELECT @in_title,@in_html
       END

       RETURN

 
 

Base class libraries C#

The .NET framework has its own Base class libraries which provide functions and features , implementation ,execution and  which can be  used with any programming language which implements .NET, such as Visual Basic, C# (or course), Visual C++, etc.

System : Responsible for  fundamentals of programming such as the data types, console, match and   arrays, loop ,event hadling

System.Data : Responsible for ADO.NET feature , dataaset , datatable,datareader ect.

System.Drawing : Responsible for  the GDI+ functionality for graphics support

System.IO: Responsible for file system and the reading and writing to data streams such as files.

System.Net: Responsible for network protocols such as SSL, HTTP, SMTP and FTP

System.Text: Responsible  for  StringBuilder class, plus regular expression capabilities.

System.Web: Namespace for ASP.NET capabilities such as Web Services and browser communication.

System.Windows.Forms: Namespace containing the interface into the Windows API for the creation of Windows Forms programs.

Thursday, 29 September 2016

Value Type & Reference Type


Value Type & Reference Type

Application memory is mainly divided into two memory component . Heap and Stack .
Heap is a separate area of memory for holding reusable objects .Actual object is allocated in Heap .A variable containing pointer to an object is allocated on stack , when call by stack actually return the memory address of the objects.

Value Type : Int/Bool/Struct /Enum
Reference Type :Class/array/interface/delegate

Value Type
  • Value Type  are stored on stack
  • Value Type  contains actual value
  • Value Type  cannot contain null values.
  • Value type is popped on its own from stack when they go out of scope.
  • For Value Type  Memory is allocated at compile time

Reference Type
  • Reference Type  are stored on heap
  • Reference Type contains reference to a value
  • Reference Type  can contain null values.
  • Reference Type required garbage collector to free memory.
  • Reference Type memory is allocated at run time
Stack
  • very fast access
  • local variables only
  • variables cannot be resized
  • It is a Last-in, First-out (LIFO) data structure
  • Static memory allocation

Heap

  • No limit on memory size
  • Variables can be resized
  • Fist in First Out
  • Dynamic memory allocation

Example of Stack
 
 int a=100;
 string ="C# program”;
  int a=5+6;

Exaple of Heap 

class MyClass
{
    int myInt = 0;
    string myString = "Something";
}

class Program
{
    static void Main(string[] args)
    {
       MyClass m = new MyClass();
    }
}

Copy Constructor


Copy Constructor : The feature is that , C# allow us to copy constructor .


Example 1
class myclass
{
    // Copy constructor.
    public myclass(myclass previousPerson)
    {
        Name = previousPerson.Name;
        address = previousPerson.Age;
    }

    // Instance constructor.
    public myclass(string name, int age)
    {
        Name = name;
        address = age;
    }
    public string address { get; set; }
    public string Name { get; set; }       
}

Here myclass is iniatialised , passing Name and address ,then the constructor is passed to another constructor is passed during initialization .This behaviors is known as copy constructor.

Private Constructor


Private Constructor : We can declare a constructor private  also ,this is a special instance constructor. Classes that contain static members only are generally used
Private construtor . By setting the access modifier to private it make clear that the constructor can not be instantiated .Generally in designed pattern , Singleton pattern
Follow the rule , where class constructor is private .This make sure in a whole solution only one instance of class can be created.

Example 1:
public class classB
{
    public int A; // Instance field
       public classB()
       {
              //
              // TODO: Add constructor logic here
              //
       }
    private classB() // This is the private constructor
    {
        this.A = 5;
    }
}

Example 2: Designed Patterns
//Create a singleton class
public sealed class myClass
{
    private static myClass instance = null;
    private static readonly object instancelock = new object();

    public myClass() //general constructor
    {
    }

    public static myClass Instance
    {
        get
        {
            lock (instancelock)
            {
                if (instance == null)
                {
                    instance = new myClass();
                }
                return instance;
            }
        }
    }
}


//Calling instance of class
    protected void Button1_Click(object sender, EventArgs e)
    {
        var instance1 = myClass.Instance;
        var instance2 = new myClass();
    }
    //only one instance of this class is possible

Static Constructor


Static Constructor : The constructor was introduced for C#. This is a special constructor and gets called before the first object is created of the class, it is use to instantaneities some static data.It is access by only static member.Statis constructor do not accept any parameter.

public class myClass
{
    protected static readonly DateTime instanceCreateTiem;
    public myClass()
       {
              //
              // TODO: Add constructor logic here
              //
       }

    //Static constructor
    static myClass()
    {
        instanceCreateTiem = DateTime.Now;

         // The following statement produces the first line of output,
         // and the line occurs only once.
         Console.WriteLine("Static constructor sets  start time to {0}",
             instanceCreateTiem.ToLongTimeString());
     }
}


//I am creating an instance of a class
//costrutorr automaticall called
myClass obj = new myClass();

In the console you will get like
Static constructor sets  start time to 12/03/2009 00:10:20:20
Which shows that static constructor works as designed by Microsoft.

The main use of static constructor to initialized some static fields to perform some specific purpose only.

Constructor C#


Constructor C#

Constructor is a special method in a class , which is called automatically when a 
Class is instant is created . The constructor is created in the same name of a class .
A constructor does not have any refund type ,but constructor can be overloaded.
In .Net , constructor declaration is not must ,, it automatically create and called by
.Net

public class myClass
{
       public myClass()
       {
              //This is a constructor
              // TODO: Add constructor logic here
              //
       }
}
 
//I am creating an instance of a class
//costrutorr automaticall called
myClass obj = new myClass();

Static Constructor : The constructor was introduced for C#. This is a special constructor and gets called before the first object is created of the class, it is use to instantaneities some static data.It is access by only static member.Statis constructor do not accept any parameter.

public class myClass
{
    protected static readonly DateTime instanceCreateTiem;
    public myClass()
       {
              //
              // TODO: Add constructor logic here
              //
       }

    //Static constructor
    static myClass()
    {
        instanceCreateTiem = DateTime.Now;

         // The following statement produces the first line of output,
         // and the line occurs only once.
         Console.WriteLine("Static constructor sets  start time to {0}",
             instanceCreateTiem.ToLongTimeString());
     }
}


//I am creating an instance of a class
//costrutorr automaticall called
myClass obj = new myClass();

In the console you will get like
Static constructor sets  start time to 12/03/2009 00:10:20:20
Which shows that static constructor works as designed by Microsoft.

The main use of static constructor to initialized some static fields to perform some specific purpose only.

Private Constructor : We can declare a constructor private  also ,this is a special instance constructor. Classes that contain static members only are generally used
Private construtor . By setting the access modifier to private it make clear that the constructor can not be instantiated .Generally in designed pattern , Singleton pattern
Follow the rule , where class constructor is private .This make sure in a whole solution only one instance of class can be created.

Example 1:
public class classB
{
    public int A; // Instance field
       public classB()
       {
              //
              // TODO: Add constructor logic here
              //
       }
    private classB() // This is the private constructor
    {
        this.A = 5;
    }
}

Example 2: Designed Patterns
//Create a singleton class
public sealed class myClass
{
    private static myClass instance = null;
    private static readonly object instancelock = new object();

    public myClass() //general constructor
    {
    }

    public static myClass Instance
    {
        get
        {
            lock (instancelock)
            {
                if (instance == null)
                {
                    instance = new myClass();
                }
                return instance;
            }
        }
    }
}


//Calling instance of class
    protected void Button1_Click(object sender, EventArgs e)
    {
        var instance1 = myClass.Instance;
        var instance2 = new myClass();
    }
    //only one instance of this class is possible

Copy Constructor : The feature is that , C# allow us to copy constructor .


Example 1
class myclass
{
    // Copy constructor.
    public myclass(myclass previousPerson)
    {
        Name = previousPerson.Name;
        address = previousPerson.Age;
    }

    // Instance constructor.
    public myclass(string name, int age)
    {
        Name = name;
        address = age;
    }
    public string address { get; set; }
    public string Name { get; set; }       
}

Here myclass is iniatialised , passing Name and address ,then the constructor is passed to another constructor is passed during initialization .This behaviors is known as copy constructor.


Constructors in Inheritance : 
Inheritance is a feature of any object oriented programming language .A class is created and its child is inherited .The first class is called Base class and rest child class .When this type of situation arrives and child class in instantiated , the
Contractor behaves as followes

Base class constructor will called first , then child class constructor will be created.

public class myBase
{
    public myBase()
    {
        Console.WriteLine("I am now in the Base class");
    } 
}

public class myDerived : myBase
{
    public myDerived()
    {
        Console.WriteLine("I am now in the Child class");
    }
}

myDerived obj = new myDerived();
        //the output will be
        I am now in the Base class
        I am now in the Child class
        //That shows that base class contructor called first the child class


বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...