locked
ObjectDataSource doesn't trigger the Data Access Layer Method RRS feed

  • Question

  • User-1993844047 posted
    <%@ Page Title="" Language="C#" MasterPageFile="~/RCS.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RCS2_04.clerk.Default" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <script type="text/javascript">
            function OnSave(obj) {
                // Find the row this button is in
                var tr = $(obj).closest("tr");
                // Get the value from the edit control
                var firstNameEdit = tr.find("[id*='firstNameEdit']").val();
                // assign value to hidden input
                tr.find("[id*='firstNameHidden']").val(firstNameEdit);
                var lastNameEdit = tr.find("[id*='lastNameEdit']").val();
                tr.find("[id*='lastNameHidden']").val(lastNameEdit);
            }
        </script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="IconContentPlaceHolder" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="SideBarContentPlaceHolder" runat="server">
    </asp:Content>
    <asp:Content ID="Content4" ContentPlaceHolderID="PageHeaderContentPlaceHolder" runat="server">
    </asp:Content>
    <asp:Content ID="Content5" ContentPlaceHolderID="TableHeaderNamePlaceHolder" runat="server">
    </asp:Content>
    <asp:Content ID="Content6" ContentPlaceHolderID="TableContentHolder" runat="server">          
        <div style='margin: 5px 5px 5px 5px; border: 1px solid blue; text-align: center;
            <%= Visibility("MSG") %>'>
            No record to display
        </div>
        <div style='margin: 5px 5px 5px 5px; border: 1px; <%= Visibility("Navigation") %>'>
            <table width="100%" border="0">
                <tr>
                    <td align="right">
                        Page Size&nbsp;:&nbsp;<asp:DropDownList ID="ddlPageSize" runat="server" AutoPostBack="true">
                            <asp:ListItem Text="7" Selected="True" Value="7"></asp:ListItem>
                            <asp:ListItem Text="10" Value="10"></asp:ListItem>
                            <asp:ListItem Text="20" Value="20"></asp:ListItem>
                            <asp:ListItem Text="50" Value="50"></asp:ListItem>
                            <asp:ListItem Text="99" Value="99"></asp:ListItem>
                            <asp:ListItem Text="100" Value="100"></asp:ListItem>
                            <asp:ListItem Text="200" Value="200"></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <asp:LinkButton ID="lbtnFirst" runat="server" Text=" first " OnClick="lbtnFirst_Click"></asp:LinkButton>
                        &nbsp; &nbsp;
                        <asp:LinkButton ID="lbtnPrev" runat="server" Text=" previous " OnClick="lbtnPrev_Click"></asp:LinkButton>&nbsp;
                        &nbsp;
                        <asp:LinkButton ID="lbtnNext" runat="server" Text=" next " OnClick="lbtnNext_Click"></asp:LinkButton>
                        &nbsp; &nbsp;
                        <asp:LinkButton ID="lbtnLast" runat="server" Text=" last " OnClick="lbtnLast_Click"></asp:LinkButton>
                    </td>
                </tr>
            </table>
        </div>
          
            <asp:Repeater ID="rptIssues" runat="server" onitemcommand="rptIssues_ItemCommand" OnItemDataBound="OnItemDataBound">            
                <HeaderTemplate>
                    <table class="table table-hover table-bordered" width="100%" cellpadding="2" cellspacing="2">
                        <tr>
                            <td></td>
                            <td style="width: 20%; font-weight: bold">
                                <asp:LinkButton ID="lbtnItemNo" Text="Item #" runat="server" CommandName="ItemNo"></asp:LinkButton>
                            </td>
                            <td style="width: 20%; font-weight: bold">
                                <asp:LinkButton ID="lbtnProjectCode" Text="Project Code" runat="server" CommandName="ProjectCode"></asp:LinkButton>
                            </td>
                            <td style="width: 20%; font-weight: bold">
                                <asp:LinkButton ID="lbtnRespRaisedDate" Text="Response Raised Date" runat="server" CommandName="RespRaisedDate"></asp:LinkButton>
                            </td>
                            <td style="width: 20%; font-weight: bold">
                                <asp:LinkButton ID="lbtnFirstName" Text="First Name" runat="server" CommandName="FName"></asp:LinkButton>
                            </td>
                            <td style="width: 20%; font-weight: bold">
                                <asp:LinkButton ID="lbtnLastName" Text="Last Name" runat="server" CommandName="LName"></asp:LinkButton>
                            </td>
                            <td style="width: 20%; font-weight: bold">
                                <asp:LinkButton ID="lbtnMobileNum" Text="Mobile Number" runat="server" CommandName="MobileNum"></asp:LinkButton>
                            </td>
                            <td style="width: 20%; font-weight: bold">
                                <asp:LinkButton ID="lbtnEmail" Text="Email" runat="server" CommandName="Email"></asp:LinkButton>                            
                            </td>
                            <td style="width: 20%; font-weight: bold">
                                <asp:LinkButton ID="lbtnRespDesc" Text="Response Description" runat="server" CommandName="RespDesc"></asp:LinkButton>
                                
                            </td>
                            <td style="width: 20%; font-weight: bold">
                                <asp:LinkButton ID="lbtnSupportDoc" Text="Support Document" runat="server" CommandName="SupportDoc"></asp:LinkButton>                            
                            </td>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>             
                        <td>
                            <asp:ImageButton ID="ImageButton1" ImageUrl="../Images/EditDocument.png" runat="server" CommandName="edit" />
                            <asp:ImageButton ID="ImageButton2" ImageUrl="../Images/Delete_black_32x32.png" runat="server"
                                CommandName="delete" />
                        </td> 
                        <td>
                            <asp:Label runat="server" ID="itemNo"><%# Eval("ItemNo") %></asp:Label>
                            <asp:PlaceHolder runat="server" ID="itemNoPlaceHolder" />
                            <input type="hidden" runat="server" id="itemNoHidden" visible="false" />
                        </td>
                        
                        <td>
                            <asp:Label runat="server" ID="projectCode"><%# Eval("ProjectCode") %></asp:Label>
                            <asp:PlaceHolder runat="server" ID="projectCodeEditPlaceHolder" />
                            <input type="hidden" runat="server" id="projectCodeHidden" visible="false" />
                        </td>
                        <td>
                            <asp:Label runat="server" ID="respRaisedDate"><%# Eval("RespRaisedDate",  "{0:dd-MMM-yyyy}") %></asp:Label>
                            <asp:PlaceHolder runat="server" ID="respRaisedDatePlaceHolder" />
                            <input type="hidden" runat="server" id="respRaisedDateHidden" visible="false" />
                        </td>
                        <td>
                            <asp:Label runat="server" ID="firstName"><%# Eval("FName") %></asp:Label>
                            <asp:PlaceHolder runat="server" ID="firstNameEditPlaceholder" />
                            <input type="hidden" runat="server" id="firstNameHidden" visible="false" />
                        </td>
                        <td>
                            <asp:Label runat="server" ID="lastName"><%# Eval("LName") %></asp:Label>
                            <asp:PlaceHolder runat="server" ID="lastNameEditPlaceholder" />
                            <input type="hidden" runat="server" id="lastNameHidden" visible="false" />
                        </td>
                        <td>
                            <asp:Label runat="server" ID="mobileNum"><%# Eval("MobileNum") %></asp:Label>
                            <asp:PlaceHolder runat="server" ID="mobileNumPlaceHolder" />
                            <input type="hidden" runat="server" id="mobileNumHidden" visible="false" />
                        </td>
                        <td>
                            <asp:Label runat="server" ID="email"><%# Eval("Email") %></asp:Label>
                            <asp:PlaceHolder runat="server" ID="emailPlaceHolder" />
                            <input type="hidden" runat="server" id="emailHidden" visible="false" />
                        </td>
                        <td>
                            <asp:Label runat="server" ID="respDesc"><%# Eval("RespDesc") %></asp:Label>
                            <asp:PlaceHolder runat="server" ID="respDescPlaceHolder" />
                            <input type="hidden" runat="server" id="respDescHidden" visible="false" />
                        </td>
                        <td>
                            <asp:Image runat="server" ID="supportDoc" ImageUrl = '<%# Eval("SupportDoc") %>' /></asp:Image>
                            <asp:PlaceHolder runat="server" ID="supportDocPlaceHolder" />
                            <input type="hidden" runat="server" id="supportDocHidden" visible="false" />
                        </td>
                         
    
                         
    
                    </tr>                 
                </ItemTemplate>
                
                <FooterTemplate>
                        <tr>
                            <td>
                                <asp:ImageButton ID="Add" ImageUrl="../Images/112_Plus_Blue_32x32_72.png" runat="server"
                                    OnClick="OnAddRecord" />
                            </td>
                            <td><asp:TextBox runat="server" ID="NewItemNo" /></td>
                            <td><asp:TextBox runat="server" ID="NewProjectCode" /></td>
                            <td><asp:Calendar runat="server" ID="NewRespRaisedDate" /></td>
                            <td><asp:TextBox runat="server" ID="NewFirstName" /></td>
                            <td><asp:TextBox runat="server" ID="NewLastName" /></td>
                            <td><asp:TextBox runat="server" ID="NewMobileNum" /></td>
                            <td><asp:TextBox runat="server" ID="NewEmail" /></td>
                            <td><asp:TextBox runat="server" ID="NewRespDesc" /></td>
                            <td><asp:FileUpload runat="server" ID="NewSupportDoc" /></td>
                             
                        </tr>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
    
    
         
        <asp:ObjectDataSource ID="MainIssueDS" runat="server" 
            InsertMethod="InsertIssue"
            SelectMethod="GetAllIssues"         
            TypeName="RCS2_04.common.IssueLogDataAccessLayer"          
            DataObjectTypeName="RCS2_04.common.Issue">
            <InsertParameters>
                <asp:Parameter Name="ProjectCode" Type="Int32" />
                <asp:Parameter Name="RespRaisedDate" DbType="Date" />
                <asp:Parameter Name="FName" Type="String" />
                <asp:Parameter Name="LName" Type="String" />
                <asp:Parameter Name="MobileNum" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
                <asp:Parameter Name="RespDesc" Type="String" />
                <asp:Parameter Name="SupportDoc" />
            
            </InsertParameters>
        </asp:ObjectDataSource>
    
    </asp:Content>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Configuration;
    using System.Data;
    using System.Data.Sql;
    using System.Data.SqlClient;
    
    
    namespace RCS2_04.common
    { 
        public class IssueLogDataAccessLayer
        {
            public static List<Issue> GetAllIssues()
            {
                List<Issue> listIssues = new List<Issue>();
    
                string CS = ConfigurationManager.ConnectionStrings["RCS_2_02ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(CS))
                {
                    SqlCommand cmd = new SqlCommand(@"SELECT i.[ItemNo], i.[ProjectCode], i.[RespRaisedDate], i.[Lname], i.[Fname], i.[MobileNum], i.[Email], i.[RespDesc], s.[Image_Data] AS [SupportDoc]
                                                        FROM [IssueLog] AS i 
                                                        LEFT JOIN
                                                        [SupportDoc] AS s
                                                        ON i.ItemNo = s.ItemNo", con);
    
                    con.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        Issue issue = new Issue();
                        issue.ItemNo = Convert.ToInt32(rdr["ItemNo"]);
                        issue.ProjectCode = Convert.ToInt32(rdr["ProjectCode"]);
                        issue.RespRaisedDate = Convert.ToDateTime(rdr["RespRaisedDate"]);
                        issue.FName = rdr["FName"].ToString();
                        issue.LName = rdr["FName"].ToString();
                        issue.MobileNum = rdr["MobileNum"].ToString();
                        issue.Email = rdr["Email"].ToString();
                        issue.RespDesc = rdr["RespDesc"].ToString();
    
                        listIssues.Add(issue);
                    }
    
                    return listIssues;
    
                }
            }
    
            public static void InsertIssue(Issue newIssue)
            { 
                string CS = ConfigurationManager.ConnectionStrings["RCS_2_02ConnectionString"].ConnectionString; 
                
                using (SqlConnection con = new SqlConnection(CS))
                {
                    SqlTransaction trans = null;
                    try
                    {
                        trans = con.BeginTransaction("Insert_IssueLog_Coherent");
    
                        SqlCommand cmd = new SqlCommand(
                            @"INSERT INTO [IssueLog] ([ProjectCode], [RespRaisedDate], [Lname], [Fname],
                                    [MobileNum], [Email], [RespDesc])
                                  VALUES (@ProjectCode, @RespRaisedDate, @Lname, @Fname,
                                    @MobileNum, @Email, @RespDesc", con);
    
                        cmd.Parameters.AddWithValue("@ProjectCode", newIssue.ProjectCode);
                        cmd.Parameters.AddWithValue("@RespRaisedDate", newIssue.RespRaisedDate);
                        cmd.Parameters.AddWithValue("@Fname", newIssue.FName);
                        cmd.Parameters.AddWithValue("@Lname", newIssue.LName);
                        cmd.Parameters.AddWithValue("@MobileNum", newIssue.MobileNum);
                        cmd.Parameters.AddWithValue("@Email", newIssue.Email);
                        cmd.Parameters.AddWithValue("@RespDesc", newIssue.RespDesc);
    
                        cmd.ExecuteNonQuery();
    
                        cmd = new SqlCommand(
                            @"INSERT INTO [SupportDoc] (ItemNo, Image_Data)
                                  VALUES (@ItemNo, @Image_Data)", con);
    
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@ItemNo", newIssue.ItemNo);
                        cmd.Parameters.AddWithValue("@Image_Data", newIssue.SupportDoc);
    
                        cmd.ExecuteNonQuery();
    
    
                        trans.Commit();
                    }
                    catch (Exception e)
                    {
                        trans.Rollback();
                    }
                } // end using
            }
                    
                    
        }
    
        
    }

    I am trying to add a new record to the database, but when I place a breakpoint at the insertIssue method,

    I pressed on the imagebutton with ID="Add", and Data Access Layer method of InsertIssue is not called.

    How come?

    Thanks

    Jack

    Tuesday, March 24, 2015 1:24 AM

All replies

  • User-1993844047 posted

    I know now.

    I am missing this part

    public IList<Issue> IssueList
            {
                get
                {
                    IList<Issue> list;
    
                    if (ViewState["IssueList"] != null)
                        list = ViewState["IssueList"] as IList<Issue>;
    
    
                    list = IssueLogDataAccessLayer.GetAllIssues();
    
                    if (list == null)
                        return new List<Issue>();
                    else
                        ViewState["IssueList"] = list;
    
    
                    IssueListCount = list.Count;
    
                    string sortExpression = (ViewState["SortExpression"] ?? "").ToString();
                    bool isAscending = true;
    
                    if (ViewState["SortDetails"] == null)
                    {
                        Hashtable hs = new Hashtable();
                        hs.Add("ItemNo", false);
                        ViewState["SortDetails"] = hs;
                    }
    
                    Hashtable hsSortDetails = ViewState["SortDetails"] as Hashtable;
    
                    if (sortExpression.Length > 0)
                    {
                        if (!hsSortDetails.Contains(sortExpression))
                            hsSortDetails.Add(sortExpression, true);
                        isAscending = bool.Parse(hsSortDetails[sortExpression].ToString());
                        hsSortDetails[sortExpression] = !isAscending;
                    }
    
                    switch (sortExpression)
                    {
                        case "ItemNo":
                            return list.OrderByItemNo(isAscending);
                        case "ProjectCode":
                            return list.OrderByProjectCode(isAscending);
                        case "RespRaisedDate":
                            return list.OrderByRespRaisedDate(isAscending);
                        case "FName":
                            return list.OrderByFirstName(isAscending);
                        case "LName":
                            return list.OrderByLastName(isAscending);
                        case "MobileNum":
                            return list.OrderByMobileNumber(isAscending);
                        case "Email":
                            return list.OrderByEmail(isAscending);
                        case "RespDesc":
                            return list.OrderByResponseDescription(isAscending);
                        case "SupportDoc":
                            return list;
    
                        default:
                            return list.OrderByItemNo(isAscending);
                    }
                }
    
                set;  << missing part
    
            }

    But I am getting error for not implementing the set method, what statements should I put in there?

    Update:

    No, actually. The Add of IList is not working

    Tuesday, March 24, 2015 1:45 AM
  • User1918509225 posted

    Hi lucky7456969,

    set; << missing part

    Could you please explain why you need to "set"?

    second ,

    No, actually. The Add of IList is not working

    Do it has any error message ?have you tried to add instance to the list?

    Best Regards,

    Kevin Shen.

    Tuesday, March 24, 2015 9:49 PM
  • User-1993844047 posted

    Hello Kevin,

    I finally found out on the web for how the ObjectDataSource should be correctly dealt with.

    But I am not sure on how this is done.

    Let me show you.

    You know my ItemNo, ProjectCode and SupportDoc can't be of type string. But I have no choices in selecting

    which type of InputParameters I can pass to the ObjectDataSource, any ideas on how to work around this?

    Thanks

    Jack

    protected void OnAddRecord(object sender, EventArgs e)
            {
                // Get the textboxes using the button as the starting point
                Label itemNo = ((Control)sender).Parent.FindControl("NewItemNo") as Label;
                TextBox projectCode = ((Control)sender).Parent.FindControl("NewProjectCode") as TextBox;
                Calendar respRaiseDate = ((Control)sender).Parent.FindControl("NewRespRaisedDate") as Calendar;
                TextBox firstName = ((Control)sender).Parent.FindControl("NewFirstName") as TextBox;
                TextBox lastName = ((Control)sender).Parent.FindControl("NewLastName") as TextBox;
                TextBox mobileNum = ((Control)sender).Parent.FindControl("NewMobileNum") as TextBox;
                TextBox email = ((Control)sender).Parent.FindControl("NewEmail") as TextBox;
                TextBox respDesc = ((Control)sender).Parent.FindControl("NewRespDesc") as TextBox;
                FileUpload supportDoc = ((Control)sender).Parent.FindControl("newSupportDoc") as FileUpload;
    
                try
                {
                    //  No point in adding anything if empty
                    if (!string.IsNullOrWhiteSpace(firstName.Text) || !string.IsNullOrWhiteSpace(lastName.Text))
                    { 
                        // Hack
                        //List<Issue> issueList = IssueLogDataAccessLayer.GetAllIssues();
    
                        Issue issue = new Issue();
                        issue.ItemNo = Data.NextId;
                        issue.ProjectCode = Convert.ToInt32(projectCode.Text);
                        issue.RespRaisedDate = respRaiseDate.SelectedDate;
                        issue.FName = firstName.Text;
                        issue.LName = lastName.Text;
                        issue.MobileNum = mobileNum.Text;
                        issue.Email = email.Text;
                        issue.RespDesc = respDesc.Text;
                        issue.SupportDoc = supportDoc.FileBytes;                     
    
     
                        Data.IssueList.Add(issue);
    
      
                        MainIssueDS.InsertParameters["ItemNo"].DbType = DbType.Int32;
                        MainIssueDS.InsertParameters["ItemNo"].DefaultValue = issue.ItemNo.ToString();
                        MainIssueDS.InsertParameters["ProjectCode"].DefaultValue =
                            issue.ProjectCode.ToString();
                        MainIssueDS.InsertParameters["RespRaisedData"].DefaultValue =
                            issue.RespRaisedDate.ToShortDateString();
    
                        MainIssueDS.InsertParameters["FName"].DefaultValue =
                             issue.FName;
                        MainIssueDS.InsertParameters["LName"].DefaultValue =
                             issue.LName;
                        MainIssueDS.InsertParameters["MobileNum"].DefaultValue =
                            issue.MobileNum;
    
                        MainIssueDS.InsertParameters["Email"].DefaultValue =
                            issue.Email;
                        MainIssueDS.InsertParameters["RespDesc"].DefaultValue =
                            issue.RespDesc;
                        MainIssueDS.InsertParameters["SupportDoc"].DefaultValue =
                            issue.SupportDoc;
                        MainIssueDS.Insert();
    
                         
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            
            }  

    Wednesday, March 25, 2015 2:05 AM