I’ve populated a combo box named UserID using the following code, which works
fine: (This is not the entire code block, but enough to see what I'm doing)
objConn = New SqlConnection(strConn)
objCommand = New SqlCommand("SELECT UserID, UserName FROM tblUser", objConn)
objDataAdapter = New SqlDataAdapter
objDataAdapter.SelectCommand = objCommand
objDataTable = New DataTable
objDataAdapter.Fill(objDataTable)
Me.UserID.DataSource = objDataTable
Me.UserID.DisplayMember = "UserName"
Me.UserID.ValueMember = "UserID"
blnIsLoading = False
Now, for the UserID.SelectedIndexChanged event I have the following: I’ve
tried various ways to convert the UserID value to an integer value, but am
having no luck. The error I get for the following code is “Conversion from
type “DataRowView†to type “Integer†is not validâ€. I'm assuming it has
something to do with converting the UserID to an integer.
Private Sub UserID_SelectedIndexChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles UserID.SelectedIndexChanged
objConn = New SqlConnection(strConn)
objConn.Open()
objCommand = New SqlCommand
objCommand.CommandText = "dbo.usp_User"
objCommand.CommandType = CommandType.StoredProcedure
objCommand.Connection = objConn
objCommand.Parameters.Add("@intUserID", SqlDbType.Int).Value =
Cint(Me.UserID.SelectedValue)
objDataReader = objCommand.ExecuteReader
If objDataReader.HasRows Then
objDataReader.Read()
Me.UserName.Text = objDataReader.Item("UserName")
End If
End Sub
What I'm trying to achieve here is the following. 1. I want to display a
listing of UserNames in my UserID combo box. I've got this part working. 2.
Then, I want to select a UserName in the listing and display another veriable
in my tbluser table. For this testing I'm using UserName, but it could also
be FirstName, LastName, or what ever. I'm just trying to get this to work
right.
I started this code from a training book I have, except the training code is
using OLEDB, while I'm using SQL Server. If anyone can show me how to achieve
what I'm looking for would be great.
Any help would be greatly appreciated.
Greg