Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
if (Convert .ToByte (FileUpload2.PostedFile.ContentLength )> 0)
            {
               int imgsize = FileUpload2.PostedFile.ContentLength;

                int imagesize = imgsize / 1024;
                if (imagesize < 50)
                {
                    string fn = System.IO.Path.GetFileName(FileUpload2.PostedFile.FileName);
                    string[] fname1 = fn.Split(',');
                    int i = fname1.Length;
                    string fext = fname1[i - 1];
                    if (fext == "jpg")
                    {
                        string serverpath = Server.MapPath("./employeephoto/");
                        string filelocation = serverpath + "\\" + f[0] + "."+ fext;
                        //string filelocation = serverpath +"\\" + f[0] + ";."+ fext;  /** Path at server to save file**/
                        //string path = "./photo" + "\\"+ fn;   /** Path to enter in database **/
                        FileUpload2.PostedFile.SaveAs(filelocation);
                        /*Code to insert values into the table of spareparts*/



                        Label2.Text = "The Image has been uploaded successfully";
                    }
Posted
Updated 13-Sep-12 20:44pm
v2
Comments
Sergey Alexandrovich Kryukov 14-Sep-12 2:34am    
"Not working" is not descriptive. This is not even a question.
--SA
sakshi1111 14-Sep-12 2:42am    
protected void Button3_Click(object sender, EventArgs e)
{

lblerror.Text = null;
Lblimg.Text = "";

string b = TextBox2.Text.ToString();
string[] f = b.Split('.');

string d1 = TextBox4.Value.ToString();

char[] a = new char[10];
a = d1.ToCharArray();
//d1.CopyTo(0, a, 0, 10);
string d2 = "" + a[3] + a[4] + a[2] + a[0] + a[1] + a[5] + a[6] + a[7] + a[8] + a[9] + "";
DateTime date1 = DateTime.Parse(d2);

string d3 = TextBox5.Value.ToString();

char[] bb = new char[10];
bb = d3.ToCharArray();
//d1.CopyTo(0, a, 0, 10);
string d4 = "" + a[3] + a[4] + a[2] + a[0] + a[1] + a[5] + a[6] + a[7] + a[8] + a[9] + "";
DateTime date2 = DateTime.Parse(d4);


string edu = DropDownList2.SelectedValue.ToString();
string edu1 = educ(edu);
string des6 = DropDownList4.SelectedValue.ToString();
string des7 = desig(des6);



con.Open();


string Sql = "update employee set Gender = '" + emp_sex.Value + "',Fname='" + fname.Value + "', DOB='" + date1.Date + "', BloodGroup='" + blood_group.Value + "',Education = '" + edu1 + "',Email = '" + admission_no.Value + "',ContactNo1 = '" + TextBox6.Text + "',ContactNo2 = '" + TextBox7.Text + "',Address = '" + current_address.Value + "',Designation = '" + des7 + "',JoiningDate = '" + date2.Date + "',Married='" + DropDownList5.SelectedValue.ToString() + "',Children='" + TextBox1.Text + "' where EmpID='" + f[0] + "'";

cmd = new SqlCommand(Sql, con);

int Result = cmd.ExecuteNonQuery();
con.Close();

if (Result > 0)
{
if (Convert .ToByte (FileUpload2.PostedFile.ContentLength )> 0)
{
int imgsize = FileUpload2.PostedFile.ContentLength;

int imagesize = imgsize / 1024;
if (imagesize < 50)
{
string fn = System.IO.Path.GetFileName(FileUpload2.PostedFile.FileName);
string[] fname1 = fn.Split(',');
int i = fname1.Length;
string fext = fname1[i - 1];
if (fext == "jpg")
{
string serverpath = Server.MapPath("./employeephoto/");
string filelocation = serverpath + "\\" + f[0] + "."+ fext;
//string filelocation = serverpath +"\\" + f[0] + ";."+ fext; /** Path at server to save file**/
//string path = "./photo" + "\\"+ fn; /** Path to enter in database **/
FileUpload2.PostedFile.SaveAs(filelocation);
/*Code to insert values into the table of spareparts*/



Label2.Text = "The Image has been uploaded successfully";
}
lblerror.Text = "Record updated successfully"



this is the button code....
Sergey Alexandrovich Kryukov 14-Sep-12 3:07am    
Do you think more code makes the post a question? :-)
--SA
Sandeep Mewara 14-Sep-12 2:35am    
Elaborate! It's a simple update query... what is the issue that you face?
sakshi1111 14-Sep-12 2:39am    
image not be updated....
i got the error on this line like...
if (Convert .ToByte (FileUpload2.PostedFile.ContentLength )> 0)
error--Object reference not set to an instance of an object..

if (FileUpload2.HasFile)
{
int length = FileUpload2.PostedFile.ContentLength;
byte[] imgbyte = new byte[length];
if (length> 0)
{
//int imgsize = FileUpload2.PostedFile.ContentLength;

int imagesize = length / 1024;
if (imagesize< 50)

C#
{
string fn = System.IO.Path.GetFileName(FileUpload2.PostedFile.FileName);
string[] fname1 = fn.Split(',');
int i = fname1.Length;
string fext = fname1[i - 1];
if (fext == "jpg")
{
string serverpath = Server.MapPath("./employeephoto/");
string filelocation = serverpath + "\\" + f[0] + "."+ fext;
//string filelocation = serverpath +"\\" + f[0] + ";."+ fext; /** Path at server to save file**/
//string path = "./photo" + "\\"+ fn; /** Path to enter in database **/
FileUpload2.PostedFile.SaveAs(filelocation);
/*Code to insert values into the table of spareparts*/



Label2.Text = "The Image has been uploaded successfully";
}
  lblerror.Text = "Record updated successfully"
 
Share this answer
 
2 things:
1. Make sure you have uploaded a new file for upload. If you think, older one path file still exists, you are mistaken. So, if user has uploaded a new file, you will have access to it for upload.
2. Make sure you check for 'HasFile' property in fileupload control. Checking for file and NULL values are helpful.

Once you have the file, it would be simple update statement. If the file is not browsed and uploaded by user, you wont be able to update it.
 
Share this answer
 
Comments
sakshi1111 14-Sep-12 3:18am    
Thanks 4 ur reply....
I hv checked both the conditions but got the same error...
Monday is my deadline to complete this project plz help me....
C#
if (FileUpload2.HasFile)
            {
                if (FileUpload2.FileName.Split(new char[] { '.' })[1].ToLower() == "jpg")
                {
                    FileUpload2.PostedFile.SaveAs(Server.MapPath(" ") + "\\employeephoto\\" + FileUpload2.FileName);
                }
            }
 
Share this answer
 
Comments
sakshi1111 14-Sep-12 3:29am    
Thanks 4 Reply.........
But its not working........
CoderVivs 14-Sep-12 3:55am    
In your code why you are converting it to byte if you want to upload the image to a directory, use FileUpload2.PostedFile.SaveAs(Server.MapPath(" ") + "\\employeephoto\\" + FileUpload2.FileName);

and see the design are you using updatepanels ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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