2011年1月31日 星期一

正確處理 combobox 的 lostfocus

於 LostKeyboardFocus 事件函數中判斷
if (!(e.NewFocus is ComboBoxItem) && e.OldFocus == sender)
{
// 要處理的事情
}

2011年1月28日 星期五

使用 AJAX 的 cascadingdropdown 於下拉選單出現 [方法錯誤500]

因為下拉選單的資料量超出預設的限制造成
必須於 web.config 增加一段設定以便擴充限制

<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="500000" />
</webServices>
</scripting>
</system.web.extensions>

2011年1月27日 星期四

SQL Server 填補字串右邊字元函數

create FUNCTION dbo.PadRight
(
@PadChar char(1),
@PadToLen int,
@BaseString varchar(100)
)
RETURNS varchar(1000)
AS
BEGIN
DECLARE @Padded varchar(1000)
DECLARE @BaseLen int
SET @BaseLen = datalength(@BaseString)
IF @BaseLen >= @PadToLen
BEGIN
SET @Padded = @BaseString
END
ELSE
BEGIN
SET @Padded = @BaseString + REPLICATE(@PadChar, @PadToLen - @BaseLen)
END
RETURN @Padded
END

註 : 此函數會將中文當作兩個字元看待,以便使用固定寬度字型時能夠正確對齊中英文混雜的資料

2011年1月20日 星期四

ListView 搭配資料庫中的圖片

1. 在ListView中放置顯示圖片及上傳圖片的控制項,圖片網址為一泛行處理常式
<asp:Image ID="picAlbum" runat="server" ImageUrl='<%# "ShowImage.ashx?id=" + Eval("id") %>' />
<asp:FileUpload ID="imgUpload" runat="server" />

2. 撰寫泛行處理常式顯示資料庫中的圖片欄位
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int id;
if (context.Request.QueryString["id"] != null)
id = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");

context.Response.ContentType = "image/jpeg";
Stream strm = GetImage(id);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);

while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}

private Stream GetImage(int id)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT 照片 FROM 照片表格 WHERE ID = @ID";
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", id);
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
}
}
}

public bool IsReusable
{
get
{
return false;
}
}
}

3. 於datasource的inserting、updating事件函數中取得使用者選擇的檔案作為參數值
private Byte[] getImage(FileUpload img)
{
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
//To create a PostedFile
HttpPostedFile File = img.PostedFile;
//Create byte Array with file len
imgByte = new Byte[File.ContentLength];
//force the control to load data in array
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
return imgByte;
}

4. 針對updating或inserting事件必須考慮使用者未選取任何檔案時會遇到資料型別不符問題,因為null會當作nvarchar型態造成語法錯誤問題,所以必須抽換參數改用SqlParameter並指定型別為Image,因為原本的參數型別不包含image
e.Command.Parameters.Remove(e.Command.Parameters["@照片"]);
e.Command.Parameters.Add(new SqlParameter("@照片", System.Data.SqlDbType.Image));

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new var newmodel = _contextXXX.CreateProxy<yyy>(); ... _contextXXX.yyy.Add(newmodel); await _contextXXX.SaveC...