Conversion from string "SID" to type 'Integer' is not valid - from combobox ValueMember

  • Thread starter Thread starter Rocky48
  • Start date Start date
R

Rocky48

Guest
I have 2 comboboxes which contain ID columns (SID & CID) as the valuemember and the displayMember is text. I want to use the valueMembers to INSERT into an SQL database. However the ValueMember property is a String and the column in the database is an Integer.

How can I get these 2 valueMembers converted into Integers. The code for the complete form is shown below.

Imports System.Data.SqlClient
Imports My_Greeting.Add_VersesDataSetTableAdapters

Public Class frmAddRecord
Private MyDatAdp As New SqlDataAdapter
Private MyDataTbl As New DataTable
Private sql As String = Nothing
Private ds As New DataSet()
Private stepinfo As String = String.Empty
Dim E1 As String
Dim E2 As String
Dim Verse As String

Private connectionString As String = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"

Private Sub frmAddRecord_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TopMost = True
Me.WindowState = FormWindowState.Normal
Me.Sub_EventTableAdapter1.Fill(Me.EventSubDataSet1.Sub_Event)
Me.Event_TypeTableAdapter1.Fill(Me.PrimEventDataSet1.Event_Type)
Dim evt As String
Dim SEvt As String
Dim Verse As String
Dim sql As String = Nothing
Dim ds As New DataSet()
Dim stepinfo As String = String.Empty
Try
stepinfo = "Step: instatiate connection"
Using connection As New SqlConnection(connectionString)
stepinfo = "step: test open connection"
connection.Open()
sql = "Select SID, EVENT from Event_Type"
Using adaptor As New SqlDataAdapter(sql, connection)
adaptor.Fill(ds, "Tab_Event_Type")
End Using
stepinfo = "Load second data"
sql = "Select BID,SUBEVENT from Sub_Event"
Using adaptor As New SqlDataAdapter(sql, connection)
adaptor.Fill(ds, "Tab_Sub_Event")
End Using
End Using
stepinfo = "Step: Bind Event combobox"
cboEvent.DataSource = ds.Tables("Tab_Event_Type")
cboEvent.ValueMember = "SID"
cboEvent.DisplayMember = "Event"
stepinfo = "Bind Sub_Event combobox"
cboSEvent.DataSource = ds.Tables("Tab_Sub_Event")
cboSEvent.ValueMember = "BID"
cboSEvent.DisplayMember = "SubEvent"


Catch ex As Exception
MessageBox.Show($"Error:{stepinfo}{vbNewLine}{ex.ToString}")
End Try

evt = cboEvent.ValueMember
SEvt = cboSEvent.ValueMember
Verse = txtNewVerse.Text
E1 = CType(evt, Integer)
E2 = CType(SEvt, Integer)
End Sub



Private Sub btnClose4_Click(sender As Object, e As EventArgs) Handles btnClose4.Click
Close()
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim query As String = String.Empty
query &= "INSERT INTO Verse (Event, Event_Sub, Verse)"
query &= "VALUES (@Event, @Event_Sub, @Verse)"

Using conn As New SqlConnection("Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True")
Using Comm As New SqlCommand()
With Comm
.Connection = conn
.CommandType = CommandType.Text
.CommandText = query
.Parameters.AddWithValue("@Event", E1)
.Parameters.AddWithValue("@Event_Sub", E2)
.Parameters.AddWithValue("@Verse", Verse)
End With
Try
conn.Open()
Comm.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Error Message")

End Try
End Using
End Using

End Sub

End Class

Friend Class DatabaseClass
End Class



I have tried CInt and CType, but they dont work.


TEH

Continue reading...
 
Back
Top