如何将DropDownList中的正确值插入数据库表

时间:2015-04-17 15:18:26

标签: mysql asp.net vb.net

我有一个表单,管理员可以将公司添加到名为“公司”的数据库表中

在此表单中有一个下拉列表,用于选择公司是否为奖学金公司,有两个选项“是”和“否”。我已经创建了一个表来从称为“YesNo”中提取这些选项。

当我在下拉列表中选择是并点击提交时,它会更新表格,因此ScholarshipCompany的单元格为'1'而不是'是'。虽然我在添加公司表单时遇到了麻烦,但是我在编辑表单中工作正常,但是我只是更新了公司表格。这个表格会在表格中插入一个新条目。

请参阅以下代码;

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Session("Name") = Nothing Then
        Response.Redirect("Login.aspx")
    End If
    Session("Name") = Session("Name")
    Session("Username") = Session("Username")

    Try

        If Not IsPostBack Then

            Dim locationList As OleDbDataReader = Database.DoSQLReturnDataReader("SELECT [{Location}].ID, [{Location}].Location FROM [{Location}] ORDER BY [{Location}].ID")
            ddlLocation.DataSource = locationList
            ddlLocation.DataTextField = "Location"
            ddlLocation.DataValueField = "ID"
            ddlLocation.DataBind()
            locationList.close()

            Dim statusList As OleDbDataReader = Database.DoSQLReturnDataReader("SELECT *  FROM EmployerStatus  ORDER BY ID ASC")
            ddlStatus.DataSource = statusList
            ddlStatus.DataTextField = "Status"
            ddlStatus.DataValueField = "ID"
            ddlStatus.DataBind()
            statusList.close()

            Dim yesnoList As OleDbDataReader = Database.DoSQLReturnDataReader("SELECT * FROM YesNo ORDER BY ID DESC")
            ddlYesno.DataSource = yesnoList
            ddlYesno.DataTextField = "Option"
            ddlYesno.DataValueField = "Option"
            ddlYesno.DataBind()
            yesnoList.close()

        End If

    Catch ex As Exception
        Response.Redirect("index.aspx?Yr=" & Replace(Request.QueryString("Yr"), "'", "''"))
    End Try
End Sub


Protected Sub BtnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click

    Dim strSql As String = "INSERT INTO Company(" & _
    "CompanyName, " & _
    "URL, " & _
    "Location, " & _
    "EmployerStatus, " & _
    "Address, " & _
    "ScholarshipCompany, " & _
    "ScholarshipNotes, " & _
    "Town, " & _
    "County, " & _
    "Blurb, " & _
    "InterviewTips, " & _
    "HRContactForename, " & _
    "HRContactSurname, " & _
    "HREmail, " & _
    "Telephone, " & _
    "[Password], " & _
    "Fax, " & _
    "HRContactForename2, " & _
    "HRContactSurname2, " & _
    "HREmail2, " & _
    "Telephone2, " & _
    "HRContactForename3, " & _
    "HRContactSurname3, " & _
    "HREmail3, " & _
    "Telephone3, " & _
    "Postcode) " & _
    "VALUES " & _
    "( " & _
    "'" & TxtBoxCompanyName.Text.Replace("'", "''") & "', " & _
    "'" & TxtBoxURL.Text.Replace("'", "''") & "', " & _
    "'" & Replace((ddlLocation.SelectedIndex + 1), "'", "''") & "', " & _
    "'" & Replace((ddlStatus.SelectedIndex + 1), "'", "''") & "', " & _
    "'" & TxtAreaAddress.InnerText.Replace("'", "''") & "', " & _
    "'" & Replace((ddlYesno.SelectedIndex), "'", "''") & "', " & _
    "'" & TxtBoxSchol.Text.Replace("'", "''") & "', " & _
    "'" & TxtBoxTown.Text.Replace("'", "''") & "', " & _
    "'" & TxtBoxCounty.Text.Replace("'", "''") & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & "" & "', " & _
    "'" & TxtBoxPostcode.Text.Replace("'", "''") & "'" & _
    ")"

    Dim updateCompanyDetails As OleDbDataReader = Database.DoSQLReturnDataReader(strSql)
    updateCompanyDetails.Close()
    System.Threading.Thread.Sleep("1000")

1 个答案:

答案 0 :(得分:0)

而不是

Replace((ddlLocation.SelectedIndex + 1), "'", "''") & "', " & _ "'" & Replace((ddlStatus.SelectedIndex + 1), "'", "''") & "', " & _

尝试

Replace((ddlLocation.SelectedValue + 1), "'", "''") & "', " & _ "'" & Replace((ddlStatus.SelectedValue + 1), "'", "''") & "', " & _

或SelectedText