SqlDataSource used multiple times

Posted on 2007-08-02
Medium Priority
Last Modified: 2013-11-07
I'm trying to use an SqlDataSource to populate several drop down lists in a gridview.  I assume there's a way to just use one datasource and reset it's properties but I can't get it to work.  Here's what I have:

 SqlDataSource2.SelectCommand = "app_InvoiceVendors_GetAll"
ddlVendor.DataTextField = "VendorName"
ddlVendor.DataValueField = "VendorID"
Dim liVendor As ListItem = ddlVendor.Items.FindByText(sVendor)
liVendor.Selected = True

 Dim ddlProjectAIT As DropDownList = e.Row.Controls(5).FindControl("ddlProjectAIT")

SqlDataSource2.SelectCommand = "app_Projects_GetforInvoice"
ddlProjectAIT.DataTextField = "ProjectName"
ddlProjectAIT.DataValueField = "ProjectID"
Dim liProjectAIT As ListItem = ddlProjectAIT.Items.FindByText(sProjectAIT)
liProjectAIT = ddlProjectAIT.Items.FindByText(sProjectAIT)
liProjectAIT.Selected = True

Works fine on the first pass but bombs out on the second.  Any thoughts
Question by:stretch73
  • 2
  • 2
LVL 16

Expert Comment

ID: 19618433
Are you getting any errors? Most likely what's happening is that databindings are occuring when you don't realize it. You probably could add your sql data source switching code to the Databinding events of each drop down list but I don't really believe this would be a recommended practice.

Author Comment

ID: 19618459
I am getting errors.  It's saying:

"'System.Data.DataRowView' does not contain a property with the name 'ProjectName'.

ProjectName should be coming out of the second instance of the datasource.

Is the more logical thing to just use more datasources, seems like kind of a waste.
LVL 16

Accepted Solution

TSmooth earned 1000 total points
ID: 19618608
Yes, use more data sources or go with your own business/data logic and use datasets or typed datasets but that will get a little more complicated. If you want to stick with one sql data source then I think your only option is to assign the correct values in the databinding events of each control sharing the datasource. However, that is really no less wasteful than using two sql data sources and when you use two sql data sources your code will be far more readable/understandable than it would be if you were changing the values all around in the databinding events, IMO.

Author Comment

ID: 19618618
I'll give it a shot, thanks

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SingleRun is a tool that ensures that only one instance of an application is started, running it again brings the application to focus.
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question