Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a prd i want to do update database but when i want only update name then image give error please help


C#
string f = Path.GetFileName(FileUpload2.PostedFile.FileName);
        FileUpload2.SaveAs(Server.MapPath("images/" + f));
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
        con.Open();
        string str = "update ImagesPath set Name='" + TextBox4.Text + "',Link='" + TextBox5.Text + "',ImagePath='" + "images/" + f + "',Status='"+DropDownList2.SelectedItem.Text+"' where ID='" + DropDownList1.SelectedItem.Text + "'";
        cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }




i find this error

Could not find a part of the path 'E:\visual work\office work\manish\Eat advise\images\'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'E:\visual work\office work\manish\Eat advise\images\'.

Source Error:


Line 113:
Line 114: string f = Path.GetFileName(FileUpload2.PostedFile.FileName);
Line 115: FileUpload2.SaveAs(Server.MapPath("images/" + f));
Line 116: SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
Line 117: con.Open();
Posted

you are also updating ImagePath and other columns with name column.All will get effected and if you are not selecting any image while updating then the following query gives error.

C#
string str = "update ImagesPath set Name='" + TextBox4.Text + "',Link='" + TextBox5.Text + "',ImagePath='" + "images/" + f + "',Status='"+DropDownList2.SelectedItem.Text+"' where ID='" + DropDownList1.SelectedItem.Text + "'";


So better maintain a flag that will specify either you are doing insert or update..


Thanks
 
Share this answer
 
Comments
M@anish 21-Feb-13 5:04am    
but sir now i want if image feld id blank then other column is also updated actually i donot know where i put if else cundition in this code please help me
M@anish 21-Feb-13 5:06am    
can i use like this
if (FileUpload2.HasFile)
{
string f = Path.GetFileName(FileUpload2.PostedFile.FileName);
FileUpload2.SaveAs(Server.MapPath("images/" + f));
//file.SaveAs(path);
}
else
{
//string f = Path.GetFileName(FileUpload2.PostedFile.FileName);
//FileUpload2.SaveAs(Server.MapPath("images/" + f));
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
con.Open();
string str = "update ImagesPath set Name='" + TextBox4.Text + "',Link='" + TextBox5.Text + "',ImagePath='" + "images/" + f + "',Status='" + DropDownList2.SelectedItem.Text + "' where ID='" + DropDownList1.SelectedItem.Text + "'";
cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
}
C#
protected void Button3_Click(object sender, EventArgs e)
    {
        filldata();

        if (FileUpload2.HasFile)
        {
            string f = Path.GetFileName(FileUpload2.PostedFile.FileName);
            FileUpload2.SaveAs(Server.MapPath("images/" + f));
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
            con.Open();
            string str = "update ImagesPath set ImagePath='" + "images/" + f + "' where ID='" + DropDownList1.SelectedItem.Text + "'";
            cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();

        }
        else
        {
            //string f = Path.GetFileName(FileUpload2.PostedFile.FileName);
            //FileUpload2.SaveAs(Server.MapPath("images/" + f));
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
            con.Open();
            string str = "update ImagesPath set Name='" + TextBox4.Text + "',Link='" + TextBox5.Text + "',Status='" + DropDownList2.SelectedItem.Text + "' where ID='" + DropDownList1.SelectedItem.Text + "'";
            cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
 
Share this answer
 
v2
Comments
AshishChaudha 21-Feb-13 23:48pm    
yes you can do like this...
The exception you get is quite clear:
either you do not have specified directory (images) created
or you have created directory but application does not have permissions to access it and save file.

Create the directory and grant write permission to application.
 
Share this answer
 

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