SqlDataSource used multiple times

Posted on 2007-08-02
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
    LVL 16

    Expert Comment

    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

    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

    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

    I'll give it a shot, thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now