本文介绍了网格视图中的问题从数据库中检索图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在为我的项目工作。我已成功将图片插入数据库,但是当我想使用网格视图在网页上显示时,它会显示其他详细信息但不显示图像。 I am Working on My Project. I have Successfully Insert Pictures into the the database but when i want to display this on the web page using Grid View, it displays other details but doesn't display the images.using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Configuration;using System.Data;public partial class AddParty : System.Web.UI.Page{ DBClass db1 = new DBClass(); protected void Page_Load(object sender, EventArgs e) { } protected void BtnAddParty_Click1(object sender, EventArgs e) { int userId = 0; string message = string.Empty; try { FileUpload img = (FileUpload)PartyimgUpload; FileUpload img2 = (FileUpload)PartyFlagimgUpload; Byte[] imgByte = null; Byte[] imgByte2 = null; if (img.HasFile && img.PostedFile != null) { //To create a PostedFile HttpPostedFile File = PartyimgUpload.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); } if (img2.HasFile && img2.PostedFile != null) { //To create a PostedFile HttpPostedFile File1 = PartyFlagimgUpload.PostedFile; //Create byte Array with file len imgByte2 = new Byte[File1.ContentLength]; //force the control to load data in array File1.InputStream.Read(imgByte, 0, File1.ContentLength); } db1.sqlcmd = new SqlCommand("UspAddParty"); using (SqlDataAdapter sda = new SqlDataAdapter()) { db1.sqlcmd.CommandType = CommandType.StoredProcedure; db1.sqlcmd.Parameters.AddWithValue("@PartyName", TxtBxPartyName.Text.Trim()); db1.sqlcmd.Parameters.AddWithValue("@PartyLeader", TxtBxPLeader.Text.Trim()); db1.sqlcmd.Parameters.AddWithValue("@PartyAddress", TxtBxPAdress.Text.Trim()); db1.sqlcmd.Parameters.AddWithValue("@PartyLogo", imgByte); db1.sqlcmd.Parameters.AddWithValue("@PartyFlag", imgByte2); db1.sqlcmd.Parameters.AddWithValue("@PartyDescription", TxtBoxPartyDescription.Text.Trim()); db1.sqlcmd.Connection = db1.sqlcon; db1.sqlcon.Open(); userId = Convert.ToInt32(db1.sqlcmd.ExecuteScalar()); //Response.Write(userId); //int id = Convert.ToInt32(cmd.ExecuteScalar()); //lblResult.Text = String.Format("Employee ID is {0}", id); //Image1.ImageUrl = "~/ShowImage.ashx?id=" + id; switch (userId) { case -1: message = "Party Name already exists.\\nPlease choose a different Party Name."; break; default: message = "Registration successful.\\nParty Id: " + userId.ToString(); break; } ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true); } } catch { } finally { TxtBxPartyName.Text = null; TxtBoxPartyDescription.Text = null; db1.sqlcon.Close(); Server.TransferRequest(Request.Url.AbsolutePath, false); } }} CREATE PROC UspAddParty @PartyName NVARCHAR(30),@PartyLeader Varchar(50),@PartyAddress Varchar(max), @PartyLogo image,@PartyFlag image, @PartyDescription VARCHAR(MAX)ASBEGIN SET NOCOUNT ON; IF EXISTS(SELECT PartyId FROM TblParty WHERE PartyName = @PartyName) BEGIN SELECT -1 -- Party exists. END ELSE BEGININSERT INTO TblParty (PartyName,PartyLeader,PartyLogo,PartyFlag,PartyAddress,PartyDescription) VALUES(@PartyName,@PartyLeader,@PartyLogo,@PartyFlag,@PartyAddress,@PartyDescription) SELECT @@IDENTITY SELECT SCOPE_IDENTITY() -- PartyId ENDEND 这是在网页上显示整个数据的代码。 Here is the Code For Displaying This Whole data on the Web page.<div style="width:96%; height:450px; margin:0 auto; margin-removed70px; border:3px groove black; overflow: scroll;"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Horizontal"> <Columns> <asp:BoundField DataField="PartyName" HeaderText="PartyName" SortExpression="PartyName" /> <asp:BoundField DataField="PartyLeader" HeaderText="PartyLeader" SortExpression="PartyLeader" /> <asp:BoundField DataField="PartyAddress" HeaderText="PartyAddress" SortExpression="PartyAddress" /> <asp:BoundField DataField="PartyLogo" HeaderText="PartyLogo" SortExpression="PartyLogo" /> <asp:BoundField DataField="PartyFlag" HeaderText="PartyFlag" SortExpression="PartyFlag" /> <asp:BoundField DataField="PartyDescription" HeaderText="PartyDescription" SortExpression="PartyDescription" /> </Columns> <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F7F7F7" /> <SortedAscendingHeaderStyle BackColor="#4B4B4B" /> <SortedDescendingCellStyle BackColor="#E5E5E5" /> <SortedDescendingHeaderStyle BackColor="#242121" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ECII(Old)ConnectionString %>" SelectCommand="SELECT [PartyName], [PartyLeader], [PartyLogo], [PartyFlag], [PartyAddress], [PartyDescription] FROM [TblParty]"></asp:SqlDataSource></div> 推荐答案 这篇关于网格视图中的问题从数据库中检索图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 01:58