tan lee

tan lee

  • NA
  • 1
  • 0

Datalist template w button to update table column

Jan 23 2010 4:43 AM
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:

<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

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?



Answers (1)