顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2018年3月18日 星期日

[C#] Telerik RadComboBox 注意事項 problem

當設定 RadComboBox MaxLength=n時,但在 CodeBehide 或是 ASP.NET RadComboBoxItem Text 多加一些前置文字,但Value不會超過MaxLength,Text字數大於MaxLength會導致表單送(POST)不出去。

如有此需求,這最好是改用 ItemTemplate 另外設計新介面,較為恰當。

2018年3月15日 星期四

[C#] SqlDataSource 不允許從資料類型 sql_variant 隱含轉換到 uniqueidentifier。請使用 CONVERT 函數來執行查詢。

在VS2010 Design介面設定SqlDataSource時,勾選從系統產生的預防資料同步修改時,產生的衝突 ASP.NET 代碼,當資料庫欄位為 uniqueidentifier 時,ASP.NET代碼產生:

<asp:Parameter Name="original_gid" Type="Object" />

此會造成

System.Data.SqlClient.SqlException    不允許從資料類型 sql_variant 隱含轉換到 uniqueidentifier。請使用 CONVERT 函數來執行查詢。

此時要把 Type="Object" 刪除,改成如下:

<asp:Parameter Name="original_gid" />

Refer:

http://www.blueshop.com.tw/board/FUM20041006161839LRJ/BRD200710311108001NI.html

https://dotblogs.com.tw/mis2000lab/archive/2010/11/03/uniqueidentifier_sqldatasource.aspx

2016年7月3日 星期日

Remove RadGrid DetailTables AutoGeneratedColums

Refer:

Problem: I can’t take the result by using RadGrid’s ItemCommand() that autogenerated fixed columns once created. So I can’t get dynamic detail tables.

 

Solution: Should use RadGrid’s PreRender() . This is an important concept here.


protected void MainList_PreRender(object sender, EventArgs e)
{
    RadGrid thisGrid = sender as RadGrid;
    thisGrid.MasterTableView.DetailTables.Clear();
    thisGrid.MasterTableView.DetailTables.Add(new GridTableView());
    foreach (GridDataItem item in MainList.MasterTableView.Items)
    {
        if (item.Expanded)
        {
            GridTableView nestedView = (GridTableView)item.ChildItem.NestedTableViews[0];

            nestedView.DataSource = your_dataSource;
            nestedView.DataBind();
        }
    } 
}

 

[C#] LinqDataSource by using FK to Get data, using two tables

Description: I want to show Table1 through PK2.

DB ERD:

(Table1,PK1)----(Relation_Table2_has_Table1, contained the Primary Key of Table1,Table2)----(Table2,PK2)

ASP.Net Code:

<asp:linqdatasource id="LinqDataSource1" runat="server" contexttypename="LinQDataContext" enableupdate="True" entitytypename="" tablename="Table1" where="Relation_Table2_has_Table1.Any(PK2==@data)">
    <whereparameters>
        <asp:sessionparameter name="data" sessionfield="data" type="String" />
    </whereparameters>
</asp:linqdatasource>

If @data is Int, then PK2==Int32?(@data)

Refer: http://forumarray.com/c-binding-linqdatasource-to-two-separate-table-516379

Visual Studio 2010 “Server Explorer”, “LinqToSQL DBML” Bug

LinqToSQL DBML with re-dragging tables from VS Server Explorer, there is a Bug:

當資料庫表格及關連都建立好了之後,修改一個誤打的欄位名稱,再重新抓表格,結果DBML檔不會自動建立關係,需要重新命名新的表格名稱。(看似有某種快取的影響, 重開Visual Studio就可以了。)

Solution: Restart Visual Studio.

2016年6月6日 星期一

telerik:RadCaptcha 使用技巧

telerik:RadCaptcha

預設的排版格式不一定會符合大眾的需求,這邊簡介幾個供客製。

這本身就是 Validator。

屬性:

Display="None" 設定 ErrorMessage 不顯示。

CaptchaImage-RenderImageOnly,True: 不顯示使用者輸入欄位(INPUT)

ValidatedTextBoxID,指定自設的輸入欄位

2016年6月1日 星期三

C# 取得呼叫的功能名稱, Get Caller Member Name, Performance Compared

Refer: http://stackoverflow.com/questions/1044519/get-property-name-inside-setter

取得呼叫的功能名稱,

public static int Dummy {
    get {
        var propertyName = MethodBase.GetCurrentMethod().Name.Substring(4);
        Console.WriteLine(propertyName);
        return 0;
    }
}
或
public static int Dummy {
    get {
        var propertyName = MethodInfo.GetCurrentMethod().Name.Substring(4);
        Console.WriteLine(propertyName);
        return 0;
    }
}

CallerMemberName 功能是 .NetFrameWork 4.5以後的功能,

Refer: https://msdn.microsoft.com/zh-tw/library/system.runtime.compilerservices.callermembernameattribute(v=vs.110).aspx

版本資訊
Universal Windows Platform
自 4.5 起可用
.NET Framework
自 4.5 起可用
Portable Class Library
支援版本:portable .NET platforms
Windows Phone Silverlight
自 8.0 起可用
Windows Phone
自 8.1 起可用

private static object GetSessionValue([CallerMemberName]string propertyName = "") 
{
    return Session[propertyName];
}

private static void SetSessionValue(object value, [CallerMemberName]string propertyName = "") 
{
    Session[propertyName] = value;
}

public int MyIndex
{
    get { return (int)GetSessionValue(); }
    set { SetSessionValue(value); }
}

【Performance,實測效能】,

使用Visual Studio 2013開發,.Net Framework 4.5,
各跑10000次的時間:

MethodInfo:		00:00:00.0222161
CallerMemberName:	00:00:00.0006698
MethodBase:		00:00:00.0183167
StackFrame:		00:00:00.0870535
StackTrace:		00:00:00.1230636
Constant String:	00:00:00.0005125
MethodInfo與MethodBase基本大致上是接近的。
StackFrame與StackTrace在執行時需要new出來,可能因此花了多一些時間。
Constant String與CallerMemberName兩者也是接近的,猜想CallerMemberName在編譯時就寫入固定字串了。

Code: Program.cs

2016年5月24日 星期二

C#產生 Json 的寫法,並加入時間格式轉換器

使用Newtonsoft.Json library 使用兩種不同的寫法,產生相同結果:

型式1:適合動態加入json欄位

JObject result = new JObject();
        
JsonSerializer jsr = new JsonSerializer();
jsr.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd" });

JArray jpool = JArray.FromObject(pool, jsr);

result.Add("data", jpool);

return result.ToString();
型式2:直接回應
return @"{""data"": "+ Newtonsoft.Json.JsonConvert.SerializeObject(pool, Formatting.None, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd" }) +" }";

2016年5月19日 星期四

自訂工具介紹--LINQtoSQL: Customize the Code Generated by the Designer

Refer: http://www.codeproject.com/Articles/31519/LINQtoSQL-Customize-the-Code-Generated-by-the-Desi

 

如要自訂 Linq To Sql 的 Class 可以用參考網址的方式來設定。

轉貼介紹如下:

Introduction

LINQ to SQL is used to manipulate data in a SQL Server database. Visual Studio ships with the LINQtoSQL designer which is a very powerful tool to generate LINQ. It is fast, easy to use, and great in lots of aspects.

There is a shortcoming though. You cannot customize the generated code. As with basically all the other designers, it works, it's easy to crank up a demo, but once you get in real life situations, you always want that little bit more. That extra bit is always something that depends on the problem. I will, therefore, not attempt to create better LINQ to SQL code. I will just show you how to customize and enrich the generated code in a matter of minutes. The customization is completely transparent to the standard LINQtoSQL designer, and you will still be using it for modeling your classes and data access.

2016年5月15日 星期日

Microsoft.Ace.OleDb C# 開啟Excel發生例外錯誤

問題:c# 開啟Excel發生例外錯誤,「Source: Microsoft JET Database Engine, ErrorMessage: 定義太多欄位。」

實測:

//此連接可以操作.xls與.xlsx文件
Provider=Microsoft.Ace.OleDb.12.0;data source=excelFile;Extended Properties='Excel 12.0; IMEX=1'

結果:Microsoft.Ace.OleDB.12.0 Fill()函式可以讀取最多可 255 欄位,超過的部分則不予讀取,只Fill到255欄,不會丟出例外錯誤。

image

image若可以讀到IV欄,則會顯示欄名「讀取不到」,但只讀到IU欄。

image

 

//此連接只能操作Excel2007之前(.xls)文件
Provider=Microsoft.Jet.OleDb.4.0;data source=excelFile;Extended Properties='Excel 8.0; IMEX=1'

結果:Microsoft.Jet.OleDb.4.0 Fill()函式可以讀取最多可 255 欄位,超過則發生例外錯誤「定義太多欄位。」