0

So I have a fileupload control that I use to upload a picture to my server. I've gotten it to work no problem without the drop down list, but I need the value from the drop down list to specify which record I want to update in a database. Here is the aspx:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
        DataFile="~/App_Data/TravelJoansDB.mdb" 
        SelectCommand="SELECT * FROM [Table2] ORDER BY ID DESC" 
        UpdateCommand="UPDATE Table2 SET (Image) VALUES (@Image) WHERE ID=@ID" >
        <UpdateParameters>
            <asp:Parameter Name="Image" Type="String" />
            <asp:Parameter Name="ID" Type="Int16" />
        </UpdateParameters>
</asp:AccessDataSource>

<asp:Label ID="SelectBlogLabel" runat="server" Text="Select a Blog ID for the photo that you are uploading" /><br />
<asp:DropDownList ID="BlogPostTitleDDL" runat="server" 
                  AutoPostBack="true" 
                  DataSourceID="AccessDataSource1" 
                  DataValueField="ID" /><br />

<asp:FileUpload ID="FileUpload1" runat="server" />

<asp:Button ID="UploadButton" runat="server" OnClick="UploadFile" Text="Upload photo" /><br />
<asp:Label ID="UploadStatusLabel" runat="server" Text="Status: " /><br />

And here is the code behind:

protected void UploadFile(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        try
        {
            if (FileUpload1.PostedFile.ContentType == "image/jpeg")
            {
                if (FileUpload1.PostedFile.ContentLength < 10240000)
                {
                    string filename = Path.GetFileName(FileUpload1.FileName);
                    FileUpload1.SaveAs(Server.MapPath("~/PlaceImages/") + filename);
                    UploadStatusLabel.Text = "Upload status: Complete!";
                    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\TravelJoansDB.mdb;";
                    string cmdstr = "UPDATE Table2 (Image) VALUES (@Image) WHERE ID=@ID";

                    OleDbConnection con = new OleDbConnection(constr);
                    OleDbCommand com = new OleDbCommand(cmdstr, con);

                    int blogID = BlogPostTitleDDL.SelectedIndex;
                    con.Open();
                    com.Parameters.AddWithValue("@Image", filename);
                    com.Parameters.AddWithValue("@ID", blogID);
                    com.ExecuteNonQuery();
                    con.Close();
                }
                else
                    UploadStatusLabel.Text = "Upload status: The file has to be less than 10 MB!";
            }
            else
                UploadStatusLabel.Text = "Upload status: Only JPEG files are accepted!";
        }
        catch (Exception ex)
        {
            UploadStatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

Whenever I try to upload a file, it tells me there is an error in the syntax of the update statement. So I just don't really know how to use the value from the DDL to specify which record in the database I want. Any help would be appreciated.

4

1 回答 1

2

FOR TSQL UPDATE QUERY IS SOMETHING LIKE THIS

UPDATE TABLENAME SET COLUMN1='valuesyouwant' where='whatever'

For your query it should be something like this

"UPDATE Table2 SET Image=@Image WHERE ID=@ID"
于 2013-10-25T02:00:46.427 回答