Answered by:
How to insert a selected DDL.datatextfield into the SqlDataSource?

Question
-
User-830563764 posted
In a VS2010 ASP.net 4 webpage, I have a DDL control, 1 textbox, 1 button, and 1 SqlDataSource controls.
Trying to insert the DDL selected text into the database, I coded a button onclick script code to
run SqlDataSource1.Insert().
In the <asp:sqldatasource> codes, I have had insertcommand.... VALUES (@CoName,...), and <insertparameters>
<asp:formparameter name="Coname" formfield="DropDownList1.datatextfield" />
Running the web page displayed an error: Cannot insert the value NULL into column ...
Apparently, the DropDownList1.datatextfield does not get the selected text. What's wrong?
TIA,
JeffreyMonday, May 16, 2016 7:27 PM
Answers
-
User-2010311731 posted
Try DropDownList1.SelectedItem.Text
Matt
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 16, 2016 10:14 PM -
User-2010311731 posted
[Edit] Oops I see you already figured this out. :-) [/Edit]
In the code behind for the button's onclick event, just before you run the insert, try setting the parameter like this...
SqlDataSource1.InsertParameters["Coname"].DefaultValue = DropDownList1.SelectedItem.Text;
Matt
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 17, 2016 5:01 PM
All replies
-
User-2010311731 posted
Try DropDownList1.SelectedItem.Text
Matt
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 16, 2016 10:14 PM -
User-830563764 posted
Matt,
Thanks, but still got the same error. I tried <asp:formparameter name="CoName" formfield="DropDownList1.SelectedItem.Text"
and DropDownList1.datatextfield.Text".Tuesday, May 17, 2016 3:55 PM -
User-830563764 posted
Tried using Label.Text = DropDownList1.SelectedItem.Text to display the selected item.
It worked, the Label showed the selected company name.Tuesday, May 17, 2016 4:11 PM -
User-830563764 posted
Got it!
Before the SqlDataSource1.Insert(), added SqlDataSource1.InsertParameters("CoName").DefaultValue
= DropDownList1.Selected.Text. Apparently, <asp:formparameter formid=... is not good enough to
get the selected text from the DropDownList1.Tuesday, May 17, 2016 4:48 PM -
User-2010311731 posted
[Edit] Oops I see you already figured this out. :-) [/Edit]
In the code behind for the button's onclick event, just before you run the insert, try setting the parameter like this...
SqlDataSource1.InsertParameters["Coname"].DefaultValue = DropDownList1.SelectedItem.Text;
Matt
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 17, 2016 5:01 PM