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

SQLServer : จัดการค่า NULL

NULL เป็นปัญหาถ้าไม่จัดการให้ดี

SELECT unit * quantity AS total FROM .....
มีปัญหาเกิด error ถ้าค่า unit หรือ quantity เป็นค่า NULL 

วิธีแก้ 

1. ตอน design table ให้กำหนดค่า Default value=0 และ ห้ามเป็นค่า NULL (Allow Nulls=false)


2. หรือใช้ function ช่วย 
      2.1 ISNULL(fieldname, value_replace_IF_null)
 
               เช่น 
                      SELECT (ISNULL(unit,0) * ISNULL(quantity,0)) AS total FROM.......

      2.2 COALESCE(express1,express2, ....) คืนค่า  express ตัวแรกที่ไม่ใช่ค่า NULL หรือ 
คืนค่า NULL ถ้า express ทั้งหมดเป็นค่า NULL

เช่น 
SELECT COALESCE(NULL,NULL,300,200,NULL) จะได้ค่า 300

จากตัวอย่างข้างบน 
                      SELECT (COALESCE(unit,0) * COALESCE(quantity,0)) AS total FROM.......

นอกจากนี้ยังมีปัญหาการทดสอบค่า NULL

                ....WHERE  unit_code <>  NULL จะใช้ไม่ได้ต้องเขียนแบบนี้
               .... WHERE  unit_code IS NOT NULL  หรือ  
              .......WHERE NOT ISNULL(unit_code)



วันจันทร์ที่ 16 มิถุนายน พ.ศ. 2557

sqlserver : Update First N Record

Update First N Record

ต้องการ update 10 Record แรก


WITH First10Rows AS
(
  SELECT TOP 10 Col1,Col2,Col3,...
  FROM MyTable
  WHERE Col..= .....
  ORDER BY SomeDateColumn
)
UPDATE First10Rows
SET Col1=..., Col2=...

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

asp.net : Gridview Footer

การใช้งาน Footer ใน gridview ของ asp.net

ต้องการ





 ใน event RowDataBound ของ gridview

 Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        ' check row type
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' if row type is DataRow, add RoomNumber value to TotalSales
            TotalRoomNumber += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "RoomNumber"))
        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            ' If row type is footer, show calculated total value
            e.Row.Cells(4).Text = "รวม"
            e.Row.Cells(5).Text = TotalRoomNumber.ToString("d")
            e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Center              e.Row.Cells(5).HorizontalAlign = HorizontalAlign.Center
            e.Row.Font.Bold = True  
        End If
    End Sub

วันอังคารที่ 20 พฤษภาคม พ.ศ. 2557

ASP.NET Custom Control วิธีนำ Control ที่เขียนเองมาใช้ C#

Here is how I did it, step by step starting from nothing. This first method uses a second project/assembly. For the App_code version scroll down.
Web Application Project Method
  1. Create a new ASP.Net Web Application. Take note of the name, mine is called WebApplication2. If you already have an existing web application, which is likely, double click the properties section of your project and check the "Assembly Name" property, make note of it.
  2. Create a new Class in the web applcation named Literal.cs
  3. Put in code similar to the following for the class defenition:
    namespace CustomControls
    {
        public class Literal : System.Web.UI.WebControls.Literal
        {
        }
    }
  4. Add the following register tag to the top of your aspx page
    <%@ Register assembly="WebApplication2" namespace="CustomControls" tagprefix="web" %>
If your assembly name was different then change it here. I noticed that when I did this in VB.Net the namespace was WebApplication1.CustomControls instead of just CustomControls like it was in C#, kind of odd.
  1. Add the new control to your page:
    <web:Literal ID="Literal1" runat="server" Text="test" />
Seperate Project Method
  1. Create a new Empty Website (ASP.Net).
  2. Add a new ASP.Net Server Control library named CustomControls to the solution.
  3. Add a new Class to the new project called Literal (I'm using C# so my file is named Literal.cs). Below is my super basic code, that I believe should match the code described in the question.
    namespace CustomControls
    {
        public class Literal : System.Web.UI.WebControls.Literal
        {
        }
    }
  4. Add a reference to the CustomControls project to your website.
  5. Add the assembly registration to the top of your aspx page:
    <%@ Register assembly="CustomControls" namespace="CustomControls" tagprefix="web" %>
  6. Add a new instance of the control to your page:
    <web:Literal ID="Literal1" runat="server" Text="test" />
In App_Code Method
  1. Create a new Empty Website (ASP.Net).
  2. Add a new Class to the App_Code folder Literal2 (I'm using C# so my file is named Literal2.cs). Below is my super basic code, that I believe should match the code described in the question. I called it 2 so that you can use this in conjunction with the method described above without getting compile errors
    namespace CustomControls
    {
        public class Literal2 : System.Web.UI.WebControls.Literal
        {
        }
    }
  3. Register the assembly/namespace for app_code in your aspx page by adding the following line to the top
    <%@ Register Namespace="CustomControls" Assembly="__code" tagprefix="web" %>
  4. Add an instance of the new control to your page:
    <web:Literal2 ID="literal2" runat="server" Text="test2" />
I tested this using visual studio and it all worked fine for me.

From : http://stackoverflow.com/questions/3003343/custom-control-in-asp-net-c-sharp

วันจันทร์ที่ 10 กุมภาพันธ์ พ.ศ. 2557

SQLServer การเพิ่มเลขที่ (record number) ในข้อมูล

การเพิ่ม Record number เป็นอีก column หนึ่งเพิ่มเข้ามา

        เขียนคำสั่งได้ดังนี้

SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0))AS RecNum ,* FROM table

        เราก็จะได้  field เพิ่มขึ้นอีก field ชื่อ RecNum เรียงตั้งแต่ 1,2......

         ตรง "SELECT 0" อาจจะเปลี่ยเป็น field ที่ต้องการเรียง

RecNum     field1       field2        field3
     1              006       toyata        red
     2              004       honda        blue
     3                ......
     4