开发者

Passing value from single C# dropdown selection to a query string

开发者 https://www.devze.com 2023-03-22 01:03 出处:网络
I have a DropDownList and a ListBox that are functioning fine. I need to be able to insert the selected value from the DropDownList into the database when the user moves a selection from the Products

I have a DropDownList and a ListBox that are functioning fine. I need to be able to insert the selected value from the DropDownList into the database when the user moves a selection from the Products table to the ProductsMail table in the ListBox. I have attempted to modify the SQL string multiple times with no success like "+EmailDDN.SelectedItem+" and "+EmailDDN.SelectedValue+", etc. I can get the selected value to display in the EmailLabel1 Label but not in the query. Please provide a solution. I appreciate your time. _ JT

Hi suryakiran,

In the "DestinationDataSource":

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

I have attempted modifications such as:

"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"

and others with no success.

<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 Na开发者_运维问答me="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();
}


In the same way that you have used an <asp:ControlParameter> in the <SelectParameters> section you can pass your email value in by doing this in the <UpdateParameters> section.

  <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

精彩评论

暂无评论...
验证码 换一张
取 消