Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 3-May-13 2:38am
diggudg767
Edited 3-May-13 17:16pm
v2
Comments
smilingsameera at 3-May-13 8:09am
   
do you want to insert selected item from dropdownlist to database or all the items?
digvijay singh at 3-May-13 21:51pm
   
selected item to database
digvijay singh at 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#
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
digvijay singh at 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 at 7-May-13 10:03am
   
DropDownList ddl = FormView1.FindControl("ddlcourse");
if (ddl.selectedindex > -1) {
yoursqldatasource.insertparameters("course").defaultvalue = ddl.selectedvalue;
}
yoursqldatasource.insert()
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 381
1 Praneet Nadkar 237
2 Marcin Kozub 225
3 Sergey Alexandrovich Kryukov 195
4 Shweta N Mishra 161
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 7 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100