Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to use DropdownList in form view in asp.net web application to insert data from list to database. I tried using <asp:DropDownList />
Please any body can give me code for this to insert data from dropddownlist to database using formview or by manual


Thanks
Posted
Updated 21-Jun-23 7:46am
v2
Comments
smilingsameera 3-May-13 8:09am    
do you want to insert selected item from dropdownlist to database or all the items?
diggudg 3-May-13 21:51pm    
selected item to database
diggudg 3-May-13 22:20pm    
<asp:DropDownList ID="ddlcourse" runat="server" DataTextField='<%#Bind("course") %>' AutoPostBack="true">
<asp:ListItem Text="MCA" Enabled="true" Value="1" Selected="True">
<asp:ListItem Text="BCA" Enabled="true" Value="2">



I'm using this code for dropdownlist control but on insertion, null value is inserted into database, I want to know how to insert selected value of dropdownlist and how to use dropdownlist in formview in asp.net c#

Following should guide you on how to approach the implementation: MSDN: Modifying Data Using a FormView Web Server Control[^]

Additional hint: Have a dropdown in item/edit template as per need and then access that to get selected value. Use that for updation.

Try out. Post specific issue if you face.
 
Share this answer
 
This is not difficult digvijay. I believe you are using the insert template in formview to do the insert.if not, let me know.

on the item inserting event for form view add the below code:

var ddl = FormView1.FindControl("ddlcourse");

if ddl.selectedindex > -1 then
yoursqldatasource.insertparameters("course").defaultvalue = ddl.selectedvalue
end if;


so your sqldatasource should have an insertcommand and insert parameters.

I hope this helps.
 
Share this answer
 
This is not difficult digvijay. I believe you are using the insert template in formview to do the insert.if not, let me know.

on the item inserting event for form view add the below code:

var ddl = FormView1.FindControl("ddlcourse");
if ddl.selectedindex > -1 then
yoursqldatasource.insertparameters("course").defaultvalue = ddl.selectedvalue
end if;
yoursqldatasource.insert();


so your sqldatasource should have an insertcommand and insert parameters.

I hope this helps.
 
Share this answer
 
Comments
diggudg 7-May-13 6:49am    
I'm doing this in C# and your code is in VB, can u convert into c#
thanks for solution.
smilingsameera 7-May-13 10:03am    
DropDownList ddl = FormView1.FindControl("ddlcourse");
if (ddl.selectedindex > -1) {
yoursqldatasource.insertparameters("course").defaultvalue = ddl.selectedvalue;
}
yoursqldatasource.insert()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900