Friday, May 20, 2011

How to: Move a Database Using Detach and Attach (Transact-SQL)

You can move a detached database to another location and re-attach it to the same or a different server instance.

Attach the moved database and, optionally, its log by executing the following Transact-SQL statements:

USE master;
GO
CREATE DATABASE MyAdventureWorks
ON (FILENAME = 'C:\MySQLServer\AdventureWorks2008R2_Data.mdf'),
(FILENAME = 'C:\MySQLServer\AdventureWorks2008R2_Log.ldf')
FOR ATTACH;
GO

For a production database, place the database and transaction log on separate disks.

Enjoy on this !

Monday, May 16, 2011

MVC validation on controller.

The following link has very useful information for validation of mvc .

Before in my knowledge, I do understand the validation of mvc has to be placed on the model layer. That gives some headache to me to find right entity in entities.

That a pieces of codes saves my headache. That's just brilliant. Thanks to who posted this post. http://www.asp.net/mvc/tutorials/creating-model-classes-with-the-entity-framework-cs



public ActionResult Add()
{
return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(FormCollection form)
{
var movieToAdd = new Movie();

// Deserialize (Include white list!)
TryUpdateModel(movieToAdd, new string[] { "Title", "Director" }, form.ToValueProvider());

// Validate
if (String.IsNullOrEmpty(movieToAdd.Title))
ModelState.AddModelError("Title", "Title is required!");
if (String.IsNullOrEmpty(movieToAdd.Director))
ModelState.AddModelError("Director", "Director is required!");

// If valid, save movie to database
if (ModelState.IsValid)
{
_db.AddToMovieSet(movieToAdd);
_db.SaveChanges();
return RedirectToAction("Index");
}

// Otherwise, reshow form
return View(movieToAdd);
}

Tuesday, May 10, 2011

Microsoft SQL Server 2008 - Saving changes is not permitted

Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created.

I have the same problem above. If you reference this site, you will get the resolve the problem.

http://www.sqlcoffee.com/Troubleshooting074



Thursday, May 5, 2011

Google map api for searching lat and lng by postcode and country name

This is a big achivement for Today. I was strugling with Italy google map. the issue was italy postcode has same format with US postcode. so, google api gives returns us map instead of italy map.

I solved this issue with some codes which does reference to this site

http://www.storm-consultancy.com/blog/development/code-snippets/using-google-maps-api-to-get-latitude-longitude-co-ordinates-from-postcode-or-address/

very genius of that.
public LatLng GetLatLng(string addr)
33 {
34 var url = "http://maps.google.co.uk/maps/geo?output=csv&key=" +
35 this.API_KEY + "&q=" + HttpContext.Current.Server.UrlEncode(addr);
36
37 var request = WebRequest.Create(url);
38 var response = (HttpWebResponse)request.GetResponse();
39
40 if (response.StatusCode == HttpStatusCode.OK)
41 {
42
43 var ms = new MemoryStream();
44 var responseStream = response.GetResponseStream();
45
46 var buffer = new Byte[2048];
47 int count = responseStream.Read(buffer, 0, buffer.Length);
48
49 while (count > 0)
50 {
51 ms.Write(buffer, 0, count);
52 count = responseStream.Read(buffer, 0, buffer.Length);
53 }
54
55 responseStream.Close();
56 ms.Close();
57
58 var responseBytes = ms.ToArray();
59 var encoding = new System.Text.ASCIIEncoding();
60
61 var coords = encoding.GetString(responseBytes);
62 var parts = coords.Split(",");
63
64 return new LatLng(
65 Convert.ToDouble(parts[2]),
66 Convert.ToDouble(parts[3]));
67 }
68
69 return null;
70 }
71 }

Have fun with this !

Tuesday, May 3, 2011

How to postback on index change of ASP.net MVC dropdownlist?

How to postback on index change of ASP.net MVC dropdownlist?

I have found this on internet from this site

http://www.altafkhatri.com/Technical/ASP-NET-MVC-Dropdownlist/How-to-post-back/on-selectedIndexChanged

It is really helpful post. I put this on here future reference.

Steps to create the postback in ASP.Net MVC dropdownlist selectedindex change event:

Create a controller - Dropdownlist. Paste the code in the section below.
Create a view of the GetDD action binded to the strongly typed object(CoverObject). Paste the code in the section below.


Code in the controller

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Mvc.Ajax;



namespace MvcApplication1.Controllers

{

public class CoverObject

{

public IList iliSLI;

public string SelectedName { get; set; }

public string Name { get; set; }

public string Address { get; set; }



public string formAction { get; set; }



public CoverObject()

{

iliSLI = new List();

SelectListItem sli = new SelectListItem();

sli.Text = "Altaf";

sli.Value = "1";



iliSLI.Add(sli);



sli = new SelectListItem();

sli.Text = "Krish";

sli.Value = "22";

iliSLI.Add(sli);



sli = new SelectListItem();

sli.Text = "Sameer";

sli.Value = "3333";

iliSLI.Add(sli);



sli = new SelectListItem();

sli.Text = "Iqbal";

sli.Value = "44444";

iliSLI.Add(sli);



sli = new SelectListItem();

sli.Text = "Maimoona";

sli.Value = "5555";

iliSLI.Add(sli);



}

}

public class DropdownlistController : Controller

{

//

// GET: /Dropdownlist/

public ActionResult Index()

{

return View();

}



public ActionResult GetDD()

{

CoverObject co = new CoverObject();

co.formAction = "";

//ViewData["SecretQuestion"] = co.iliSLI;

return View(co);

}



[AcceptVerbs(HttpVerbs.Post)]

public ActionResult GetDD(CoverObject co)

{

CoverObject coNew = new CoverObject();

string indexChangedValue = co.SelectedName;





var item = from it in coNew.iliSLI

where it.Value == co.SelectedName

select it;

item.First().Selected = true;

coNew.Name = item.First().Text;

coNew.Address = co.Address;





if (!string.IsNullOrEmpty(co.formAction) && co.formAction.Equals("Submit Form By Clicking"))

{

// Based on the main submit of the form, it can process and redirect or stay on the same view.

ModelState.AddModelError("_FORM", "Form Submitted by clicking submit button");

}

else//Submitted by dropdownlist index change event

{

//DROPDOWNLIST on selectedIndexChangedEvent Handled hereModelState.AddModelError("_FORM", "Form Submitted by clicking submit button");

ModelState.AddModelError("iliSLI", "Form Submitted by selected Index Change event");

}

return View(coNew);

}

}

}



Code in the aspx file

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>











GetDD







<%= Html.ValidationSummary("Form submitted by what event notification:") %>



<% using (Html.BeginForm()) {%>





Fields







<%= Html.TextBox("co.Address", Model.Address)%>

<%= Html.ValidationMessage("Address", "*") %>









<%= Html.DropDownList("co.SelectedName", Model.iliSLI, new { onChange = "onSelectedIndexChanged()" })%>

<%= Html.ValidationMessage("iliSLI", "*") %>









<%= Html.TextBox("Name", Model.Name, new { disabled="true" })%>

<%= Html.ValidationMessage("Name", "*") %>















<% } %>