Tuesday, November 6, 2018

Sample Web.config file for a WCF service - with Baisc Authentication enabled in IIS

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpEndpointBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="WcfServerTest.Service1" name="WcfServerTest.Service1">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding" name="BasicHttpEndpoint" contract="WcfServerTest.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServerTest.Service1">
          <!-- To avoid disclosing metadata information, set the value below
               to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below
              to true. Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <security>
      <authentication>
        <anonymousAuthentication enabled="false" />
        <windowsAuthentication enabled="false" />
      </authentication>
    </security>
  </system.webServer>

</configuration>

Tuesday, March 17, 2015

Handling larger JSON string value in MVC and avoiding exceptions

Working with JSON recently has become the latest and greatest simply because it plays so nicely with others and can often be very easily serialized and deserialized to fit your needs.

We can convert a massive collection of complex objects into a simple string and passed across and then completely regenerated in its original collection .
However sometimes these strings can get big and I mean really big, like exceeding default values. and when this happens, exceptions happen.

In this post I will explain how you can prevent it in MVC .
You can simply add the MaxJsonLength property to a specific value depending on the context of your specific action :
JsonResult jResult = Json(users, JsonRequestBehavior.AllowGet);
jResult.MaxJsonLength = int.MaxValue;
return jResult;
However, if you want a more widespread solution that you could use within a single area of your application, you may want to consider overriding the JsonResult class and set your value within this newer class :
// This ActionResult will override the existing JsonResult and will automatically set the 
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new JsonResult()
    {
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding,
        JsonRequestBehavior = behavior,
        MaxJsonLength = Int32.MaxValue // Use this value to set your maximum size for all of your Requests
    };
}

Tuesday, December 4, 2012

XML file parsing in DTS




Here my requirement was to fetch an XML file from a physical location and send that XML data to SQL server and then SQL server will parse those data and save those into DB.
 

For this I have done the following steps

1. Created a Global variable in the DTS (Menu->Package->Properties)



2. I have added an ActiveX Script Task which will read the XML file from physical 
    location and assign the XML file content inside the global variable created earlier.



3. Then Created an Execute SQL Task to call the stored procedure with the global 
    variable 


4. The stored procedure will parse that XML and save into DB

Tuesday, August 9, 2011

A potentially dangerous Request.Form value was detected from the client

For .net 4.0 if you get the above error you have to add
<httpRuntime requestValidationMode="2.0" />
Inside the <system.web> tags and

ValidateRequest="false" in <Page> directive of .aspx page

Thursday, June 23, 2011

Oracle cache dependency in asp.net

private DataSet bindCachedData()
{
string sql = "SELECT * FROM np_pucm_refresh_cal";
constr = "user id=NPUSER;data source=PNPSCAL;password=NPUSER;Min Pool
Size=1;Connection Lifetime=0;Connection Timeout=60;Incr Pool
Size=1;Decr Pool Size=1;";
DataSet ds;
ds = (DataSet)Cache.Get("UserTable");

if (ds ==null)
{
lblMsg.Text = "Data fetched from Table";
using (OracleConnection con = new OracleConnection(constr))
{
con.Open();

using (OracleCommand cmd = new OracleCommand(sql, con))
{
OracleDependency dep = new OracleDependency(cmd);
cmd.Notification.IsNotifiedOnce = false;
dep.OnChange += new OnChangeEventHandler(OnMyNotificaton);

OracleDataAdapter da = new OracleDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);

Cache.Insert("UserTable", ds);
}
}
}
else
lblMsg.Text = "Data fetched from Cache";

return ds;
}

public void OnMyNotificaton(object src, OracleNotificationEventArgs arg)
{
DataTable changeDetails = arg.Details;
Cache.Remove("UserTable");
lblMsg.Text = "Data fetched from Table";
}

protected void Timer1_Tick(object sender, EventArgs e)
{
GridView1.DataSource = bindCachedData();
GridView1.DataBind();
}

Wednesday, June 15, 2011

Import a div's content into excel using javascript

Call the bellow js function "OnClientClick" event of a button

<script language="javascript" type="text/javascript">
function ABC() {
var strCopy = document.getElementById('calendar').innerHTML;
window.clipboardData.setData("Text", strCopy);
var objExcel = new ActiveXObject("Excel.Application");
objExcel.visible = true;

var objWorkbook = objExcel.Workbooks.Add;
var objWorksheet = objWorkbook.Worksheets(1);
objWorksheet.Paste;

return false;
}
</script>

Sunday, April 10, 2011

Create a Win Form with curved corners

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SampleFormApplication
{
public partial class Login : Form
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect,
int nWidthEllipse,
int nHeightEllipse
);

public Login()
{
InitializeComponent();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width - 10, Height - 10, 50, 50));
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}