Groups | Blog | Home
all groups > visual studio .net general > september 2007 >

visual studio .net general : Popullate Combo Box using VB.Net 2005


Greg
9/22/2007 6:26:01 PM
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
Stevanich
9/23/2007 2:56:41 AM
Greg,

I belive your error is here:

objDataReader.Item("UserName")

The item of an objDataReader is a DataRow or DataRowView. You need to
select the row first, and then get the UserName. Something like:

objDataReader.Item(0).Item("UserName")

Also, I've answered a couple of your questions here on the Visual Studio
General newsgroup, but you will want to ask future questions in one of the
Visual Basic newsgroups.

Hope this helps,


Steve - dotneticated.com

[quoted text, click to view]

jetan NO[at]SPAM online.microsoft.com (
9/24/2007 2:53:00 AM
Hi Greg,

Have you run your application under VS2005 debugger? If so, which statement
do you get this exception error? Based on my review, I would suspect this
exception is thrown in the statement below, yes?
objCommand.Parameters.Add("@intUserID", SqlDbType.Int).Value =
Cint(Me.UserID.SelectedValue)

However, I still can not understand why the Cint() calling will throw this
exception. When the exception is thrown in the debugger, I would recommend
you to copy "Me.UserID.SelectedValue" into the debugger "Watch" window,
this will reveal what value and its type in the watch window. Please paste
here for further analysis.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button