在查询数据库时获取InvalidCastException,如果查询被复制到db上的新查询,则它可以成功运行

时间:2012-11-22 00:32:47

标签: asp.net vb.net sqlconnection

在我的应用程序的页面上,当点击提交按钮时,上面表格中的信息将被存储到数据库中。我在运行程序时遇到此错误,但如果我要使用断点并复制SQLtext字符串,则将此代码粘贴到数据库管理器上的新查询中。错误总是如下所示,但“速度”根据ddlDrugs的值而有所不同(例如,如果我们选择了Alcohol,则错误会说从字符串“Alcohol”转换为“Integer”类型无效):

Conversion from string "Speed" to type 'Integer' is not valid. 

有谁知道为什么会这样? ddlDrugs是一个下拉列表,其中使用ddlDrugType更改时触发的回发填充。下面列出的是第一个是命中提交按钮时的子命令,下面是绑定下拉列表的功能。

    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   消息=从字符串“速度”到“整数”类型的转换无效。   来源= Microsoft.VisualBasic程序   堆栈跟踪:        在Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)        在Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(对象值)        at RR.NewCustomer.btnSubmit_Click(Object sender,EventArgs e)位于C:\ Users \ Administrator \ Documents \ Visual Studio 2010 \ Projects \ RR \ RR \ NewCustomer.aspx.vb:第276行        在System.Web.UI.WebControls.Button.OnClick(EventArgs e)        在System.Web.UI.WebControls.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)        在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)        在System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)   InnerException:System.FormatException        的HResult = -2146233033        消息=输入字符串格式不正确。        来源= Microsoft.VisualBasic程序        堆栈跟踪:             在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>

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你能发布ddlDrug的html代码吗?我怀疑你有像

这样的东西
<option selected="selected" value="Speed">Speed</option>
<option value="Alchol">Alchol</option>

相反,您应该为该值设置某种ID。

或者您需要更改

Dim drugIndex As Integer = ddlDrug.SelectedValue

Dim drugIndex As String = ddlDrug.SelectedValue