Home All Groups Group Topic Archive Search About

Selecting combobox item at runtime from code

Author
20 Jan 2006 10:13 PM
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

Author
21 Jan 2006 8:20 AM
Cor Ligthert [MVP]
Rayne,

I will be suprised if somebody understand what you are writing here.
(I have the idea that you have tried to do it to good but missed some
essentials for us)

Can you rephrase it.

First of all what method do you use to get the current user.

I assume Environment.username

http://msdn2.microsoft.com/en-us/library/system.environment.username.aspx

However, I am not even sure of that

Cor
Author
21 Jan 2006 1:13 PM
Bart Mermuys
Hi,

Show quoteHide quote
"Rayne" <wifeta***@gmail.com> wrote in message
news:1137795211.381720.180840@g47g2000cwa.googlegroups.com...
>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


Show quoteHide quote
> 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
>