Thursday, October 29, 2015

AngularJS - How to GET and POST data by http methods Get / Post

In this post, we will see how to use http requests in AngularJS. For the scope of this post, I assumed that we have already developed ASP.NET WebAPI, say for Products. So we have a ProductController which have action methods for CRUD operations. Lets see how we call that methods from AngularJS, I am using AngularJS factory method as a service componenet to communicate with server.

For example the following code snippet assumes you have a app module varaible defineds as demoApp, here we are creating a productFactory with a dependency parameter $http which we use to make requests to server.
demoApp.factory('productFactory', function ($http) {
This is how we create a get request over $http object, we passed a URL to ProductController and calling its Get method, which will return data in json string. We also passed 2 callback functions as parameters, first one is called if the request is successfull and the second will be called if request encountered any error.
 success(function (data, status, headers, config) {
  // successfully get json response from server
  console.log("get products list - success");
 error(function (data, status, headers, config) {
  // log error
This is how we can create a post request over $http object. First define an object, say config, which will define the request headers or other configuration properties. Here we are passing three parameters to the Post function, first is the URL with Controller's post action method, second is the object we want to pass as parameter/data to that function, third is our configuration object we want to set with this request.
  var config = {
  headers: { 'Content-Type': 'application/json' }

$'api/Product/PostProduct', product, config)
  function (response, status, headers, config) {
     //sucessfully posted to server
     console.log('PostProduct success: ' + product.Id);
  function (response, status, headers, config) {         
     //log error
     console.log('PostProduct failed.', response, status, headers);
Similarly following code segment will make a delete http request. Here we are only passing productID as query string parameter to the required URL, i.e. Controller's delete action method will accept an integer variable productID as parameter.
  var params = "productID=" + id;
$http.delete('api/Product/DeleteProduct?' + params)
  function (response, status, headers) {
   //sucessfully processed delete request
   console.log('DeleteProduct success: ');
  function (response, status, headers) {
   //log error
   console.log('DeleteProduct failed.', response, status, headers);
Hope this post helps you, give an idea how to deal with http methods in AngularJS.

Saturday, October 24, 2015

No type was found that matches the controller named 'values'.

I created a new project for, added a default controller named ValuesController1. When I tried to run and call the controllers action methods I got this error:
No type was found that matches the controller named 'values'.

Even after a whole day spent on googling, I could not find any solution. There are so many post/suggestions to solve this error but no one worked for me. Then I just
tried simply to change the controller's name and it works amazingly.

When a new controller is added, it was like this:
  ValuesController1 : ApiController

I just simply removed "1" from the controller's name, and make it:

  ValuesController : ApiController

And it works in my case.

Monday, October 12, 2015

MS SQL Server - Find physical folder path for data files using master.dbo.xp_regread

In previous post, we found a way to get the location path to store database files for SQL Server. Another way to find the physical path is master.dbo.xp_regread or master.dbo.xp_instance_regread. You can use these procedures to find the physical path where MS SQL Server stores database files.

xp_regread reads the exact literal registry path you specify, while xp_instance_regread reads(or can transform) the path you specify so that it matches the instance of SQL Server that you're currently using.

Lets see an example:
declare @MasterMdfFile1 nvarchar(512)
exec master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer\Parameters', N'SqlArg0', @MasterMdfFile1 output

select @MasterMdfFile1 = substring(@MasterMdfFile1, 3, 255) --removes extra '-d' in-front of path

declare @MasterMdfFile2 nvarchar(512)
exec master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer\Parameters', N'SqlArg0', @MasterMdfFile2 output

select @MasterMdfFile2 = substring(@MasterMdfFile2, 3, 255) --removes extra '-d' in-front of path
select @MasterMdfFile1 as MasterMdfFile1, @MasterMdfFile2 as MasterMdfFile2
Sample Output:
MasterMdfFile1    MasterMdfFile2
NULL                             C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008R2SP2\MSSQL\DATA\master.mdf

Here we see that master.dbo.xp_regread returns NULL to @MasterMdfFile1 variable, because it could not find the literal registry path we provided. But master.dbo.xp_instance_regread is showing the result because it converts the path that matches the current instance of SQL Server we are using.

MS SQL Server - Find physical folder path for data files using T-SQL

While deploying my application, I want to make it simplify for user to create / restore / drop databases. So I plan to automate these tasks into script and just provide a simple UI to accomplish these tasks with simple clicks. I need to find out the physical path where my database files are being stored. The following query will give you the physical path to store database's mdf and ldf files for your current SQL Server's instance.
SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1) PhysicalPath
FROM master.sys.master_files
WHERE database_id = 1 AND FILE_ID = 1

Sample Output:
C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008MYINSTANCE\MSSQL\DATA\

Here we retrieve the mdf file path for master database, i.e. database_id = 1, and file_id = 1 (file_Id = 2 will give you ldf file path, folder path will be same). Then we extract only the folder path by using substring() function.