วันพุธที่ 22 มีนาคม พ.ศ. 2560

แก้ปัญหา กด run แล้วไม่มี device ให้เลือกทั้งๆที่เปิด genymotion และต่อ device แล้ว เกิด error 'C:\Users\...\AppData\Local\Android\sdk\platform-tools\adb.exe,start-server' failed -- run manually if necessary

วิธีแก้ adb.exe,start-server' failed -- run manually if necessary

1. ถ้าเปิด genymotion อยู่ให้ปิด
2. ไปที่ Tools > Android > Unchecked Unable ADB Integration คลิกเพื่อเอาเครื่องหมายถูกออก
3. กด run ใหม่





ขอบคุณ ขอบใจ ขอบพระทัย แต้งกิ้ว
http://stackoverflow.com/questions/26137634/ddms-adb-exe-start-server-failed-run-manually-if-necessary-android-studio

วันเสาร์ที่ 6 สิงหาคม พ.ศ. 2559

การ Recycle view ใน listview

ในส่วนของ adapter   function getView ให้เพิ่มการ เช็คว่า view ที่ส่งมาเป็น null หรือเปล่า ถ้าเป็น ให้ สร้างขึ้นมาใหม่ ถ้าไม่เป็นแสดงวง่ามีของเก่าอยู่ ในทำของเก่านั้นกลับมาใช้ใหม่ ดังโคดข้างล่าง

public View getView(int i, View view, ViewGroup viewGroup)
ClassItem item;
if(view != null)
{
      item = (ClassItem) view;
}
else
{
     item  = new ClassItem(viewGroup.getContext());
}
return item;

วันอังคารที่ 2 สิงหาคม พ.ศ. 2559

Android Rule

- ห้ามใช้ Activity Context ใน Asynchronous Callback 

- ต้องรู้เสมอว่าทำไมถึงใช้ Context นั้นๆ

วันศุกร์ที่ 29 กรกฎาคม พ.ศ. 2559

วิธีการแสดง keyboard ใน Genymotion

1. Start virtual device


2. ไปที่ menu setting


3. ไปที่ Language & input


4. ไปที่ Default..


5. ตรง Hardware Physical keyboard จาก on ให้เปลี่ยนเป็น off 


วันเสาร์ที่ 16 เมษายน พ.ศ. 2559

Android การใช้ gilde แสดงผลแบบ วงกลม

Credit http://stackoverflow.com/questions/25278821/how-do-rounded-image-with-glide-library
มี 2 แบบ

1.
Glide.with(context).load(url).asBitmap().centerCrop().into(new BitmapImageViewTarget(imageView) {
        @Override
        protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                    RoundedBitmapDrawableFactory.create(context.getResources(), resource);
            circularBitmapDrawable.setCircular(true);
            imageView.setImageDrawable(circularBitmapDrawable);
        }
    });
2
Glide.with(this).load(URL).transform(new CircleTransform(context)).into(imageView);

public static class CircleTransform extends BitmapTransformation {
    public CircleTransform(Context context) {
        super(context);
    }

    @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }

    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        // TODO this could be acquired from the pool too
        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
        return result;
    }

    @Override public String getId() {
        return getClass().getName();
    }
} 

วันเสาร์ที่ 28 พฤศจิกายน พ.ศ. 2558

C# การสร้าง form register พร้อมการ upload รูปภาพ

1. สร้าง Model รับข้อมูล

    public class UserModel
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public HttpPostedFileBase File { get; set; }
    }

2.สร้าง function รับค่าใน controller

        public ActionResult Register(UserModel model)
        {
            if (model.File != null && model.File.ContentLength > 0)
            {
                var filename = Path.GetFileName(model.File.FileName);
                var path = Path.Combine(Server.MapPath("~/content/pics"), filename);
                model.File.SaveAs(path);
            }
            return RedirectToAction("Index");
        }

3. สร้าง form ใน view
    @using (Html.BeginForm("ชื่อฟังชัน", "ชื่อcontroller", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.LabelFor(m => m.FirstName)
        @Html.TextBoxFor(m => m.FirstName)
        @Html.LabelFor(m => m.LastName)
        @Html.TextBoxFor(m => m.LastName)
        <input type="file" name="file" />
        <input type="submit" value="Search" />
    }

วิธีแก้ปัญหาการ upload file แล้วขึ้น error : Maximum request length exceeded.

ปัญหานี้เกิดจากการที่เรายังไม่ได้แก้คอนฟิกของเว็บให้รองรับการอับโหลดไฟล์ขนาดใหญ่ได้นั่นเอง
แก้โดยการไปที่ web.config แล้วเพิ่ม

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="1048576" />
    </system.web>
</configuration>
 <system.webServer>
   <security>
      <requestFiltering>
         <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
   </security>
 </system.webServer>
แค่นี้ก็จะเรียบร้อย

C# การสร้าง From รับข้อมูล โดยใช้ razor

1.สร้าง Model รับตัวแปรที่เราต้องการ 
    public class UserModel
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }
    }

2. สร้าง View แสดงผลลัพธ์หลังจาก รับข้อมูลจาก form เสร็จ

3. สร้างฟังชัน 
        public ActionResult Search(UserModel model)
        {
            return View();
        }
4. สร้าง view รับข้อมูลจาก form
<div>
    <h1>Razor form</h1>
    @using (Html.BeginForm("Search", "Home"))
    {
        @Html.LabelFor(m => m.FirstName)
        @Html.TextBoxFor(m => m.FirstName)
        @Html.LabelFor(m => m.LastName)
        @Html.TextBoxFor(m => m.LastName)
        <input type="submit" value="Search" />
    }
</div>

วันศุกร์ที่ 27 พฤศจิกายน พ.ศ. 2558

AngularJs Post ส่งข้อมูลหลายๆข้อมูลไปที่ C# Controller

1.สร้างหน้ารับข้อมูลที่จะส่งไปให้ controller
<div ng-app="test-app">
    <h1>Angular</h1>
    <div ng-controller="test-controller">
        <input type="text" ng-model="startDate" />
        <input type="text" ng-model="endDate" />
        <button type="submit" ng-click="SendDataToController()">Sunmit</button>
        <div id="response">{{response}}</div>
    </div>
</div>

<script>
    var app = angular.module('test-app', []);
    app.controller('test-controller', function ($scope, $http) {

        $scope.SendDataToController = function () {
            var url = "./home/cal"
            $scope.DataRequest = { StartDate: $scope.startDate, EndDate: $scope.endDate }
            $http.post(url, $scope.DataRequest)
            .success(function (data, status) {
                $scope.response = data;
            })
        };
    })
</script>

2.สร้าง C#  Model
    public class RequestModel
    {
        public string StartDate { get; set; }

        public string EndDate { get; set; }
    }

3.สร้าง C# Controller รับข้อมูล
        public JsonResult Cal(RequestModel request)
        {
            return Json(request, JsonRequestBehavior.AllowGet);
        }

Jquery Ajax post ส่งข้อมูลหลายๆข้อมูล ถึง Controller ใน C#.Net

1.สร้าง view  รับข้อมูลจาก textbox เมื่อทำการกดปุ่ม Send ให้ส่งข้อมูลไปที่ controller

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<input type="text" name="name" id="startDate" value=" " />
<input type="text" name="name" id="endDate" value=" " />
<button id="submit">Send</button>
<div id="response"></div>
<script>
    $("#submit").click(function () {
        var startDate = $("#startDate").val();
        var endDate = $("#endDate").val();
        var objRequest = { StartDate: startDate, EndDate: endDate };
        $.ajax({
            type: "POST",
            url: "./home/cal",
            data: JSON.stringify(objRequest),
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                     $("#response").html(JSON.stringify(data)); },
            failure: function (errMsg) {
                alert(errMsg);
            }
        });
    });
</script>

2 สร้าง Model รับค่าใน C#

    public class RequestModel
    {
        public string StartDate { get; set; }

        public string EndDate { get; set; }
    }

3. สร้าง function ใน controller C# โดยเราจะทำการส่งข้อมูลที่ส่งมากลับไป

       public JsonResult Cal(RequestModel request)
        {
            return Json(request, JsonRequestBehavior.AllowGet);
        }

วันศุกร์ที่ 2 ตุลาคม พ.ศ. 2558

การติดตั้ง yii2

หลังจากที่ลง xampp แล้ว
ลง Composer ตามลิ้งนี้ 
จากนั้นทำการ cmd เข้าไปที่ cd  htdocs  แล้วพิม
composer global require "fxp/composer-asset-plugin:~1.0.0"
จากนั้นพิม composer create-project yiisoft/yii2-app-advanced ชื่อFolder
ถ้ามันถามหา Token ทำตามนี้ 
จากนั้นทำการ cmd ไปที่ project ที่เราสร้างไว้ แล้วพิมพ์ init เลือก 0 เลือก y
จบ

วันพฤหัสบดีที่ 17 กันยายน พ.ศ. 2558

การแก้ปัญหา autocomplete:off ไม่ทำงาน

ปัญหา:
 autocomplete:off  ไม่ทำงาน ใน form ที่มี input type="Password"
 วิธีแก้:
ให้ใส่
     
    
 ใน <form>
 <input type="password" style="display:none"> 
 
 
 

วันพฤหัสบดีที่ 3 กันยายน พ.ศ. 2558

Angular: ng-options การกำหนดข้อมูลlist2 โดยอ้างอิงข้อมูลที่เลือกจากlist1

http://jsfiddle.net/RK6P9/200/

 <select ng-model="item.ProcessId"
       
    ng-options="c.ProcessId as c.ProcessName  for c in columns"></select>
  
       {{item.ProcessId}}
        <select ng-model="item.refprocessId"

            ng-options="c.ProcessId as c.ProcessName  for c in columns | filter:{ProcessId: '!'+item.ProcessId }"></select>

 $scope.columns = [
       {"ProcessId":173,"ProcessName":"Paperwork","PlanTypeId":3},{"ProcessId":270,"ProcessName":"Test Update Process 14/8/2015","PlanTypeId":1},{"ProcessId":174,"ProcessName":"Floor","PlanTypeId":1},{"ProcessId":175,"ProcessName":"Wall","PlanTypeId":1},{"ProcessId":176,"ProcessName":"Roof","PlanTypeId":1},{"ProcessId":240,"ProcessName":"Prepare project","PlanTypeId":3},{"ProcessId":142,"ProcessName":"Base","PlanTypeId":1},{"ProcessId":143,"ProcessName":"Foundation","PlanTypeId":1},{"ProcessId":144,"ProcessName":"Floor","PlanTypeId":1}
    ];



วันจันทร์ที่ 13 กรกฎาคม พ.ศ. 2558

ASP.NET การใช้งาน AutoMapper ในกรณีที่ ชื่อ property ใน Model ปลายทาง ไม่ตรงกับ Model ต้นทาง

ทำเหมือน AutoMapper ธรรมดาเลย แต่เพิ่ม code ส่วนนี้เข้าไป

            Mapper.CreateMap<Modelต้นทาง, Modelปลายทาง>()
               //config property ที่ชื่อต้นทางกับปลายทางไม่ตรงกัน 
              //เช่น ต้นทาง: Id , ปลายทาง: UserId
                .ForMember(
                    dst => dst.UserId ,
                    opt => opt.MapFrom(src => src.Id ))
              //ทำเหมือนข้างบนไปเรื่อยๆในอันที่ชื่อไม่ตรงกัน
                .ForMember(
                    dst => dst.EndDate,
                    opt => opt.MapFrom(src => src.EndDatePlan));

รวม code จะได้เป็น
        public static Modelปลายทาง Toชื่อที่มันสื่อว่ามันแมพไปModelปลายทางนะ(this Modelต้นทาง model)
        {

            Mapper.CreateMap<Modelต้นทาง, Modelปลายทาง>()
                .ForMember(
                    dst => dst.UserId ,
                    opt => opt.MapFrom(src => src.Id ))
                .ForMember(
                    dst => dst.ชื่อตัวแปรของ Property Model ปลายทาง,
                    opt => opt.MapFrom(src => src.ชื่อตัวแปรของ Property Model ต้นทาง));

            return Mapper.Map<
Modelปลายทาง>(model) ?? new Modelปลายทาง();
        }

วันจันทร์ที่ 6 กรกฎาคม พ.ศ. 2558

ASP.NET การใช้งาน AutoMapper

AutoMapper ช่วยเราในการ Map Model จาก Repository เข้ากับ Service
เราไม่ต้อง foreach เองในกรณีที่ส่งมาเป็น List<>

ขั้นตอนการทำงาน
1. ไปที่ folder Service แล้วสร้าง folderตามชื่อ Service จากนั้นสร้างไฟล์ ชื่อService+Mappper
2. ใส่ using AutoMapper;
3 ตัวอย่าง     public static class TestMapper
    {
//กรณี Model ที่เป็น List
        public static List<ชื่อ Model ใน Service> To+ชื่อ Model ใน Service(this List<ชื่อ model ที่ส่งมาจาก Repository> entities)
        {
            return Mapper.Map<List<ชื่อ Model ใน Service>>(entities) ?? new List<ScheduleMemberInGroupViewModel>();
        }
 ***** ?? new List<ScheduleMemberInGroupViewModel>(); แปลว่ากรณีที่ไม่มีข้อมูลใน Return Model List<ScheduleMemberInGroupViewModel> เปล่าๆกลับไป ****

//กรณี Model ที่ไม่ได้เป็น List
        public static ScheduleMemberInGroupViewModel  ToTestViewModel(this UserInCompanies entities)
        {
            return Mapper.Map<ScheduleMemberInGroupViewModel>(entities) ?? new  ScheduleMemberInGroupViewModel;
        }
    }
4. ไปที่ Project Web ->App_Start->AutoMapperConfig
    public partial class AutoMapperConfig
    {
        public static void RegisteredAutoMapper()
        {
            ControllerAutoMapperBootstrapper.Configure();

            Mapper.CreateMap<ชื่อ model ที่ส่งมาจาก Repository, ชื่อ Model ใน Service>();
        }
     }
5. สร้าง Model ใน Service ต้องตั้งชื่อตัวแปรให้ตรงกับ Model ที่ส่งมาจาก Repo ด้วยนะไม่งั้นมัน Map ไม่ได้จะเออเร่อ
6. การใช้งาน
    .Toชื่อMapper ที่เราจะ Map ระวังว่าต้อง type ตรงกับที่เราMapด้วยนะ
7. เปิดหน้าที่เราจะใช้ Mapper แล้วพิมพ์ using Pathที่เราเก็บ ชื่อMapper ไว้                                          ถ้าไม่พิมมันจะหาไม่เจอโง่ชิบหายเลย

วันเสาร์ที่ 27 มิถุนายน พ.ศ. 2558

วันศุกร์ที่ 19 มิถุนายน พ.ศ. 2558

การใช้งาน Yii2 url แบบไม่ต้องมี index.php

1.เปิดใน config->web.php เพิ่ม

'urlManager' => [
    'enablePrettyUrl' => true,    'showScriptName'=>false,// hide index.php    'rules' => [
        'หน้าแรก' => 'site/index',    ]
],
 ในส่วนของ 'components' จะได้ตามรูปล่างนะ

2. เข้าไปที่ folder web จากนั้นก็ สร้างไฟล์ .htaccess แล้วพิมตามโลด

RewriteEngine on # if a directory or a file exists, use it directlyRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.phpRewriteRule . index.php 


 เท่านี้เวลาเราสร้าง controller แล้วเรียก funtion จะเป็นแบบนี้

localhost/webname/web/controllerName/functionName







วันพฤหัสบดีที่ 11 มิถุนายน พ.ศ. 2558

การใช้งาน ASP.NET แบบ Run เว็บได้แม้ปิด Debug Mode

คือ Run Web ค้างไว้ แล้วสามารถแก้ไขโคดได้ด้วยใน Controller  Service

1. ไปที่ Project MVC ของเรา แล้ว คลิกขวาเลือก Properties

2. เลือก Web ด้านซ้ายมือ 


3. ติ๊ก Enable Edit and Continue ออกให้ เหลือแต่ช่องล่าง

แล้วรันใหม่ แค่นี้กะใช้ได้ละ

ASP.net API การรับข้อมูล method Get ที่มี parameter ซ้อนอยู่ข้างในอีกที

    API Controller 
  [HttpGet]
        public void test([FromUri]TestModel model)
        {

        }

 Model
public class TestModel
{
     public Guid Key{get;set;}
}

เวลาเรียกใช้งาน

pathAPI/test?key=B912D178-DE60-4C69-AA8E-2B2714F96FDB


***เมตหาย
ถ้ามี Model ใหญ่ ครอบตัวแปรเราอีกที่ต้องมี [FromUri] ด้วยเสมอไม่ง้านมันหาไม่เจอ
ถ้าเรารับพารามิเตอเป็น Guid ก็ส่งต้องค่าเป็น guid แม้จะส่งเป็นสตริงก็ตาม คือจำนวนตัวอักษรต้องเท่ากับ Guid