473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Selecting combobox item at runtime from code

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(full name)

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

Jan 20 '06 #1
2 5992
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/lib....username.aspx

However, I am not even sure of that

Cor
Jan 21 '06 #2
Hi,

"Rayne" <wi*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.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

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(full name)

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

Jan 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: NCrum | last post by:
I want to set the Default value of a Combobox for any changeable record and have got this working but it is totaly unsatisfactory see the code below I loop through the items in the Combo looking...
4
by: Strahimir Antoljak | last post by:
Has anyone experienced problems with a combo box SelectIndex property? Is there a limit to the number of Items for a combo box? Namely, when I set programmatically ComboBox.SelectIndex...
7
by: sparkle | last post by:
Hi Everybody, I'm filling a combobox from a class, which works fine on it's own. But when I insert code to fill in other controls something in the combobox fill is causing the...
6
by: Mike Wilson | last post by:
Dear Group, I have a heirarchical set of database tables, say - "order" and "order_type" and want to display a series of orders in a grid control, and in place of the order_type foreign key...
0
by: koti | last post by:
hi i have written some code for selecting an item in combobox dropdown list which is in datagrid columm. by scrolling the mouse we select any item from the list. but by pressing the down key...
1
by: abattista | last post by:
Hi, My combobox has 684 items, (controlled from a separate table). However, the item I select does not populate in the combobox field? If fills with an item in the first dozen or so. Is it...
19
by: active | last post by:
I'm using a ComboBox to display objects of a class I've defined, say CQQ. Works great except somehow I occasionally set an Item to a String object instead of an object of type CQQ. It looks...
11
Frinavale
by: Frinavale | last post by:
This question is going to sound a little crazy but.........How do you set the selected item in a ComboBox? I am populating a ComboBox with a bunch of instances of a custom private class: For...
0
by: Brandon | last post by:
Hi there... I got a WPF project that I am trying to select a ComboBoxItem in a ComboBox based on a string from the selected ListView item in the project... This ComboBox is unbound now and the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.