Click to See Complete Forum and Search --> : how to add a dropdownlist


barantamer
08-10-2008, 01:55 PM
hello i am new o asp.net, i want to add a dropdownlist to my FormView which generates a list by using SqlDataSource2 , and when user selects from the dropdown list , code should fill out the form view by using SqlDataSource1 . I couldn`t connect the dropdownlist to the formview . Any help would be great . Thanks...


My dropdown list code
<asp:DropDownList ID="list1" runat="server" DataSourceID="SqlDataSource2" DataTextField="name" DataValueField="id" AutoPostBack="true" OnSelectedIndexChanged="changed">
</asp:DropDownList>

My formview code

<form id="form1" runat="server">




<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"
Width="372px">

<EditItemTemplate>
id:
<asp:TextBox ID="idTextBox" runat="server" Text='<%# Bind("id") %>' />
<br />
name:
<asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
<br />
surname:
<asp:TextBox ID="surnameTextBox" runat="server" Text='<%# Bind("surname") %>' />
<br />
email:
<asp:TextBox ID="emailTextBox" runat="server" Text='<%# Bind("email") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
&nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
id:
<asp:TextBox ID="idTextBox" runat="server" Text='<%# Bind("id") %>' />
<br />
name:
<asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
<br />
surname:
<asp:TextBox ID="surnameTextBox" runat="server" Text='<%# Bind("surname") %>' />
<br />
email:
<asp:TextBox ID="emailTextBox" runat="server" Text='<%# Bind("email") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
&nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>


<ItemTemplate>


<br />
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Bind("id") %>' />
<br />
name:
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>' />
<br />
surname:
<asp:Label ID="surnameLabel" runat="server" Text='<%# Bind("surname") %>' />
<br />
email:
<asp:Label ID="emailLabel" runat="server" Text='<%# Bind("email") %>' />
<br />
<asp:Button id="switchtoinsert" OnClick="SwitchingToInsert" Text="Insert Mode" Width="100" Runat="Server"/>
<asp:Button id="Button1" OnClick="SwitchingToEdit" Text="Edit Mode" Width="100" Runat="Server"/>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:barantestConnectionString2 %>"
SelectCommand="SELECT * FROM [UserDetails] where id=@id"
InsertCommand="insert into UserDetails (id,name,surname,email)
values (@id ,@name,@surname,@email)">



</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:barantestConnectionString2 %>"
SelectCommand="SELECT * FROM [UserDetails] order by id"
InsertCommand="insert into UserDetails (id,name,surname,email)
values (@id ,@name,@surname,@email)"


>
</asp:SqlDataSource>
</form>

ryanbutler
08-12-2008, 02:51 PM
I see a few problems with that code. First, on your SqlDataSource1, you're using a WHERE clause for the primary key of the table (I'm assuming) but you never add that to the query string parameter below the data source, which I think is the reason why the drop down list is never loading. Secondly, you're never adding the parameters for the insert command on the data source. Thirdly, on SqlDataSource2, you never add the parameters for the insert statement, which means that your values are never getting added to the parameters collection.