SQL & Using results

T

Trancedified

Hello,

I am trying to load the results of my SQL query into a combobox called
cboTemplate.

That works, but is there a way to select one of the items and do
something with it?

I tried:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click

If cboTemplate.SelectedItem = "New" Then
MsgBox("wow")
End If

End Sub

But it returns an error:

An unhandled exception of type 'System.InvalidCastException'
occurred in microsoft.visualbasic.dll

Additional information: Cast from type 'DataRowView' to
type 'String' is not valid.

'Here is my code
[code:1:17154643fb]
Private Sub cboVolume_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cboVolume.SelectedIndexChanged

Dim strSQL As String
'Connect to the data source.
Dim objConn As SqlConnection = New SqlConnection( _
"Initial Catalog=Card Services;" & _
"Data Source=CHRISXP\LASERFICHE;Integrated
Security=SSPI;")

Me.cboTemplate.Items.Clear()
Try

objConn.Open()

strSQL = "SELECT DISTINCT Tstr.TemplateName "
strSQL &= "FROM Vol, Toc, Tstr WHERE
Vol.VolumeName = " & _
"'" & cboVolume.SelectedItem &
"'" & _
" AND Vol.VolumeId = Toc.VolumeId AND
Toc.TemplateId = Tstr.TemplateId"

Dim command As SqlCommand = New SqlCommand(strSQL,
objConn)
Dim objAdapter As SqlDataAdapter = New
SqlDataAdapter(command)
Dim dataSet As DataSet = New DataSet()
objAdapter.Fill(dataSet, "Tstr")

Me.cboTemplate.DataSource =
dataSet.Tables("Tstr").DefaultView
Me.cboTemplate.DisplayMember = "TemplateName"

Catch ex As Exception
MsgBox(ex.Message)
Finally
objConn.Close()
End Try
End Sub
[/code:1:17154643fb]

Thanks for your suggestions!

Chris
 
G

Guest

hi may be this will help yo
use the tostring for selected ite
that mean
If cboTest.SelectedItem.ToString = "Test" the
msgbox "Hello
endi

this will wor
cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top