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

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">
  <DataGrid.Columns>                      
   <DataGridTemplateColumn >
    <DataGridTemplateColumn.CellTemplate>
     <DataTemplate>

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

      <DataTemplate>
   <DataGridTemplateColumn.CellTemplate>
 <DataGridTemplateColumn >
<DataGrid.Columns>       
</DataGrid>

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");       
                             txt.Text="Hello";
                          }
                        }
                   
 FindVisualChildren
  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")
 con.Open()

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


bulkCopy.DestinationTableName = "table name"
bulkCopy.WriteToServer("datatable")
myTransaction.Commit()
bulkCopy.Close()

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