Click here to Skip to main content
15,884,836 members

image is not uploading

fak_farrukh asked:

Open original thread
using this code i want to upload image and show on page_load..
but its doing nothing please help


C#
public void BindGridData()

        {
            string username=(string)(Session["UserAuthentication"]);
            SqlConnection con = new SqlConnection(connstring);
            SqlCommand cmd = new SqlCommand("SELECT imgname from [Image] WHERE imgname='" + username + "'", con);
            SqlDataReader sdr;
            con.Open();
            sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                Image1.ImageUrl = sdr[0].ToString();
            }
            con.Close();
        }
protected void Button2_Click1(object sender, EventArgs e)
        {

if (FileUpload1.HasFile)
{

int length = FileUpload1.PostedFile.ContentLength;

byte[] imgbyte = new byte[length];

HttpPostedFile img = FileUpload1.PostedFile;

img.InputStream.Read(imgbyte, 0, length);
string username=(string)(Session["UserAuthentication"]);

SqlConnection connection = new SqlConnection(connstring);
connection.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Image (imgname,Image) VALUES (@imgname,@imagedata)", connection);
cmd.Parameters.Add("@imgname", SqlDbType.VarChar, 50).Value = username;
cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;
int count = cmd.ExecuteNonQuery();
connection.Close();
if (count == 1)
{
BindGridData();

ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + username + " image inserted successfully')", true);
}
}
 public class ImageHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            
            string imageid = context.Request.QueryString["ID"];
            SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\VAS\Desktop\Orignal Project Part 1\Inventory Management System\IMS.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            conn.Open();
            SqlCommand command = new SqlCommand("select Image from Image where imgname=" + imageid, conn);
            SqlDataReader dr = command.ExecuteReader();
            dr.Read();
            context.Response.BinaryWrite((Byte[])dr[0]);
            conn.Close();
            context.Response.End();
}


ASP.NET
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?ID="+ Eval("imagename") %>'  Height="267px" 
          Width="768px" />
Tags: ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900