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 


Wednesday, 19 February 2014

XML to Object serialization in ASP.NET Web API

Simple XML to Object serialization


class Student
public string Name;


  public string ProcessXML(Student s)

Global asax.cs

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

Now you can post XML data as below

Tuesday, 4 February 2014

Get gridview row data in c#


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


 protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
int rowindex = int.Parse(e.CommandArgument.ToString());

Wednesday, 22 January 2014

Deserialization of an XML to an object in C#

Deserialization of an XML to C# class

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


public class Book
   public string Title;

   public Publisher Publisher;

public class Publisher
  public string Value;

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


Tuesday, 11 June 2013

DataTable Column to List in c#

Using LINQ:

table.AsEnumerable().Select(s => s["ID"].ToString()).ToList();

Wednesday, 3 April 2013

Find controls in WPF

Consider this data grid in wpf

<DataGrid Name="dataGrid1">
   <DataGridTemplateColumn >

               <StackPanel Name= "sp" >
                <TextBlock  Name="textBlock1" />                                                           </StackPanel>

 <DataGridTemplateColumn >

To access element  "textBlock1" which is inside stackpanel, we can use the following code.

foreach (StackPanel sp in FindVisualChildren<StackPanel>(dataGrid1))
                        if (sp.Name == "sp")
                            TextBlock txt= (TextBlock)sp.FindName("textBlock1");       
  public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
            if (depObj != null)
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                        yield return (T)child;

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                        yield return childOfChild;

Tuesday, 5 February 2013

sql bulkcopy

sql bulk copy from datatable into sql table(with transaction)

 Dim con As New  SqlConnection("connection string")

 Dim myTransaction As SqlTransaction = con.BeginTransaction()
 Dim bulkCopy As New SqlBulkCopy(con, SqlBulkCopyOptions.Default,   myTransaction)

bulkCopy.DestinationTableName = "table name"

you can use column mapping
bulkCopy.ColumnMappings.Add("column name in datatable", "column name in sql table")