locked
How to get the date from calculated column in list to custom web part RRS feed

  • Question

  • Hi,

    I have created a sharepoint list with calculated column where i am displaying date in format as 21-jul-02Aug.

    I am fetching the same calculated column in my custom web part,But i am receiving Error as below:

    Please help me if any one knows about it and why i am getting error as i stucked here from last 2 days.

    Below is the snapshot of my code and UI

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Data;
    using Microsoft.SharePoint;
    using System.Web;
    
    namespace EventListNew.EventListNew
    {
        public partial class EventListNewUserControl : UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
                DataTable mydata = new DataTable();
                SPSite mySite = SPContext.Current.Site;
                SPWeb myWeb = SPContext.Current.Web;
                SPList myList = myWeb.Lists["Event List"];
    
                SPQuery qry = new SPQuery();
                qry.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                qry.RowLimit = 4;
                SPListItemCollection myitemcol = myList.GetItems(qry);
    
                for (int i = 0; i < myitemcol.Count; i++)
                {
    
                    if (i == 0)
                    {
    
    
                        lblTitle.Text = myitemcol[i]["Title"].ToString();
                        lblDate.Text = myitemcol[i]["DateNew"].ToString();
                        lblDescription.Text = myitemcol[i]["Desc"].ToString();
                        string tempID = myitemcol[i]["ID"].ToString();
                        int TTempID = Convert.ToInt16(tempID);
                        lblTitle.NavigateUrl = "http://172.29.17.212:1989/Lists/Event%20list/DispForm.aspx?ID=" + TTempID;
    
                    }
                    if (i == 1)
                    {
                        lblTitle1.Text = myitemcol[i]["Title"].ToString();
                        lblDate1.Text = myitemcol[i]["DateNew"].ToString();
                        lblDescription1.Text = myitemcol[i]["Desc"].ToString();
                        string tempID = myitemcol[i]["ID"].ToString();
                        int TTempID = Convert.ToInt16(tempID);
                        lblTitle1.NavigateUrl = "http://172.29.17.212:1989/Lists/Event%20list/DispForm.aspx?ID=" + TTempID;
                    }
    
                    if (i == 2)
                    {
                        lblTitle2.Text = myitemcol[i]["Title"].ToString();
                        lblDate2.Text = myitemcol[i]["DateNew"].ToString();
                        lblDescription2.Text = myitemcol[i]["Desc"].ToString();
                        string tempID = myitemcol[i]["ID"].ToString();
                        int TTempID = Convert.ToInt16(tempID);
                        lblTitle2.NavigateUrl = "http://172.29.17.212:1989/Lists/Event%20list/DispForm.aspx?ID=" + TTempID;
    
                    }
    
                    if (i == 3)
                    {
                        lblTitle3.Text = myitemcol[i]["Title"].ToString();
                        lblDate3.Text = myitemcol[i]["DateNew"].ToString();
                        lblDescription3.Text = myitemcol[i]["Desc"].ToString();
                        string tempID = myitemcol[i]["ID"].ToString();
                        int TTempID = Convert.ToInt16(tempID);
                        lblTitle3.NavigateUrl = "http://172.29.17.212:1989/Lists/Event%20list/DispForm.aspx?ID=" + TTempID;
    
                    }
    
    
    
    
                }
            }
        }
    }
    
    

    Friday, August 2, 2013 12:51 PM

Answers

  • Hi Simanchal,

    I have read the snapshot of your code, I suggest as follows:

    1. Replace myitemcol[i]["DateNew"] with myitemcol[i].GetFormattedValue("DateNew").
    2. You have to convert it to the SPCalculatedField type:

       SPFieldCalculated cf = (SPFieldCalculated)myitemcol[i].Fields["DateNew "];
       string value = cf.GetFieldValueForEdit(myitemcol[i]["DateNew"]);
       or
       string value = cf.GetFieldValueAsText(myitemcol[i]["DateNew"]);

    Best regards,

    Dennis Guo




    Monday, August 5, 2013 1:45 PM