Archive for category Tech Tips-.Net

The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.

I have got the following error when I was developing a gridview editing on a webpage last week. This is most common issue, even I have faced several times earlier. But I want to keep this solution for beginners!

Here is the full error message:

"Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request"

error

My 1st suggestion is just check the cache! Meaning..clear your browser cache and refresh it and see whether it is working? if not continue with one of the following solutions!

1. Enable ViewState is false

Find the control which is giving problem in the page and then disable the ViewState! For me it was a  DropDownList so,

<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="false">
</asp:DropDownList>
</EditItemTemplate>

2. Just place the data binding on the GridView events wherever you use the edit! For and example “RowCancelingEdit”

Protected Sub GridViewRelease_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) Handles GridViewRelease.RowCancelingEdit
GridViewReleaseAnalyst.EditIndex = -1
BindData()
End Sub

That’s all, No more ViewState issue! Happy Programming!!

References:

  1. http://forums.asp.net/t/1368283.aspx/1
Advertisements

, , , , , , ,

1 Comment

Difference between ‘Shadowing’ and ‘Overriding’ (Shadowing vs Overriding) – C#

This is a very quick Tech-Tips..! Actually Shadowing is VB.Net concept, in C# this concept called hiding! We will see this chapter later. 🙂

Shadowing : Creating an entirely new method with the same signature as one in a base class.

Overriding: Redefining an existing method on a base class.

  • Both are used when a derived class inherits from a base class
  • Both redefine one or more of the base class elements in the derived class

If you want to read more regarding this, just follow this MSDN topic.


class A
 {
 public int M1()
 {
 return 1;
 }

 public virtual int M2()
 {
 return 1;
 }
 }

class B : A
 {
 public new int M1()
 {
 return 2;
 }

 public override int M2()
 {
 return 2;
 }
 }

class Program
 {
 static void Main(string[] args)
 {
 B b = new B();
 A a = (A)b;

 Console.WriteLine(a.M1()); //Base Method
 Console.WriteLine(a.M2()); //Override Method
 Console.WriteLine(b.M1());
 Console.WriteLine(b.M2());
 Console.Read();
 }
 }

The output is : 1, 2, 2, 2

, , , , ,

1 Comment

Difference between ‘ref’ and ‘out’ parameters (ref vs out) – C#

  • Both are treated differently @ runtime but same in the compile time, so can’t be overloaded.
  • Both are passed by references except ‘ref’ requires that the variable to be initialized before passed.

ref:

class Program
 {
 static void M1(out int i)
 {
 i = 1;
 i += 1;
 }

static void Main(string[] args)
 {
 int j; //Not assigned
 M1(out j);
 Console.Write(j);
 Console.Read();
 }
 }
}

The output is : 2

out:

class Program
 {
 static void M1(ref int i)
 {
 i = 1;
 i += 1;
 }

 static void Main(string[] args)
 {
 int j=0; //Assigned
 M1(ref j);
 Console.Write(j);
 Console.Read();
 }
}

The output is same: 2, but we have to assigned the variable before used otherwise we will get an error.

, , , ,

2 Comments

Constructors and Destructors (C#)

This is for my quick references and I have taken most of the information from MSDN and other web sites. BTW I have written and tested the source!

Constructors
– Constructors are methods that are executed when an object of a class is created.
– They have the same name as the class.
– A class  may have multiple constructors that take different arguments.
– Constructors enable the programmer to set default values.
– Like class, structs also can have constructors.

There are 3 kind of constructors in .Net (as far as I know), they are

  • Instance Constructors -Used to create and initialize instances of the class
  • Private Constructors – A special type of instance constructor that is not accessible outside the class (cannot be instantiated)
  • Static Constructors – Automatically initialize the class before the first instance is created or any static members are referenced
Example:
public class A //Class Name A
{
public int i;
public A() // Constructor of A
{
i=1;
}
}
class Program
{
static void Main(string[] args)
{
A a = new A(); //Initiated with new Operator
Console.WriteLine("The initialized value is: {0}", a.i);

System.Console.ReadLine(); //This line is to stop the result window
}
}

Destructors
– are used to destruct instances of classes.
– cannot be defined in structs. They are only used with classes.
– A class can only have one destructor.
– cannot be inherited or overloaded.
– cannot be called. They are invoked automatically
– does not take modifiers or have parameters.

Example:
class A
{
public A()
{
Console.WriteLine("Constructor");
}
~A()
{
Console.WriteLine("Destructor");
}
}

 

class Program
{
static void Main(string[] args)
{
A a = new A(); //Initiated with new Operator

System.Console.ReadLine(); //This line is to stop the result window
}
}

Leave a comment