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.
This is the Code For Inserting Images into the Database.
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)
{
HttpPostedFile File = PartyimgUpload.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
if (img2.HasFile && img2.PostedFile != null)
{
HttpPostedFile File1 = PartyFlagimgUpload.PostedFile;
imgByte2 = new Byte[File1.ContentLength];
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());
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)
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT PartyId FROM TblParty WHERE PartyName = @PartyName)
BEGIN
SELECT -1
END
ELSE
BEGIN
INSERT INTO TblParty (PartyName,PartyLeader,PartyLogo,PartyFlag,PartyAddress,PartyDescription) VALUES(@PartyName,@PartyLeader,@PartyLogo,@PartyFlag,@PartyAddress,@PartyDescription) SELECT @@IDENTITY
SELECT SCOPE_IDENTITY()
END
END
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>