简体   繁体   中英

How to pass the file name of an uploaded file into a SQL Value in ASP.NET C#?

I need help figuring out how to pass the name of an uploaded file into a SQL database at the time it's uploaded; so far I've only been able to get the uploader to work.

Since I'm building this project in Visual Web Developer 2008, the Sql Data Source and connection string are established in the page's code:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ARW-KidsConnectionString1 %>" 
    DeleteCommand=" ... " 
    InsertCommand="INSERT INTO [Kids] ( ... , [Picture],  ...) VALUES ( ... , @Picture,  ...)" 
    ProviderName="<%$ ConnectionStrings:ARW-KidsConnectionString1.ProviderName %>" 
    SelectCommand=" ... " 
    UpdateCommand=" ... ">

I've redacted the snippet because it's the Insert command that this focuses on. The "Picture" column in the table "Kids" is supposed to contain the name of the uploaded picture of a given child, and this is important since we do have a placeholder pic that get's used when no picture is available.

Here's the code from the form where the image is uploaded; it's part of a DetailsView which contains the form that gathers all the information for a new listing.

<InsertItemTemplate>
                Upload Image:<br />
                <asp:FileUpload ID="picUpload" runat="server" />
                <asp:CustomValidator ID="imageCheck" runat="server" ErrorMessage="Upload Image Files Only" CssClass="ErrorMsg"></asp:CustomValidator>
            </InsertItemTemplate>

And here's the complete method from the code behind that handles the uploading of the picture. You'll notice that I've got it firing on the ItemInserting event of the DetailsView.

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
    FileUpload pu = (FileUpload)DetailsView1.FindControl("picUpload");
    string virtualFolder = "~/WisconsinKids/Kid_Images/";
    string physicalFolder = Server.MapPath(virtualFolder);
    string fileName = "nopic.jpg";

    if (!pu.HasFile)
    {
    }

    if (pu.HasFile)
    {
        if (pu.FileName.ToLower().EndsWith(".jpg") ||
            pu.FileName.ToLower().EndsWith(".jpeg") ||
            pu.FileName.ToLower().EndsWith(".gif") ||
            pu.FileName.ToLower().EndsWith(".png") ||
            pu.FileName.ToLower().EndsWith(".bmp"))
        {
            fileName = pu.FileName.ToString();
            pu.SaveAs(System.IO.Path.Combine(physicalFolder, fileName));
        }

        else
        {
            CustomValidator imageCheck = (CustomValidator)DetailsView1.FindControl("imageCheck");
            imageCheck.IsValid = false;
            e.Cancel = true;
        }
    }
}

What I want to do is push that string "fileName" into the Picture record of table Kids. Since I've already got the datasource connection established on the front-end, my gut tells me that I shouldn't have to do it again in the code-behind, but after three days of searching I'm out of ideas.

Thanks in advance for any help given. If you need more information, please let me know and I'll try to clarify, just bear in mind that I'm new to this.

Try:

e.Values["Picture"] = pu.FileName;

There's an example here . According to that, the above should be all you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM