繁体   English   中英

如何从一个表中选择实际为文本的字段中的文本,并将相应的数字插入另一个表中?

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

我有一个下拉列表,我从一个表填充,我需要使用下拉列表中的选定值插入另一个表中的另一个字段。 我用来填充DDL的字段是它从中提取的文本和表具有数字PK。 如何在另一个表中将该PK作为FK插入? 这是代码:

标记:

<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>

代码隐藏:

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();
        }
    }

做这个的最好方式是什么? 我还应该补充一点,因为它当前返回的数据不匹配,因为为下拉列表选择的是文本,而Docs表中的“section”字段是一个数字。

我通过添加开关语句来完成它,如下所示:

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;
    }

这很有趣。 我仍然对其他答案持开放态度! 因为它是我正在执行此操作的下拉列表,所以我没有打扰默认情况,因为无法选择空值或空值。

尝试将DataValueField设置为通过填充下拉列表的AccessDataSource使用的查询检索的主键。 DataTextField可以保持不变。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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