Friday, July 29, 2011

Cannot insert explicit value for identity column in table ‘x' when IDENTITY_INSERT is set to OFF

Almost all the solution suggests to modify the Id column for that error to set IsDbGenerated=true. But I have some other problems. I have to delete my dbml and therefore regenerate to solve this. Another big bug of linq.

Thursday, July 28, 2011

Model binding insquential lists

<% using (Html.BeginCollectionItem("datesCollection"))
   {%>

The above statement helped me a lot when it came to dynamic population of insequential list in the page. And ofcourse the famous jquery event "live" to bind events to the listitems.






Tuesday, July 12, 2011

ASP .Net MVC, Linq, AutoComplete

Lots of things today. Lots of bugs. We all know this:

public JsonResult GetHotelsByPartialName(string hotelname) {
            var hotelList = AccommodationService.GetAccommodationByPartialname(hotelname);
            return Json(hotelList);
}

But we need to use a simple viewmodel object call JsonHotel here, cause asp .net mvc is not passing the jsonresult for linq classes without even giving an error probably because of lots of properties :

var accommodations = (from o in adc.Accommodations
                                  where o.HotelName.Contains(hotelname)
                                  select new JsonHotel { HotelID = o.HotelID, HotelName = o.HotelName, Region = o.Region }).ToList();

 In the end the below function is very significant: You can see in that the url being used like url: "/Home/GetHotelsByPartialName?hotelname=" + text instead of json style thats cause that probably wont work but webmethods. Removing all row from the table except of first: $('table').find("tr:gt(0)").remove(). This function was used for a autocomplete textbox to filter on table.
          
        function GetData(text) {
            $.ajax({
                url: "/Home/GetHotelsByPartialName?hotelname=" + text,
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    $('table').find("tr:gt(0)").remove();
                    $.map(data, function (item) {
                        $('table').append(
                                    '<tr><td><a href="/Home/Accommodation/' + item.HotelID + '">Edit</a>' +
                                        '</td><td>' + item.HotelName +
                                        '</td><td>' + item.Region +
                                        '</td></tr>');
                    });
               },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });

Monday, July 11, 2011

Can't connect to local MySQL server through socket /var/lib/mysql/mysql.sock

sudo chown -R mysql /var/lib/mysql/
sudo chgrp daemon /var/lib/mysql/
sudo /etc/init.d/mysql stopsudo /etc/init.d/mysql start

thats it to your solution to the problem.

Saturday, July 9, 2011

File Uploads of ASP .NET MVC saving image in database

[HttpPost]
        public ActionResult PartialPhotoDetail(int hotelID, HttpPostedFileBase hotelPhoto) {

            if (hotelPhoto.ContentLength > 0) {
                byte[] fileBytes = new byte[hotelPhoto.ContentLength];
                int byteCount = hotelPhoto.InputStream.Read(fileBytes, 0, hotelPhoto.ContentLength);

                AccommodationService.InsertHotelPhoto(new Photo { HotelID = hotelID, photo = fileBytes });
            }

            return RedirectToAction("Accommodation", new { id = hotelID, tab = 4 });
        }

public ActionResult GetHotelPhoto(int id) {
            var hotelPhoto = AccommodationService.GetHotelPhotoById(id);
            return new FileContentResult(hotelPhoto.photo.ToArray(), "image/png");
        }

Thursday, July 7, 2011

Using Jquery dialog on confirmation

$("a[name='delete']").click(function(e){
                e.preventDefault();
                var href = $(this).attr("href");

                $( "#dialog-confirm" ).dialog({
                buttons: {
                        "Delete": function() {
                            window.location.href = href;
                        },
                        Cancel: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });

                $( "#dialog-confirm" ).dialog( "open" );
            });

Monday, July 4, 2011

ASP .Net MVC DropDownList selected value confusion

I just spent an hour around this silly problem. Well its confusing enough from the doc and I should probably blog here so that noone gets into it spending hours.

 <%= Html.DropDownListFor(h => h.HotelMarket.ID, new SelectList(Model.Markets, "Market", "Market", Model.HotelMarket))%>

here you should remember that  h.HotelMarket.ID is the property to be binded to and Model.HotelMarket is the object that this property belongs. Simple but confusing...




Sunday, July 3, 2011

Linq to SQL not generating Associating Property

Its a common problem but it requires a simple solution. Just add or update a column of each table in a relation to be primary key. Then save and you would see associating property.