简体   繁体   中英

How do I select text from one field that is actually text from one table, and insert the corresponding number into another table?

I have a drop down list that I populate from one table that I need to use the selected value in the drop down list to insert into another field in another table. The field that I use to populate the DDL is text and table it pulls from has a numeric PK. How do I insert that PK as an FK in the other table? Here is the code:

Markup:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/webvideos.mdb" 
    DeleteCommand="DELETE FROM [Docs] WHERE [ID] = ?" 
    InsertCommand="INSERT INTO [Docs] ([Filename], [Label], [Section]) VALUES (?, ?, ?)" 
    SelectCommand="SELECT * FROM [Docs]" 
    UpdateCommand="UPDATE [Docs] SET [Filename] = ?, [Label] = ?, [Section] = ? WHERE [ID] = ?">
    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Filename" Type="String" />
        <asp:Parameter Name="Label" Type="String" />
        <asp:Parameter Name="Section" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Filename" Type="String" />
        <asp:Parameter Name="Label" Type="String" />
        <asp:Parameter Name="Section" Type="Int32" />
        <asp:Parameter Name="ID" Type="Int32" />
    </UpdateParameters>
</asp:AccessDataSource>
<asp:AccessDataSource ID="AccessDataSource2" runat="server" 
    DataFile="~/App_Data/webvideos.mdb" SelectCommand="SELECT * FROM [Sections]">
</asp:AccessDataSource>
<h2>Add a document</h2><br />
<asp:FormView ID="Formview1" runat="server" DataKeyNames="ID" 
    DataSourceID="AccessDataSource1" DefaultMode="Insert">

    <InsertItemTemplate>
        Label:
        <asp:TextBox ID="LabelTextBox" runat="server" />
        <br />
        Section:
        <asp:DropDownList ID="ddlSection" runat="server" 
        DataSourceID="AccessDatasource2" DataTextField="Sections" DataValueField="Sections" />
        <br /><br />
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:Button ID="UploadButton" runat="server" Text="Upload document" OnClick="UploadFile" /><br />
        <asp:Label ID="UploadStatusLabel" runat="server" Text="Upload Status: " />
    </InsertItemTemplate>

</asp:FormView>

Code-behind:

protected void UploadFile(object sender, EventArgs e)
{
    TextBox txtDocLabelText = (TextBox)Formview1.FindControl("LabelTextBox");
    DropDownList ddlSection = (DropDownList)Formview1.FindControl("ddlSection");
    FileUpload FileUpload1 = (FileUpload)Formview1.FindControl("FileUpload1");
    Label UploadStatusLabel = (Label)Formview1.FindControl("UploadStatusLabel");

    if (FileUpload1.HasFile)
    {
        try
        {
            if (FileUpload1.PostedFile.ContentType == "application/doc" ||
                FileUpload1.PostedFile.ContentType == "appl/text" ||
                FileUpload1.PostedFile.ContentType == "application/vnd.msword" ||
                FileUpload1.PostedFile.ContentType == "application/vnd.ms-word" ||
                FileUpload1.PostedFile.ContentType == "application/winword" ||
                FileUpload1.PostedFile.ContentType == "application/word" ||
                FileUpload1.PostedFile.ContentType == "application/msword" ||
                FileUpload1.PostedFile.ContentType == "application/x-msw6" ||
                FileUpload1.PostedFile.ContentType == "application/x-msword" ||
                FileUpload1.PostedFile.ContentType == "application/pdf" ||
                FileUpload1.PostedFile.ContentType == "application/x-pdf" ||
                FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
                FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.template"
                )
            {
                if (FileUpload1.PostedFile.ContentLength < 102400000)
                {
                    string filename = Path.GetFileName(FileUpload1.FileName);
                    string section = ddlSection.SelectedValue;
                    string label = txtDocLabelText.Text;
                    FileUpload1.SaveAs(Server.MapPath("~/docs/HRDocs") + filename);
                    UploadStatusLabel.Text = "Upload status: Complete!";
                    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
                    string cmdstr = "INSERT INTO [Docs] ([Filename], [Label], [Section]) VALUES (?, ?, ?)";

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

                    con.Open();
                    com.Parameters.AddWithValue("@Filename", filename);
                    com.Parameters.AddWithValue("@Label", label);
                    com.Parameters.AddWithValue("@Section", section);
                    com.ExecuteNonQuery();
                    con.Close();
                    Response.Redirect("ManageHRDocs.aspx");
                }
                else
                    UploadStatusLabel.Text = "Upload status: The file has to be less than 100 MB!";
            }
            else
                UploadStatusLabel.Text = "Upload status: Not an accepted file type";
        }
        catch (Exception ex)
        {
            UploadStatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.ToString();
        }
    }

What is the best way to do this? I should also add that the code as it is currently returns a data mismatch because what is selected for the drop down list is text and the "section" field in the Docs table is a number.

I did it by adding a switch statment, as follows:

switch (section)
    {
        case "Benefits - Open Enrollment":
            sectionNumber = 1;
            break;
        case "Benefits - Summaries":
            sectionNumber = 2;
            break;
        case "Benefits - 401(k)":
            sectionNumber = 3;
            break;
        case "Benefits - HSA/FSA":
            sectionNumber = 4;
            break;
        case "Forms - Tax/Payroll":
            sectionNumber = 5;
            break;
    }

That was quite fun. I'm still open to other answers as well! And since it is a drop down list that I'm doing this for, I didn't bother with a default case because it is not possible to select a blank or null value.

Try setting the DataValueField to the primary key retrieved through the query used by your AccessDataSource that populates the dropdownlist. The DataTextField can stay the same.

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