Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello i have formview inside it i go for insert and grab upload to upload image but evey time i upload i will get only words "True"

so some 1 help plz


this is for insert the image path into textbox

don't judge me iam new at this so can u fix it

What I have tried:

C#
protected void LinkButton1_Click(object sender, EventArgs e)
{
    FileUpload newfileuploadcontrol = (FileUpload)FormView1.FindControl("FileUpload1") as FileUpload;
    TextBox newtextbox = (TextBox)FormView1.FindControl("TextBox10") as TextBox;
    if (newfileuploadcontrol.HasFile)
    {
       newfileuploadcontrol.SaveAs(Server.MapPath("/Cars/Copy_of_Payment_Images/") + newfileuploadcontrol.FileName);
       newtextbox.Text = "/Cars/Copy_of_Payment_Images/" + newfileuploadcontrol.FileName;
    }
 }


And this for insert all the value in same page which pretty same

C#
  protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {

        FileUpload newfileuploadcontrol = (FileUpload)FormView1.FindControl("FileUpload1") as FileUpload;
        TextBox newtextbox = (TextBox)FormView1.FindControl("TextBox10") as TextBox;
        if (newfileuploadcontrol.HasFile)
        {
           newfileuploadcontrol.SaveAs(Server.MapPath("/Cars/Copy_of_Payment_Images/") + newfileuploadcontrol.FileName);
           newtextbox.Text = "/Cars/Copy_of_Payment_Images/" + newfileuploadcontrol.FileName;
        }
}
Posted
Updated 29-Mar-17 11:39am
v2

1 solution

Could you try with below piece of code -

If you are using "AS" operator you dont need to user explicit casting while finding control.

C#
FileUpload newfileuploadcontrol = FormView1.FindControl("FileUpload1") as FileUpload;
TextBox newtextbox = FormView1.FindControl("TextBox10") as TextBox;
           
if (newfileuploadcontrol.HasFile)
{
    string path = Server.MapPath("/Cars/Copy_of_Payment_Images/") + newfileuploadcontrol.FileName;
    newfileuploadcontrol.SaveAs(path);
    
    newtextbox.Text = path;
}
 
Share this answer
 
Comments
Member 13044689 30-Mar-17 2:20am    
its not work keep getting "true" value i want the url of image
Member 13044689 30-Mar-17 2:35am    
its work sir now u r life savior thx
Member 13044689 30-Mar-17 3:57am    
SIR SRY FOR BOTHERING i want to add two image in form view i did this but still upload only one


protected void LinkButton4_Click(object sender, EventArgs e)
{


FileUpload newfileuploadcontrol = FormView1.FindControl("FileUpload4") as FileUpload;
TextBox newtextbox = FormView1.FindControl("TextBox10") as TextBox;

if (newfileuploadcontrol.HasFile)
{
string path = Server.MapPath("/Cars/Copy_of_Payment_Images/") + newfileuploadcontrol.FileName;
newfileuploadcontrol.SaveAs(path);

newtextbox.Text = path;
}
else
{

FileUpload newfileuploadcontrol1 = FormView1.FindControl("FileUpload3") as FileUpload;
TextBox newtextbox1 = FormView1.FindControl("TextBox26") as TextBox;

if(newfileuploadcontrol.HasFile&&newfileuploadcontrol1.HasFile)
{

string path = Server.MapPath("/Cars/Copy_of_Payment_Images/") + newfileuploadcontrol.FileName;
newfileuploadcontrol.SaveAs(path);
string path1 = Server.MapPath("/Cars/Car_Image/") + newfileuploadcontrol1.FileName;
newfileuploadcontrol.SaveAs(path);
newtextbox.Text = path;
newtextbox1.Text = path1;



}
[no name] 30-Mar-17 9:03am    
Its because you are uploading same file 2 times with below stmt-

newfileuploadcontrol.SaveAs(path);

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