Solved

The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

Posted on 2014-09-10
14
153 Views
Last Modified: 2014-09-12
I have an aspx page with several combo boxes that are initially 'un-bound'
When a user clicks a button the combo's are then 'bound' to their relative SQL Data sources and all is well.

However, in the Page Init event I am checking if a value was passed in via the Query String and if so I am then binding the combo's at that point, this is when I get the above error.

How can I resolve this?
0
Comment
Question by:BrianFord
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +1
14 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 40316100
Might be you are binding SelectedIndex and SelectedValue properties of DropDown one by one.

Try commenting either of them to test out results.
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40316253
Hi BrianFord,

It will be good if you post your code here.
It might that you are binding a field that is not available in you dataset or  you have not set the binding values for the dropdown. on which event are you binding your data to the dataset?
0
 

Author Comment

by:BrianFord
ID: 40316636
Here is the code that's binding one of the boxes, I have 9 other combo's all being bound in the same manner

Sub populateDropDownReason()

cboDetReason.DataSourceID = "sqlReasons"
cboDetReason.DataTextField = "reason"
cboDetReason.DataValueField = "reason"
cboDetReason.DataBind()

End Sub

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 12

Expert Comment

by:jitendra patil
ID: 40316733
Brian,

the code you provided is a just like a common function , so could you post the exact code ,where and when are you using the above function, and how does the binding fails ?

Also, can you check whether the sqlDataSource "sqlReasons" is filled with data before binding or before setting the default values.
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40317263
Why that datasource is in double quotes?
It should be like:

cboDetReason.DataSourceID = sqlReasons
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40317282
Why that datasource is in double quotes?
It should be like:

cboDetReason.DataSource = sqlReasons
0
 

Author Comment

by:BrianFord
ID: 40318989
Here is the code, I've removed all non-rellevant lines to keep it clear:

 Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init

        If Not IsPostBack Then
           
            ' Check if a contactID was passed in?
            If Not Request.QueryString("contactID") Is Nothing Then
                lblContactID.Text = Request.QueryString("contactID")
		getContactDetailsFromUrl(CType(lblContactID.Text, Integer))
               
  	   End If
        End If

End Sub

 Protected Sub cmdSearchContact_Click(sender As Object, e As EventArgs) Handles cmdSearchContact.Click

        getContactDetails
	
	<snip - doing irrelevant stuff here>

    End Sub
	
Sub getContactDetailsFromUrl(ByVal pContactId As Integer)

        getContactDetails(pContactID)
		
	<snip - doing different irrelevant stuff here>
       		
End SubSub 

sub getContactDetails()

	<snip>
	populateDropDownReason

End Sub

 Sub populateDropDownReason()

        ' Populate the reason drop-downlist
            cboDetReason.DataSourceID = "sqlReasons"
            cboDetReason.DataTextField = "reason"
            cboDetReason.DataValueField = "reason"
            cboDetReason.DataBind(

End Sub

Open in new window

0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40319023
From where are you getting the datasource? Does it filled with the records?
0
 

Author Comment

by:BrianFord
ID: 40319038
The data source is defined in the html mark up with an ID of sqlReasons, it does return valid records.

The populateDropdownReasons code works perfectly when called via the button click even after the page has finished loadingt, but when called via the page init event, that's when I get the error

I'm suspecting that it has something to do with what happens during initial page load and postback events?
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 40319171
Hello BrianFord,

I would like to also view the respective declaration of combobox in .aspx page.
0
 

Author Comment

by:BrianFord
ID: 40319349
The Combo Box:

<asp:DropDownList ID="cboDetReason" runat="server" CssClass="form-control"></asp:DropDownList>

Open in new window


The Datasource:
<asp:SqlDataSource ID="sqlReasons" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" ProviderName="<%$ ConnectionStrings:myConnectionString.ProviderName %>" SelectCommand="SELECT reason FROM reason ORDER BY reason"></asp:SqlDataSource>

Open in new window

0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 40319420
Just for a test - Comment the code line :-

cboDetReason.DataValueField = "reason"

and then test. And on which specific line does error occurs?
0
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 500 total points
ID: 40319488
Call your page init code on page load event.
0
 

Author Closing Comment

by:BrianFord
ID: 40319744
Your suggestion was spot on, simply moving the logic to the Load event fixed the issue.

Thank you very much.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reading excel file in .net 2 45
EF5 How do I stop pre-compiled views? 8 48
How does GeoJson relates to Leaflet and Mapbox? 4 44
Ajax calendar distorted 4 15
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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