当点击提交按钮时,在我的应用程序页面上,来自上述表单的信息将被存储到数据库中。运行程序时出现此错误,但如果我要使用断点并复制 SQLtext 字符串,则将此代码粘贴到数据库管理器上的新查询中,它可以工作。错误总是显示以下内容,但“速度”根据 ddlDrugs 中的值而有所不同(例如,如果我们选择酒精,错误会显示从字符串“酒精”到类型“整数”的转换无效):
Conversion from string "Speed" to type 'Integer' is not valid.
有谁知道为什么会这样?ddlDrugs 是一个下拉列表,其中使用 ddlDrugType 更改时触发的回发填充。下面首先列出的是点击提交按钮时的 sub,然后是绑定下拉列表的函数。
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
Dim objGetConnInfo As New ConnInfo
Dim strConn As String
strConn = objGetConnInfo.GetConnString
Dim objConn As SqlConnection
objConn = New SqlConnection(strConn)
Dim myCmd As SqlCommand
myCmd = New SqlCommand
myCmd.Connection = objConn
myCmd.CommandType = CommandType.Text
Dim ds As SqlClient.SqlDataReader
objConn.Open()
Dim fName As String = txtfName.Text
Dim lName As String = txtlName.Text
Dim DOB As String = ddlDOBmonth.SelectedValue & "/" & ddlDOBday.SelectedValue & "/" & ddlDOByear.SelectedValue
Dim sexChoice As Integer = rdoSex.SelectedIndex
Dim sex As String
If sexChoice = 0 Then
sex = "m"
ElseIf sexChoice = 1 Then
sex = "f"
End If
Dim SSN As String = txtSSN.Text
Dim address As String = txtAddress.Text
Dim city As String = txtCity.Text
Dim state As String = ddlState.SelectedValue
Dim zip As String = txtZip.Text
Dim phone As String = txtPhone1.Text
Dim cellPhone As String = txtPhone2.Text
Dim altPhone As String = txtPhone3.Text
Dim EC As String = txtECname.Text
Dim relationshipIndex As Integer = ddlECrelationship.SelectedValue
myCmd.CommandText = "Select Name from Relationships where ID = " & relationshipIndex
Dim relationship As String = myCmd.ExecuteScalar
Dim ECphone As String = txtECphone.Text
Dim findRRindex As Integer = ddlHowRR.SelectedValue
myCmd.CommandText = "Select Name from foundRR where ID =" & findRRindex
Dim findRR As String = myCmd.ExecuteScalar
Dim rush As String
If chkRush.Checked = True Then
rush = "1"
Else
rush = "0"
End If
Dim reason As String = txtReason.Text
Dim drugType As String = ddlDrugType.SelectedValue
Dim drugIndex As Integer = ddlDrug.SelectedValue
myCmd.CommandText = "Select Drug from " & drugType & " where Id = " & drugIndex
Dim drugObj As Object = myCmd.ExecuteScalar
Dim drug As String = ddlDrug.SelectedItem.Text
Dim previous As String = txtPrevTreatment.Text
Dim dateCompleted As String = ddlDOCmonth.SelectedValue & "/" & ddlDOCdate.SelectedValue & "/" & ddlDOCyear.SelectedValue
Dim treatmentRep As String = txtTreatmentRep.Text
Dim test As String = ddlDrug.SelectedValue
' insert into Clients values ('mike','abramowitz','9/12/1986', 'm', 187664309, '2132 mather way', 'elkins park', 'pa', 19027, '2222222222','3333', '4', 'dad', 'mom', '55', 'magic', 0, 'need it', 'drug type', 'pot', 'none', '9/12/1988', 'tim')
Dim SQLtext As String = "exec insertClient '" & fName & "','" & lName & "','" & DOB & "', '" & sex & "'," & SSN & ", '" & address & "', '" & city & "', '" & state & "', " & zip & ", '" & phone & "','" & cellPhone & "', '" & altPhone & "', '" & EC & "', '" & relationship & "', '" & ECphone & "', '" & findRR & "'," & rush & ", '" & reason & "', ' " & drugType & "','" & drug & "', '" & previous & "', ' " & dateCompleted & "', '" & treatmentRep & "')"
Dim clientID As Integer = myCmd.ExecuteScalar
objConn.Close()
myCmd.Dispose()
End Sub
在回发时绑定下拉列表
Protected Sub ddlDrugType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim objGetConnInfo As New ConnInfo
Dim strConn As String
strConn = objGetConnInfo.GetConnString
Dim objConn As SqlConnection
objConn = New SqlConnection(strConn)
Dim myCmd As SqlCommand
myCmd = New SqlCommand
myCmd.Connection = objConn
myCmd.CommandType = CommandType.Text
Dim ds As SqlClient.SqlDataReader
myCmd.CommandText = "Select Id, Drug from " & ddlDrugType.SelectedValue
objConn.Open()
ds = myCmd.ExecuteReader
ddlDrug.DataSource = ds
ddlDrug.DataTextField = "Drug"
ddlDrug.DataValueField = "Id"
ddlDrug.DataBind()
ds.Close()
objConn.Close()
myCmd.Dispose()
End Sub
这是详细的错误
System.InvalidCastException was unhandled by user code
HResult=-2147467262 Message=从字符串“Speed”到类型“Integer”的转换无效。Source=Microsoft.VisualBasic StackTrace:Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(Object Value) at RR.NewCustomer.btnSubmit_Click(Object sender, EventArgs e) in C :\Users\Administrator\Documents\Visual Studio 2010\Projects\RR\RR\NewCustomer.aspx.vb:System.Web.UI.WebControls.Button.OnClick(EventArgs e) 处 System.Web.UI.WebControls 处的第 276 行.Button.RaisePostBackEvent(String eventArgument) 在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException: System.FormatException HResult=-2146233033 Message=输入字符串不在正确的格式。Source=Microsoft.VisualBasic StackTrace: 在 Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) 在 Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) InnerException: Boolean includeStagesAfterAsyncPoint) InnerException: System.FormatException HResult=-2146233033 Message=输入字符串格式不正确。Source=Microsoft.VisualBasic StackTrace: 在 Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) 在 Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) InnerException: Boolean includeStagesAfterAsyncPoint) InnerException: System.FormatException HResult=-2146233033 Message=输入字符串格式不正确。Source=Microsoft.VisualBasic StackTrace: 在 Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) 在 Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) InnerException:
编辑:这是实际下拉菜单的代码
<asp:dropdownlist style="Z-INDEX: 0" id="ddlDrugType"
OnSelectedIndexChanged="ddlDrugType_SelectedIndexChanged" runat="server"
Height="16px" ViewStateMode="Enabled" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist style="Z-INDEX: 0" id="ddlDrug" runat="server"></asp:dropdownlist>
谢谢你的帮助。