Ok thx, that works fine if the user uploads a file,
But I also wan't it to work if the user dousn't upload a file.
Then the text that the person writes should just be put into the database.
My code looks like this:
aspx page:
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/admin/MasterPage.master" AutoEventWireup="true" CodeFile="skriv_beskrivelse_tekniker.aspx.cs" Inherits="admin_skriv_beskrivelse_tekniker" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Titel:<br /><textarea id="titel" cols="35" runat="server" rows="7"></textarea><br /><br />
indsæt billede:<br /><asp:FileUpload ID="fu" runat="server"/><br />
<asp:Button ID="opret" runat="server" Text="opret beskrivelse" OnClick="opret_Click"/>
<asp:Label ID="lblOutput" runat="server"></asp:Label><br />
</asp:Content>
cs page:
Code:
protected void opret_Click(object sender, EventArgs e)
{
SqlCommand objcmd = new SqlCommand();
objcmd.CommandType = CommandType.Text;
objcmd.Connection = objconn;
objcmd.CommandText = "UPDATE TEKNIKKERE SET beskrivelse = @beskrivelse, img_url = @img_url WHERE id = @id";
objcmd.Parameters.AddWithValue("@beskrivelse", (titel.Value.Replace(Environment.NewLine, "<br />")));
objcmd.Parameters.AddWithValue("@img_url", fu.FileName);
objcmd.Parameters.AddWithValue("id", Request.QueryString["id"]);
objconn.Open();
objcmd.ExecuteNonQuery();
objconn.Close();
string fn = System.IO.Path.GetFileName(fu.PostedFile.FileName);
fu.SaveAs(Server.MapPath("../admin/images/") + fn);
}
}
now if the user only types text in the textbox an error page comes up with this error:
Could not find a part of the path 'D:\hshome\kischi2\radio.web.surftown.dk\admin\ima ges\'.
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 'D:\hshome\kischi2\radio.web.surftown.dk\admin\ima ges\'.
Source Error:
Line 47: string fn = System.IO.Path.GetFileName(fu.PostedFile.FileName) ;
Line 48:
Line 49: fu.SaveAs(Server.MapPath("../admin/images/") + fn);
Line 50:
Line 51:
I assume it is because it is looking for a file to put into the folder "images" but there is no file.
But I am not sure.
I hope you understand what I mean and can help me?
Thanks
|