TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Aditya Bansal
NA
1
1.6k
Error in code while Uploading Image on the Server .. and then retrieving back
Aug 14 2010 6:20 PM
Hi!
Firstly, I'm a Newbie in this and dont know much about it. Still Trying hard to figure out the solution.
I have been trying to Upload an image to the Server and then retrieving the same at the same form.
But my Code is totally messed up.
Kindly help me figuring out this problem.
User.Aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="User.aspx.cs" Inherits="User" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div dir="ltr" style="height: 27px; width: 558px">
tbl_User<br />
<br />
<asp:Label ID="Label11" runat="server" Text="Type"></asp:Label>
<asp:TextBox ID="TextBox_Type" runat="server"></asp:TextBox>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>
<br />
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Password"></asp:Label>
<asp:TextBox
ID="TextBox_Password" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label>
<asp:TextBox ID="Textbox_F_Name" runat="server" Width="157px"></asp:TextBox>
<asp:Label ID="Label12" runat="server" Text="Last Name"></asp:Label>
<asp:TextBox ID="Textbox_L_Name" runat="server" Width="176px"></asp:TextBox>
<p>
<asp:Label ID="Label2" runat="server" Text="Fathers_First_Name"></asp:Label>
<asp:TextBox ID="Textbox_F_F_N" runat="server"></asp:TextBox>
</p>
<asp:Label ID="Label3" runat="server" Text="Fathers_Last_Name"></asp:Label>
<asp:TextBox ID="Textbox_F_L_N" runat="server"></asp:TextBox>
<p>
<asp:Label ID="Label4" runat="server" Text="Mothers_First_Name"></asp:Label>
<asp:TextBox ID="Textbox_M_F_N" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label5" runat="server" Text="Mothers_Last_Name"></asp:Label>
<asp:TextBox ID="Textbox_M_L_N" runat="server"></asp:TextBox>
</p>
<asp:Label ID="Label6" runat="server" Text="Date_Of_Birth"></asp:Label>
<asp:TextBox ID="Textbox_DOB" runat="server"></asp:TextBox>
<p>
<asp:Label ID="Label7" runat="server" Text="PhotoGraph"></asp:Label>
<asp:FileUpload ID="imgUpload" runat="server" />
</p>
<asp:Label ID="Label8" runat="server" Text="Gender"></asp:Label>
<asp:RadioButton ID="Radiobutton1" runat="server"
oncheckedchanged="Button1_Click" Text="Male" />
<asp:RadioButton ID="Radiobutton2" runat="server"
oncheckedchanged="Button1_Click" Text="Female" />
<p>
<asp:Label ID="Label9" runat="server" Text="Relationship_Status"></asp:Label>
<asp:TextBox ID="Textbox_Relationship_Status" runat="server"></asp:TextBox>
</p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
<br />
<br />
<asp:Image ID="Image1" runat="server" Height="113px" Width="196px" />
</form>
</body>
</html>
User.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
public partial class User : System.Web.UI.Page
{
//int count;
protected void Page_Load(object sender, EventArgs e)
{
//count =Convert.ToInt32( Application["id"]);
}
protected void Button1_Click(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["iFormConnectionString"].ConnectionString);
FileUpload img = (FileUpload)imgUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
//To create a PostedFile
HttpPostedFile File = imgUpload.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);
}
connection.Open();
string gender = "";
if (Radiobutton1.Checked)
{
gender = "Male";
}
else if (Radiobutton2.Checked)
{
gender = "Female";
}
string insqr = "INSERT INTO tbl_User ";
insqr += "(User_Type, ";
insqr += "id_Pwd, ";
insqr += "First_Name, ";
insqr += "Last_Name, ";
insqr += "Fathers_First_Name, ";
insqr += "Fathers_Last_Name, ";
insqr += "Mothers_First_Name, ";
insqr += "Mothers_Last_Name, ";
insqr += "Date_Of_Birth, ";
//// insqr += "Photograph, "; Upload Image on the server.
insqr += "Gender, ";
insqr += "Relationship_Status) ";
insqr += "VALUES(";
insqr += "'1', ";
insqr += "'" + TextBox_Password.Text + "', ";
insqr += "'" + Textbox_F_Name.Text + "', ";
insqr += "'" + Textbox_L_Name.Text + "', ";
insqr += "'" + Textbox_F_F_N.Text + "', ";
insqr += "'" + Textbox_F_L_N.Text + "', ";
insqr += "'" + Textbox_M_F_N.Text + "', ";
insqr += "'" + Textbox_M_L_N.Text + "', ";
insqr += "'" + Textbox_DOB.Text + "', ";
insqr += "'" + gender.ToString() + "', ";
insqr += "'" + Textbox_Relationship_Status.Text + "')";
SqlCommand com = new SqlCommand(insqr, connection);
string sql = "INSERT INTO tbl_User(Photograph) VALUES(@imgUpload) SELECT @@IDENTITY";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@imgUpload", imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());
Response.Write(insqr);
Response.Write(sql);
Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
com.ExecuteNonQuery();
connection.Close();
}
}
}
Also,
ShowImage.ashx
handler
<%@ WebHandler Language="C#" Class="ShowImage" %>
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 empno;
if (context.Request.QueryString["id"] != null)
empno = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = ShowEmpImage(empno);
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);
}
public Stream ShowEmpImage(int empno)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["iFormConnectionString"].ConnectionString);
string sql = "SELECT Photograph FROM tbl_User WHERE id_User = @ID";
SqlCommand cmd = new SqlCommand(sql,connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", empno);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
Now the problem is;
I am getting an sqlException in
int id = Convert.ToInt32(cmd.ExecuteScalar());
saying: Cannot insert the value NULL into column 'First_Name', table 'iForm.dbo.tbl_User'; column does not allow nulls. INSERT fails.
The statement has been terminated.
I have been trying to upload the image ... but something is messed up somwhere.. But i dont know where... Kindly help me out guys
Reply
Answers (
0
)
How to print custom size receipt In C# using crystal reports
Rotating Control