Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Can't store data to a drop down list
Message
From
05/04/2007 10:12:51
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01210953
Message ID:
01212463
Views:
11
Well, you said in your earlier post that DOC_TYPE.SelectedItem.Text = "Anything"; worked fine ... I suspect the problem is where you're putting this code.

At any rate, the problem is caused because there is no selected item at that point. Bracket it with an if ... in a WinForm ComboBox, I'd say to check that the .SelectedIndices.Count > 0, but the WebForm DropDownList doesn't appear to have that property.

If you put your code in an Index Changed event, then you probably won't encounter the problem because there *will* be a .SelectedItem already.

If you're trying to initialize the Text, then maybe you want something like this:
if (DOC_TYPE.Items[0].Selected == false)
    DOC_TYPE.Items[0].Selected = true;

DOC_TYPE.SelectedItem.Text = reader["DOC_TYPE"].ToString();
~~Bonnie


>Hi Bonnie
>I redid the code and have pasted it directly from the page.Load
>
>string x;
>x = reader["DOC_TYPE"].ToString();
>DOC_TYPE.SelectedItem.Text = "ABC";
>
>First two lines work ok
>Third line gives a run time error: Object reference not set to an instance of an object
>
>regards,
>Gerard
>
>
>
>
>>Where are these snippets of code?
>>
>>If both of these statements are one after the other, then I'd say that the reader object doesn't exist yet:
>>
>>DOC_MAN.SelectedItem.Text = "Anything";
>>DOC_MAN.SelectedItem.Text = reader["DOC_MAN"].ToString();
>>
>>However, you say that this snippet works fine:
>>
>>string x;
>>x=reader["DOC_MAN"].ToString();
>>
>>which means that the reader object *does* exist, at least where you put this code snippet.
>>
>>If all 4 of these lines of code are together, one after the other, then your "object reference not set to an instance of an object" error is not making any sense.
>>
>>~~Bonnie
>>
>>
>>
>>>Hi Bonnie.
>>>Did what you suggested and it worked up to a point:-
>>>DOC_MAN.SelectedItem.Text = "Anything"; works fine
>>>DOC_MAN.SelectedItem.Text = reader["DOC_MAN"].ToString();
>>> compiled ok but gave a run time--> object reference not set to an instance of an object.
>>>
>>>However, the following ran ok:
>>>string x;
>>>x=reader["DOC_MAN"].ToString();
>>>
>>>There is just plain text in the DOC_MAN field that comes from a table.
>>>
>>>Regards,
>>>Gerard
>>>
>>>
>>>
>>>>Gerard,
>>>>
>>>>I haven't checked this out (don't currently have a web project open), but according to the docs, there *is* a .Text property for the DropDownList ... maybe it just doesn't show up in Intellisense? However, I suggest that you try this instead:
>>>>
>>>>
>>>>  DOC_MAN.SelectedItem.Text = "ANYTHING";
>>>
>>>>
>>>>
>>>>Just a guess ...
>>>>
>>>>~~Bonnie
>>>>
>>>>>Hi
>>>>>
>>>>>I have a web form with a Drop Down List(DOC_TYPE) which is populated from a table. The populating etc is working fine
>>>>>Storing data to the table in insert,update is ok. I use
>>>>> DOC_TYPE.SelectedValue
>>>>>
>>>>>But when I try to go the other way (Store the Table value to the Form Field)
>>>>> DOC_MAN.Selectedtext = "ANYTHING";
>>>>>
>>>>>There is no DOC_TYPE.Text so I cant use that ?
>>>>>
>>>>>It blows out the Web Page and does not load
>>>>>
>>>>>Any Ideas ?
>>>>>
>>>>>Regards,
>>>>>Gerar
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform