2011-07-21 90 views
0

我有一個DropDownListListBox運行良好。當用戶將Products表中的所選內容移動到ListBox中的ProductsMail表時,我需要能夠將所選值從DropDownList插入到數據庫中。我試圖多次修改SQL字符串,但沒有成功,例如"+EmailDDN.SelectedItem+""+EmailDDN.SelectedValue+"等。我可以將所選值顯示在EmailLabel1標籤中,但不在查詢中。請提供解決方案。 我很感謝你的時間。 _ JT將單個C#下拉選擇的值傳遞給查詢字符串

嗨suryakiran,

在 「DestinationDataSource」:

UpdateCommand="UPDATE ProductsMail SET Name = @Name, Email = @Email, EmailID = @EmailID WHERE ID = @ID". 

我試圖修改,如:

"UPDATE ProductsMail SET Name = @Name, Email = ' + @Email + ', EmailID = @EmailID WHERE ID = @ID" and "UPDATE ProductsMail SET Name = @Name, Email = ' +EmailDDN.SelectedItem+ ', EmailID = @EmailID WHERE ID = @ID" 

和其他人,但沒有成功。

<telerik:RadScriptManager ID="ScriptManager" runat="server" /> 
<telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1" /> 
<telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel1" LoadingPanelID="RadAjaxLoadingPanel1"> 
    <asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:IPdataConnectionString %>" 
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT DISTINCT Email FROM [aspnet_Membership]" 
    runat="server"></asp:SqlDataSource> 
    <label for="EmailDDN"> 
    <asp:Label ID="Label2" runat="server" class="text">Select Email:&nbsp;</asp:Label> 
    </label> 
    <asp:DropDownList ID="EmailDDN" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" 
    AppendDataBoundItems="True" DataTextField="Email" DataValueField="Email" OnSelectedIndexChanged="DDN_SelectedIndexChanged"> 
    <asp:ListItem> 
      Select your email address 
    </asp:ListItem> 
    </asp:DropDownList> 
    <br /> 
    <br /> 
    <asp:Label ID="EmailLabel1" runat="server" /> 
    <br /> 
    <br /> 
    <br /> 
</telerik:RadAjaxPanel> 
<telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel2" /> 
<telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel2" LoadingPanelID="RadAjaxLoadingPanel1"> 
    <telerik:RadListBox runat="server" ID="RadListBox1" DataSourceID="SourceDataSource" 
    AllowAutomaticUpdates="true" DataKeyField="ID" DataTextField="Name" DataSortField="SortOrder" 
    Width="230px" Height="200px" AllowTransfer="true" TransferToID="RadListBox2" TransferMode="Copy" 
    AutoPostBackOnTransfer="true" /> 
    <telerik:RadListBox runat="server" ID="RadListBox2" DataSourceID="DestinationDataSource" 
    AllowAutomaticUpdates="true" DataKeyField="ID" DataTextField="Name" DataSortField="SortOrder" 
    Width="230px" Height="200px" AllowReorder="true" AutoPostBackOnReorder="true" AllowDelete="True" 
    AutoPostBackOnDelete="true" /> 
</telerik:RadAjaxPanel> 
<asp:SqlDataSource ID="SourceDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:IPdataConnectionString %>" 
    ProviderName="System.Data.SqlClient" DeleteCommand="DELETE FROM Products WHERE ID = @ID" 
    InsertCommand="INSERT INTO Products (Name, ID, Email, EmailID) VALUES (@Name, @ID, @Email, @EmailID)" 
    SelectCommand="SELECT Name, ID, Email FROM Products" UpdateCommand="UPDATE Products SET Name = @Name, Email = @Email, EmailID = @EmailID WHERE ID = @ID"> 
    <UpdateParameters> 
    <asp:Parameter Name="Name" Type="String" /> 
    <asp:Parameter Name="ID" Type="Int32" /> 
    <asp:Parameter Name="Email" Type="String" /> 
    <asp:Parameter Name="EmailID" Type="Int32" /> 
    </UpdateParameters> 
    <DeleteParameters> 
    <asp:Parameter Name="ID" Type="Int32" /> 
    </DeleteParameters> 
    <InsertParameters> 
    <asp:Parameter Name="Name" Type="String" /> 
    <asp:Parameter Name="ID" Type="Int32" /> 
    <asp:Parameter Name="Email" Type="String" /> 
    <asp:Parameter Name="EmailID" Type="Int32" /> 
    </InsertParameters> 
    <SelectParameters> 
    <asp:ControlParameter ControlID="EmailDDN" DefaultValue="Select One" Name="Email" 
     PropertyName="SelectedValue" /> 
    </SelectParameters> 
</asp:SqlDataSource> 
<asp:SqlDataSource ID="DestinationDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:IPdataConnectionString %>" 
    ProviderName="System.Data.SqlClient" DeleteCommand="DELETE FROM ProductsMail WHERE ID = @ID" 
    InsertCommand="INSERT INTO ProductsMail (Name, ID, Email, EmailID) VALUES (@Name, @ID, @Email, @EmailID)" 
    SelectCommand="SELECT Name, ID, Email FROM ProductsMail" UpdateCommand="UPDATE ProductsMail SET Name = @Name, Email = @Email, EmailID = @EmailID WHERE ID = @ID"> 
    <UpdateParameters> 
    <asp:Parameter Name="Name" Type="String" /> 
    <asp:Parameter Name="Email" Type="String" /> 
    <asp:Parameter Name="EmailID" Type="Int32" /> 
    <asp:Parameter Name="ID" Type="Int32" /> 
    </UpdateParameters> 
    <DeleteParameters> 
    <asp:Parameter Name="ID" Type="Int32" /> 
    </DeleteParameters> 
    <InsertParameters> 
    <asp:Parameter Name="Name" Type="String" /> 
    <asp:Parameter Name="Email" Type="String" /> 
    <asp:Parameter Name="EmailID" Type="Int32" /> 
    <asp:Parameter Name="ID" Type="Int32" /> 
    </InsertParameters> 
    <SelectParameters> 
    <asp:ControlParameter ControlID="EmailDDN" DefaultValue="Select One" Name="Email" 
     PropertyName="SelectedValue" /> 
    </SelectParameters> 
</asp:SqlDataSource> 
============================================ 
============================================ 
protected void DDN_SelectedIndexChanged(System.Object sender, System.EventArgs e) 
{ 
    EmailLabel1.Text = EmailDDN.SelectedItem.ToString(); 
} 
+0

您可以在那裏定義sql數據源並在select-change上更新電子郵件標籤,但是您實際上在哪裏調用數據庫更新? – Frater

+0

上面的回覆.... –

回答

1

在你已經在<SelectParameters>部中使用的<asp:ControlParameter>您可以通過在<UpdateParameters>節這樣通過電子郵件值相同的方式。

<InsertParameters> 
    <asp:Parameter Name="Name" Type="String" /> 
    <asp:ControlParameter Name="Email" PropertyName="SelectedValue" ControlID="EmailDDN" /> 
    <asp:Parameter Name="EmailID" Type="Int32" /> 
    <asp:Parameter Name="ID" Type="Int32" /> 
    </InsertParameters> 
+0

謝謝rtpHarry,這個伎倆!一個附加問題。 Products表中的我的ID列被配置爲「[ID] [int] IDENTITY(1,1)NOT NULL,」。發生插入時,ProductsMail ID列將接收NULL而不是數字值。我怎樣才能讓它插入? –

+0

你應該真的開始一個新的問題的新線程,但我認爲你可以從插入刪除整個和@ID。您只需將其忽略爲自動遞增標識列,當您添加新記錄時它應該填充自身。 – rtpHarry

相關問題