Friday, 10 February 2012

ADDING LIST ITEM


 public partial class listdataUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            listadd();
        }
        public void listadd()
        {
            using (SPSite osite = new SPSite(SPContext.Current.Web.Site.ID))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    try
                    {
                        SPList olist = oweb.Lists.TryGetList("Training");
                        SPListItem oitem = olist.Items.Add();
                        oitem["Title"] = "listdata";
                        oitem["Sex"] = "Anand";
                        oitem["Age"] = "22";
                        oitem["Qualification"] = "BE";
                        oweb.AllowUnsafeUpdates = true;
                        oitem.Update();
                        olist.Update();
                        oweb.AllowUnsafeUpdates = false;
                        SPQuery oqry = new SPQuery();
                        oqry.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>Kumar</Value></Eq></Where>";

                        grd.DataSource = olist.GetItems(oqry).GetDataTable();
                        grd.DataBind();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }

No comments:

Post a Comment