Thread: Datalist button to update value in table

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    1

    Datalist button to update value in table

    Hi,

    I have a datalist tempate with 1 textbox (to display message), and 2 buttons in it. They are the Accept and Reject button.

    What i want it to do is when the user clicks on my Accept button, my table with the column "Accepted" will be updated from Null to value "Y" and when the user clicks on the Reject button, the value will be updated to "N".

    The problem I have now is that on button click, my table is not updated and I do not know why.

    This is my source for my datalist:
    Code:
    <table class="main_right_table" width="240px">
    		    		<tr class="headrow">
    		    		   <td width="10px" style="height: 25px"></td>		    		   
    		    		   <td colspan="2" >
    		    		   		<br />Invitation from friends
    		    		   </td>
    		    		   <td width="10px"> </td>
                          
    					</tr>
    					
    					<tr>
    					   <td width="10px" > </td>
    		    		   <td >
    		    		  		 <br/> 
    		    		   		  </td>
    		    		   <td> 	
    		    		  	<br/>Message:<br/>
    							  <asp:DataList ID="DataList1" runat="server" DataKeyField="USR_ID" 
                                   DataSourceID="SqlDataSource1"
                                   OnUpdateCommand="DataList1_UpdateCommand">
                                   <ItemTemplate>
                                       From: <asp:Label ID="Label1" runat="server" Text='<%# Eval("FRIEND_ID") %>'></asp:Label>
                                       <br />
                                       <br />
                                       <asp:TextBox ID="TextBox1" runat="server" Height="64px" 
                                           Text='<%# Eval("MESSAGE") %>' TextMode="MultiLine" ReadOnly="True"></asp:TextBox>
                                       <br />
                                        <br />
                                       <asp:Button ID="Button1" runat="server" Text="Accept" CommandName="Accept" 
                                           />
                                        <asp:Button ID="Button2" runat="server" Text="Reject" CommandName="Reject" 
                                            />
                                       <br />
                                       _______________________<br />
                                        
                                   </ItemTemplate>
                               </asp:DataList>
    		            		 <br/>
    		            		  	  	
    						   <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                   ConnectionString="<%$ ConnectionStrings:FITNESS4FUNDBConnectionString %>" 
                                   DeleteCommand="DELETE FROM [T_USER_FRIEND] WHERE [USR_ID] = @USR_ID AND [FRIEND_ID] = @FRIEND_ID" 
                                   InsertCommand="INSERT INTO [T_USER_FRIEND] ([USR_ID], [FRIEND_ID], [MESSAGE], [ACCEPTED]) VALUES (@USR_ID, @FRIEND_ID, @MESSAGE, @ACCEPTED)" 
                                   ProviderName="<%$ ConnectionStrings:FITNESS4FUNDBConnectionString.ProviderName %>" 
                                   SelectCommand="SELECT [USR_ID], [FRIEND_ID], [MESSAGE], [ACCEPTED] FROM [T_USER_FRIEND] WHERE (([USR_ID] = @USR_ID) AND ([ACCEPTED] IS NULL))" 
                                   UpdateCommand="UPDATE [T_USER_FRIEND] SET [MESSAGE] = @MESSAGE, [ACCEPTED] = @ACCEPTED WHERE [USR_ID] = @USR_ID AND [FRIEND_ID] = @FRIEND_ID">
                                   <SelectParameters>
                                       <asp:SessionParameter Name="USR_ID" SessionField="USR_ID" Type="String" />
                                   </SelectParameters>
                                   <DeleteParameters>
                                       <asp:Parameter Name="USR_ID" Type="String" />
                                       <asp:Parameter Name="FRIEND_ID" Type="String" />
                                   </DeleteParameters>
                                   <InsertParameters>
                                       <asp:Parameter Name="USR_ID" Type="String" />
                                       <asp:Parameter Name="FRIEND_ID" Type="String" />
                                       <asp:Parameter Name="MESSAGE" Type="String" />
                                       <asp:Parameter Name="ACCEPTED" Type="String" />
                                   </InsertParameters>
                                   <UpdateParameters>
                                       <asp:Parameter Name="MESSAGE" Type="String" />
                                       <asp:Parameter Name="ACCEPTED" Type="String" />
                                       <asp:SessionParameter Name="USR_ID" SessionField="usr_id" Type="String" />
                                       <asp:ControlParameter ControlID="DataList1" Name="FRIEND_ID" 
                                           PropertyName="SelectedValue" Type="String" />
                                   </UpdateParameters>
                               </asp:SqlDataSource>
    						</td>
    		    		   <td style="width: 10px" > </td>
    					</tr>
    					
    					<tr>
    					   <td width="10px"> </td>
    		    		   <td>
    		    		  		 <br/> 
    		    		   		 <br/><br/>
    		    		   </td>
    		    		  
    					   <td>
    		    		        </td>
    		    		   <td style="width: 10px"> </td>
    					</tr>
    
    		    	</table>

    the CommandName properties for my Accept button is called "Accept", and for Reject button called "Reject".

    the DataKeyField for my Datalist properties is USR_ID

    Code:
    this is the .cs code behind.
    
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class member_dashboard_dashboard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void DataList1_UpdateCommand(object source,    DataListCommandEventArgs e)
        {
            SqlDataSource1.UpdateParameters["ACCEPTED"].DefaultValue = "Y";
    
            SqlDataSource1.Update();
    
            DataList1.EditItemIndex = -1;
            DataList1.DataBind();
        }
        protected void DataList1_ItemCommand(object source,    DataListCommandEventArgs e)
        {
            if (e.CommandName == "Reject")
            {
                // Add code here to add the item to the shopping cart.
                // Use the value of e.Item.ItemIndex to retrieve the data 
                // item in the control.
                SqlDataSource1.UpdateParameters["ACCEPTED"].DefaultValue = "N";
    
    
                SqlDataSource1.Update();
    
                DataList1.EditItemIndex = -1;
                DataList1.DataBind();
    
            }
            else if (e.CommandName == "Accept")
            {
                // Add code here to add the item to the shopping cart.
                // Use the value of e.Item.ItemIndex to retrieve the data 
                // item in the control.
                SqlDataSource1.UpdateParameters["ACCEPTED"].DefaultValue = "Y";
    
                SqlDataSource1.Update();
    
                DataList1.EditItemIndex = -1;
                DataList1.DataBind();
    
            }
        }
    }

    My table name for that table is called T_USER_FRIEND, and I have a column called "Accepted" where by default it is Null value, and when clicked, the column will be updated to "Y" when Accept button is clicked, and "N" when Reject button is clicked.

    Anybody knows what went wrong?

  2. #2
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    SqlDataSource1.UpdateParameters["ACCEPTED"].DefaultValue = "N";

    Are you sure you don't want to set the Value and not the DefaultValue? Have you debugged it to make sure all the necessary parameters are populated? Is the program actually getting to that point in the code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 06:53 PM
  3. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  4. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  5. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM

Tags for this Thread