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

Export DataTable To Excel ไม่ต้องสร้าง Excel Tag

 ที่มา : https://social.msdn.microsoft.com/Forums/en-US/812b55a1-73e1-47cd-beb2-06c49d1187d1/export-datatable-to-excel?forum=aspgettingstarted

By : User-2119480821 posted

ใส่ในปุ่ม Export หน้า ExportToExcel.aspx ได้เลย

//*********************

  string constr = @"Server=xxxx;Database=xxxxxx;UID=xxxx;PWD=xxxxxxxx;Application Name=exportExcel";

  using (SqlConnection con = new SqlConnection(constr))

  {

       using (SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[tblCustomers] with(nolock)"))

       {

             using (SqlDataAdapter sda = new SqlDataAdapter())

             {

                  cmd.Connection = con;

                  sda.SelectCommand = cmd;

                   using (DataTable dt = new DataTable())

                   {

                       sda.Fill(dt); // select data to datatable

                       GridView tmpGv = new GridView();

                       tmpGv.DataSource = dt;

                       tmpGv.DataBind();

                       System.IO.StringWriter sw = new System.IO.StringWriter();

                       System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

                            tmpGv.RenderControl(htw);

 

                            Response.Clear();

                            Response.Charset = "";

                            Response.ContentEncoding = System.Text.Encoding.UTF8;

 

                            Response.Cache.SetCacheability(HttpCacheability.NoCache);

                            Response.ContentType = "application/vnd.xls";

                            Response.AddHeader("content-disposition", "attachment;filename=" + "test.xls");

                            Response.Write(sw.ToString());

                            sw = null;

                            htw = null;

                            Response.Flush();

                            Response.End();

                        }

                    }

                }

  }

//***************

/* asp.net VB.net

            Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

                Dim constr As String = "Server=xxx;Database=xxx;UID=xxxxxx;PWD=xxxxxxx;Application Name=testexportExcel"

 

                Using con As SqlConnection = New SqlConnection(constr)

 

                    Using cmd As SqlCommand = New SqlCommand("SELECT * FROM customers")

 

                        Using sda As SqlDataAdapter = New SqlDataAdapter()

                            cmd.Connection = con

                            sda.SelectCommand = cmd

 

                            Using dt As DataTable = New DataTable()

                                sda.Fill(dt)

                                Dim tmpGv As GridView = New GridView()

                                tmpGv.DataSource = dt

                                tmpGv.DataBind()

                                Dim sw As System.IO.StringWriter = New System.IO.StringWriter()

                                Dim htw As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(sw)

                                tmpGv.RenderControl(htw)

                                Response.Clear()

                                Response.Charset = ""

                                Response.ContentEncoding = System.Text.Encoding.UTF8

                                Response.Cache.SetCacheability(HttpCacheability.NoCache)

                                Response.ContentType = "application/vnd.xls"

                                Response.AddHeader("content-disposition", "attachment;filename=" & "test.xls")

                                Response.Write(sw.ToString())

                                sw = Nothing

                                htw = Nothing

                                Response.Flush()

                                Response.[End]()

                            End Using

                        End Using

                    End Using

                End Using

            End Sub

*/


วันพุธที่ 5 มิถุนายน พ.ศ. 2562

How To Migrate asp.net(under .net 4.5 ) website to TLS 1.2?


เมื่อมีการปิด TLS1.0
Error : The request was aborted: Could not create SSL/TLS secure channel.
ที่มา : https://github.com/navossoc/KeePass-Yet-Another-Favicon-Downloader/issues/23
สาเหตุ : อ่านเองนะ https://www.swoo.co.uk/blog/post/40135/how-to-migrate-a-net-website-to-tls-12

วิธีการแก้ไข ที่ง่ายได้ผล
Old Code : เดิมมีให้เลือกแค่นี้(VB.net)

// Only available from .Net 4.5+
 
// System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;

New Code : ตามนี้คับ
************************************************************
Dim _Tls12 As System.Security.Authentication.SslProtocols = CType(&HC00, System.Security.Authentication.SslProtocols)

Dim Tls12 As SecurityProtocolType = CType(_Tls12, SecurityProtocolType)
ServicePointManager.SecurityProtocol = Tls12
************************************************************

วิธีนี้ไม่ต้อง update .net Framework ได้ผลสำหรับผมนะ

วันพฤหัสบดีที่ 20 เมษายน พ.ศ. 2560

Install IIS on windows10

ไปที่ Control Panel\All Control Panel Items\Programs and Features























set Application Pools >> Enable 32-Bit Applications=True






วันเสาร์ที่ 29 ตุลาคม พ.ศ. 2559

Microsoft SQL Server T-SQL date and datetime formats

Microsoft SQL Server T-SQL date and datetime formats
– Date time formats – mssql datetime 
– MSSQL getdate returns current system date and time in standard internal format
SELECT convert(varchar, getdate(), 100) – mon dd yyyy hh:mmAM (or PM)
                                                                   – Oct  2 2008 11:01AM        
SELECT convert(varchar, getdate(), 101) – mm/dd/yyyy - 10/02/2008                
SELECT convert(varchar, getdate(), 102) – yyyy.mm.dd – 2008.10.02          
SELECT convert(varchar, getdate(), 103) – dd/mm/yyyy
SELECT convert(varchar, getdate(), 104) – dd.mm.yyyy
SELECT convert(varchar, getdate(), 105) – dd-mm-yyyy
SELECT convert(varchar, getdate(), 106) – dd mon yyyy
SELECT convert(varchar, getdate(), 107) – mon dd, yyyy
SELECT convert(varchar, getdate(), 108) – hh:mm:ss
SELECT convert(varchar, getdate(), 109) – mon dd yyyy hh:mm:ss:mmmAM (or PM)
                                                                    – Oct  2 2008 11:02:44:013AM  
SELECT convert(varchar, getdate(), 110) – mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) – yyyy/mm/dd
SELECT convert(varchar, getdate(), 112) – yyyymmdd
SELECT convert(varchar, getdate(), 113) – dd mon yyyy hh:mm:ss:mmm
                                                                   – 02 Oct 2008 11:02:07:577    
SELECT convert(varchar, getdate(), 114) – hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 120) – yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 121) – yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 126) – yyyy-mm-ddThh:mm:ss.mmm
                                                                   – 2008-10-02T10:52:47.513
– SQL create different date styles with t-sql string functions
SELECT replace(convert(varchar, getdate(), 111), ‘/’, ‘ ‘) – yyyy mm dd
SELECT convert(varchar(7), getdate(), 126)                 – yyyy-mm
SELECT right(convert(varchar, getdate(), 106), 8)          – mon yyyy

วันศุกร์ที่ 19 สิงหาคม พ.ศ. 2559

convert web asp.net from .net framework 2.0 to .net framework 3.5 fix Ajax return msg.d

เพิ่ม tag javascript ในหน้าที่มีการเรียกใช้ ajax

$.ajaxSetup({
            dataFilter: function (data) {
                return JSON.stringify($.parseJSON(data).d);
            }

        });

เนื่องจาก project มีการเรียกใช้หลายที่ ทำให้แก้ไขยาก ถ้าใช้วิธีนี้ไม่ต้องแก้ไข code เก่าเลย ลดงาน ลดเวลา ไม่กระทบงานเก่า

วันพุธที่ 13 มกราคม พ.ศ. 2559

เว็บเล่น file media video

ตัวอย่าง
http://www.mia-pi.ca/promo_video2.html





 ที่มา
http://www.mia-pi.ca/promo_video2.html

View Code

วันอาทิตย์ที่ 19 เมษายน พ.ศ. 2558

Error message 401.2.: Unauthorized: Logon failed due to server configuration. Verify that you have permission to view this directory or page based on the credentials you supplied and the authentication methods enabled on the Web server. Contact the Web server's administrator for additional assistance.



วิธีแก้ไข
folder: %windir%\Microsoft.Net\Framework\v2.0.50.27\CONFIG\web.config
************************************
<system .web="">
        <authorization>
            <allow users="*" verbs="GET,POST">
        </allow></authorization>
</system>
************************************
แก้ไขเป็น
*********************************
<system .web="">
        <authorization>
            <allow users="*" verbs="">
        </allow></authorization>
</system>
*********************************