Sqlite.Interop.dll Could not be loaded MVC

had both x64 and x86 folders with respective dll inside the bin folder of my mvc application, IUSR and IIS_IUSR rights also have been provided , still the error was throwing up, after some digging around s.o this answer among a lot of other useful answers turns to be the one working for me so for my memory the steps involved are

Open IIS Manager
Find the Application Pool your site is running under.
Click Advanced Settings from the actions
Change Identity to LocalSystem

Thanks SO Community.

System.BadImageFormatException: Could not load file or assembly

When trying to run my MVC application targeting 64 bit environment, i got this error as usual tried a lot with configuration manager without any effect, finally a google search leads to this page https://www.koskila.net/fixing-not-load-file-assembly-one-dependencies-error/
which has an absolute spot on answer (w.r.t my case) , make the iis express to run in 64 bit mode will remove this error,(Visual Studio – Tools – Options – Projects and Solutions – Web Projects), for regular deployment on iis change the enable 32 bit applications app_pool property to false will help to resolve this error. (from statckoverflow) . Learning never cease to exist.

There is no ViewData item of type ‘IEnumerable’ that has the key

when adding data from controller to dropdown list in view, this might happen sometimes, the root cause in most case is the viewdata is returning null , some other points to check are your function generating viewdata should not be private , the name of the dropdownlist and your viewdata property are not same etc, In my case the name mismatch happened. and this so link helped to find out that, will ensure to remember that next time. for reference the offending code block is

ViewBag.ddlsec = new SelectList(lst.Select(x => x.section).Distinct().ToList())

@Html.DropDownList(“section”, ViewBag.ddlsec as List, “Select”, htmlAttributes: new { @class = “form-control” })

replace section in razor with ddlsec solved the issue

Linq and Dynamic Columns

System.Linq.Dynamic has been around well let’s say from 2008 with the advent of Framework 4.0, but i have missed it most probably i was still using 3.5 at that time and not very much familiar with Linq. After exactly 10 years later I had a request to show only columns in a gridview / table /datatables which are filtered from an UI kind of reporting stuff , and my boss also hinted me to this website Dynamic Linq from scottgu

As usual i was bit lazy to read and understand the article instead searched a lot in google probably the date of article also in 2008. I thought some latest techniques might be there in framework versions > 4.5 , after some 2 to 3 hours of searching i am repeatedly pointed to this same solution. So went back there and searched for a downloadable link but to my surprise i can’t find one. Search again for the System.Linq.Dynamic keyword resulted in https://github.com/kahanu/System.Linq.Dynamic  repository. From there it’s a piece of cake. Nuget install the package , and in my cs

String columnstodisplay=”fielda,fieldb”;

var x= list.select(“new (” + columnstodisplay + “)”);

this results in an IEnumerable just contains only the fields we have requested. Bind to your table container and all is good.

This is for my memory.

ref: https://stackoverflow.com/questions/8770897/linq-access-property-by-variable

 

 

SQL Server Error 26 Error Locating Server/Instance specified

Seems a straight forward error, check the tcp connectivity and all usual suspects like checking port no 1433 for tcp connection over firewall etc.  But there is a trick in the error in small but bold letters there is a message sql express database file auto-creation error:

this is a different error and i have encountered it for the first time. but fortunately after some search in stackoverflow there is https://stackoverflow.com/a/1402588/186244 , this link suggest check for rolemanager in web config.

But i have not used role manager in that particular project, which once again little bit wired , so i once again checked the web config file line by line and my bad the file is having the tag <roleManager enabled=”true”/> which some what found its place in to this we config. so i just changed true to false and obviously error has gone.

Lesson learnt be careful while copy pasting.

 

 

multipart/form-data and asmx

For a project i was trying to pass formdata which holds input type file to be processed by asp.net web service (.asmx) . The service will upload the file to a specific folder and update the database with relevant values.

To my surprise i was not able to do that in the way i used to pass ajax data , it starts to throw some random error .

After  a deep search, i found the answer in stackoverflow,

.NET will not allow the multipart/form-data for the content-type:

which is also having a link pointing to the Scottgu’s website.

essential these are trying to prevent JSON hijacking attacks. all the asmx webmethod  unless until specified is always defaults to POST , the user has to specify explicitly the web method to use httpget to use GET method while making an ajax call.

and other important thing is


There is a built-in validation layer of protection that ASP.NET enforces for
both GET and POST based ASP.NET AJAX web methods, which is thatregardless
of the HTTP verb being used, ASP.NET always requires that the HTTP
Content-Type header is set to the value application/json. It this
content type header is not sent, ASP.NET AJAX will reject the request on the
server.
This particular thing is preventing me from posting formdata to the webmethod. Either i have to serialize the form or i have to use generic handler (.ashx) to handle file upload. Since i am not sterilizing the form because i am creating formdata on runtime , i have to settle with a generic handler to upload the file.
 
sigh… every day i am learning something.

system.invalidoperation exception request format is unrecognized for URL unexpectedly ending in ‘/yourfunctionname’

A more or less obscure error when trying to access a asmx webmethod using ajax .

The solution is here

http://stackoverflow.com/a/657315/186244

for unknown reasons httppost and httpget are disabled by default. so enabling them in webconfig will resolve this issue most of the time.

  <system.web>
    <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
    </system.web>

https://support.microsoft.com/en-us/kb/819267

asp.net treeview and jquery

I found this snippet to check nodes and child nodes in asp.net Treeview control using jquery which is very useful. But actually there is a small issue with it. Till Jquery 1.4 the click event of check box will work properly along all browsers but the change will not , but later versions the change is working very good.

And also the code in the sample works only once for me, but not the second time so i searched again in google and found this link which provides explanation for the behaviour and reported as a bug in Jquery itself. The page also provides the solution. for further assistance to me as usual stackoverflow has the working example for the issue. so the final code is

$(function () {
$("[id*=TreeView1] input[type=checkbox]").on("change", function () {
var table = $(this).closest("table");
if (table.next().length > 0 && table.next()[0].tagName == "DIV") {
//Is Parent CheckBox
var childDiv = table.next();
var isChecked = $(this).is(":checked");
$("input[type=checkbox]", childDiv).each(function () {
// if (isChecked) {
// $(this).attr("checked", "checked");
// } else {
// $(this).removeAttr("checked");
//}
$(this).prop('checked',this.checked);
});
} else {
//Is Child CheckBox
var parentDIV = $(this).closest("DIV");
if ($("input[type=checkbox]", parentDIV).length == $("input[type=checkbox]:checked", parentDIV).length) {
$("input[type=checkbox]", parentDIV.prev()).prop("checked", true);
} else {
$("input[type=checkbox]", parentDIV.prev()).prop("checked",false);
}
}
});
})