我有一个表单,管理员可以将公司添加到名为“公司”的数据库表中
在此表格中,有一个下拉列表可供选择公司是否为奖学金公司,有"is"和“否”两个选项。我创建了一个表来从名为“YesNo”的表中提取这些选项。
当我在下拉列表中选择"is"并点击“提交”时,它会更新表格,因此奖学金公司的单元格为“1”而不是"is"。这在我的编辑表单中工作得很好,它只更新了公司,尽管我在添加公司表单时遇到了麻烦,该表单会在表中插入一个新条目。
请参阅下面的代码;
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")
最佳答案
而不是
替换((ddlLocation.SelectedIndex + 1), "'", "''") & "', "& _
"'"& Replace((ddlStatus.SelectedIndex + 1), "'", "''") & "', "& _
尝试
替换((ddlLocation.SelectedValue + 1), "'", "''") & "', "& _
"'"& Replace((ddlStatus.SelectedValue + 1), "'", "''") & "', "& _
或选定文本
关于mysql - 如何将 DropDownList 中的正确值插入到数据库表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29703206/