Monday 23 June 2014

validation of viewstate MAC failed in ASP.NET Web Form

validation of viewstate MAC failed

There are few reason for this error. Below is one of them.

Check the idle time of application pool in IIS and try to increase.

  1. Open IIS
  2. Click on application pools node
  3. Locate your web application's application pool
  4. Right-Click and select Advanace Settings
  5. Set the Idle Time-out(minutes) property to 0 or increase 

Ref:
stackoverflow

Wednesday 19 February 2014

XML to Object serialization in ASP.NET Web API

Simple XML to Object serialization


Model

class Student
{
[XmlElement("Name")]
public string Name;

Controller

 [HttpPost]
  public string ProcessXML(Student s)
   {
        //code
   } 

Global asax.cs

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
                 xml.SetSerializer<Student>(new XmlSerializer(typeof(Student))); 

Now you can post XML data as below
<Student>
<Name>balaji</Name>
</Student>
 

Tuesday 4 February 2014

Get gridview row data in c#

Grid

  <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false"            onrowcommand="grid_RowCommand" >
 <Columns>
 <asp:TemplateField >
 <ItemTemplate>
 <asp:TextBox ID="txt"  runat="server" Text='<%#Eval("xxx") %>'  >                   </asp:TextBox>
  </ItemTemplate>
  </asp:TemplateField>
 <asp:TemplateField >
 <ItemTemplate>
<asp:LinkButton ID="lnk" CommandArgument=<%# Container.DataItemIndex + 1 %> CommandName="arg">Click</asp:LinkButton>
  </ItemTemplate>
  </asp:TemplateField>
  </Columns>
  </asp:GridView>

Code

 protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowindex = int.Parse(e.CommandArgument.ToString());
((TextBox)(grid.Rows[rowindex].FindControl("txtgvunit"))).Text
}

Wednesday 22 January 2014

Deserialization of an XML to an object in C#

Deserialization of an XML to C# class

 XML
<Book Title="My Book">
   <Publisher Reference="XYZ123">Some Publisher</Publisher>
</Book>

Class

[XmlRoot("Book")]
public class Book
{
   [XmlAttribute]
   public string Title;

   [XmlElement]
   public Publisher Publisher;
}

[Serializable]
public class Publisher
{
  [XmlText]
  public string Value;

  [XmlAttribute]
  public string Reference;
}
 
 
Book res = new Book(); 
XmlTextReader reader = new XmlTextReader("file path"); 
res = (Book)serializer.Deserialize(reader);


Ref
stackoverflow