on a page in my application when the submit button is hit, the information from the form above is to be stored into the database. I am getting this error when I run the program, but if I am to use breakpoints and copy the SQLtext string then paste this code into a new query on the DB manager it works. The error always says the following, but “Speed” is different depending on the value from ddlDrugs (for example, if we selected Alcohol, the error would say Conversion from string “Alcohol” to type ‘Integer’ is not valid) :
Conversion from string "Speed" to type 'Integer' is not valid.
Does anyone see why this is happening? ddlDrugs is a dropdown in which is populated using a post back triggered when ddlDrugType is changed. Listed below first is the sub when the submit button is hit, and below that the function that binds the dropdown.
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
Binding the dropdown on postback
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
and here is the detailed error
System.InvalidCastException was unhandled by user code
HResult=-2147467262
Message=Conversion from string “Speed” to type ‘Integer’ is not valid.
Source=Microsoft.VisualBasic
StackTrace:
at 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:line 276
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at 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=Input string was not in a correct format.
Source=Microsoft.VisualBasic
StackTrace:
at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
InnerException:
edit: here is the code for the actual dropdowns
<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>
Thanks for your help.
Can you post the html code for ddlDrug. I am suspecting you have something like
Instead you should have some sort of ID for the value.
or you need to change
to