Selecting combobox item at runtime from code

R

Rayne

I have a combobox that is bound to a datasource. It lists the users in
the system and I want it to automatically select the current logged on
user, so that the screen loads that user's information by default when
the app loads.

I have a stored procedure in SQL Server that returns the integer value
of the userid, but I'm having trouble setting that to be the selected
record in the combobox.

In the stored procedure I have:

declare @EID int
select @EID = coalesce(ID, 0) from Employees where Name = @ename
return @EID

It successfully returns the UserID

In VB.net I added that stored procedure as a single value query to my
table adapter for the Employee table. Then in the code I have:

cboEmployees.SelectedValue =
me.EmployeesTableAdapter.get_EmployeeIDbyName(fullname)

fullname is assigned the current user's full name when the app loads.
At first I was getting an exception that the nullable object must have
a value. So I edited the return value on the query in the table adapter
to AllowDBNull = False since the stored procedure will return 0 if the
logged in user isn't yet in the DB anyway.

When I preview data from the dataset designer, it returns the correct
value, but when the code runs, it doesn't select the matching record.
I also tried assigning the return value to a integer variable, then put
a breakpoint to check the value...it didn't return the right value. So
somehow when the table adapter calls the query something isn't right.

Can anyone please help with this? It's critical that the app load the
current user info by default.

Thanks!
Rayne
 
B

Bart Mermuys

Hi,

Rayne said:
I have a combobox that is bound to a datasource. It lists the users in
the system and I want it to automatically select the current logged on
user, so that the screen loads that user's information by default when
the app loads.

I have a stored procedure in SQL Server that returns the integer value
of the userid, but I'm having trouble setting that to be the selected
record in the combobox.

In the stored procedure I have:

declare @EID int
select @EID = coalesce(ID, 0) from Employees where Name = @ename
return @EID

I recently had the same problem, i'm not entirely sure, but i doubt you can
use the return value with a TableAdapter generated method. If you set it to
single-value-query then the method will return the value for the first
row/col of the returned resultset (if any). And second the coalesce you
have will not work as you expect, if the name doesn't exist then coalesce
isn't even used, it just returns the default value for the local variable.

Try something like:

SELECT ISNULL((SELECT ID FROM Employees WHERE Name = @ename),0);
RETURN

This returns a single row/col resultset and the single value is the found ID
or 0.

I'm also wondering if the ComboBox is bound to the (same) Employees table
which would mean it's already loaded into a DataTable and then you could
find the ID using the DataTable.

HTH,
Greetings
 

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