Monday, 2 November 2015

how to fetch values from list and inserted into custom form

private void LoadDefaultData()
        {
            string requestedby = SPContext.Current.Web.CurrentUser.Name;
            string loginid = SPContext.Current.Web.CurrentUser.Email;
            this.txtRequestedBy.Text =requestedby;
            this.txtPRId.Text = String.Format("{0:s}", DateTime.Now).Replace(':', '_');
            this.txtDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            LoadPRForm(loginid, "Employees");

}

 public void LoadPRForm(string loginid, string lstname)
        {

using(SPSite site=new SPSite(SPContext.Current.Web.Url.ToString()))
                {
                    using(SPWeb web=site.OpenWeb())
                    {
                        SPList lst=web.Lists[lstname];
                        SPQuery query=new SPQuery();
                        query.Query = "<Where><Eq><FieldRef Name='UserName'/><Value Type='Text'>" + loginid + "</Value></Eq></Where>";
                        query.ViewFields="<FieldRef Name='Title'/><FieldRef Name='Department'/><FieldRef Name='Company'/>";
                        SPListItemCollection lic = lst.GetItems(query);
                        foreach (SPListItem item in lic)
                        {
                           txtDepartment.Text = item["Department"].ToString();
                           txtCompany.Text = item["Company"].ToString();
                          //hdnEmpNo.Value = item["EmployeeID"].ToString();
                         
                        }
                    }
}

No comments:

Post a Comment