.NET + SQL Server พี่ครับผมช่วยดู Error ของข้อมูล long binary data ที่แสดงใน DataGridView หน่อยครับ
ลองดูครับ Ex. c# ดูไฟล์
Preview binary data store in sql server to cilent browser by Gridview
1. Set up Gridview in html tax.
Code (C#)
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetDataSrh"
TypeName="superbo.ksn.Data.KsnFileTableAdapters.KsnFileSrhTableAdapter"
EnablePaging="True" MaximumRowsParameterName="MaxRows"
SelectCountMethod="MaxRowsSrh" StartRowIndexParameterName="RowIndex">
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
CssClass="table_edit" DataSourceID="ObjectDataSource1"
EnableModelValidation="True" ForeColor="#333333" GridLines="None"
AllowPaging="True" OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged"
EnableSortingAndPagingCallbacks="True" Caption="จำนวนข้อมูล 0 รายการ"
CaptionAlign="Right">
<AlternatingRowStyle BackColor="#E6ECF7" />
<EditRowStyle BackColor="#BECFEB" />
<FooterStyle BackColor="#3366CC" Font-Bold="True" ForeColor="White"
HorizontalAlign="Left" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerSettings Mode="NumericFirstLast"
FirstPageImageUrl="~/images/34all2/tostart.png"
LastPageImageUrl="~/images/34all2/toend.png"
NextPageImageUrl="~/images/34all2/foward.png"
PreviousPageImageUrl="~/images/34all2/backward.png" />
<PagerStyle BackColor="White" ForeColor="#3366CC" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" VerticalAlign="Top" />
<SelectedRowStyle BackColor="#B3C7E8" Font-Bold="True" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="Id_KsnFile" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="Id_KsnFile">
<ItemStyle Width="60px" />
</asp:BoundField>
<asp:BoundField DataField="Id_GroupFile" HeaderText="ลำดับกลุ่ม"
SortExpression="Id_GroupFile" ReadOnly="True">
<ItemStyle Width="60px" />
</asp:BoundField>
<asp:BoundField DataField="GroupName" HeaderText="กลุ่ม"
SortExpression="GroupName" ReadOnly="True" >
<ItemStyle Width="250px" />
</asp:BoundField>
<asp:TemplateField HeaderText="ภาพแสดง" ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ibtnView" runat="server" CssClass="ImageBox1" CausesValidation="False"
CommandName="View" ImageUrl=""
ToolTip="ภาพ" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ibtnEdit" runat="server" CausesValidation="False"
CommandName="Edit" ImageUrl="~/images/Function_Icon_Set/paper_content_pencil_48.png"
ToolTip="แก้ไขข้อมูลรายการนี้" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
Interface as
2. Create code in event that fix in Gridview (hidden code or exist script runat server)
Code (C#)
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (!String.IsNullOrEmpty(e.Row.Cells[0].Text))
{
ImageButton ibtnView = (ImageButton)e.Row.FindControl("ibtnView");
if (ibtnView != null)
{
ibtnView.CommandArgument = e.Row.Cells[0].Text;
ibtnView.ImageUrl = "../Shared/LoadContent.aspx?fz=ksnfile&fid=" + e.Row.Cells[0].Text;
ibtnView.OnClientClick = "window.open('../Shared/LoadContent.aspx?fz=ksnfile&fid=" + e.Row.Cells[0].Text + "');";
}
ImageButton ibtnEdit = (ImageButton)e.Row.FindControl("ibtnEdit");
if (ibtnEdit != null)
{
ibtnEdit.CommandArgument = e.Row.Cells[0].Text;
}
//ImageButton ibtnDelete = (ImageButton)e.Row.FindControl("ibtnDelete");
//if (ibtnDelete != null)
//{
// ibtnDelete.CommandArgument = e.Row.Cells[0].Text;
//}
}
}
3. LoadContent.aspx is blank page that have code this
Code (C#)
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (!String.IsNullOrEmpty(this.Request.QueryString["fz"]))
{
bool havefile = false;
//ksnfile (TKsnFile)
if (this.Request.QueryString["fz"].ToString() == "ksnfile")
{
if (!String.IsNullOrEmpty(this.Request.QueryString["fid"]))
{
int fileID = Convert.ToInt32(this.Request.QueryString["fid"].ToString());
superbo.ksn.Data.KsnFileTableAdapters.TKsnFileTableAdapter da = new superbo.ksn.Data.KsnFileTableAdapters.TKsnFileTableAdapter();
superbo.ksn.Data.KsnFile.TKsnFileDataTable dt = da.GetFileByPK(fileID);
if (dt != null)
{
foreach (superbo.ksn.Data.KsnFile.TKsnFileRow dr in dt.Rows)
{
Response.BinaryWrite(dr.FileObject);
havefile = true;
}
}
dt.Dispose();
da.Dispose();
}
}
}
}
}
4. Try it.
Date :
2009-11-18 23:37:00
By :
bo
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace ASPNetBLOB
{
public partial class ViewImg : System.Web.UI.Page
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd;
string strConnString;
string strSQL;
protected void Page_Load(object sender, EventArgs e)
{
strConnString = "Server=localhost;Uid=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn.ConnectionString = strConnString;
objConn.Open();
//*** DataTable ***'
SqlDataAdapter dtAdapter = default(SqlDataAdapter);
DataTable dt = new DataTable();
strSQL = "SELECT * FROM files WHERE FilesID = @sFilesID ";
dtAdapter = new SqlDataAdapter(strSQL, objConn);
objCmd = dtAdapter.SelectCommand;
objCmd.Parameters.Add("@sFilesID", SqlDbType.Int).Value = Request.QueryString["FilesID"].ToString();
dtAdapter.Fill(dt);
if (dt.Rows.Count > 0)
{
Response.ContentType = dt.Rows[0]["FilesType"].ToString();
Response.BinaryWrite((byte[])dt.Rows[0]["FilesName"]);
}
dt = null;
}
protected void Page_Unload(object sender, System.EventArgs e)
{
objConn.Close();
objConn = null;
}
}
}
Go to : (C#) ASP.NET SQL Server BLOB Binary Data and Parameterized Query
Date :
2011-06-02 18:15:52
By :
webmaster
Load balance : Server 03