Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
namespace Gridwebpart.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                SPWeb site = SPContext.Current.Web;
                SPList list = site.Lists["GridList"];
                SPListItemCollection items = list.Items;
                DataTable dt = items.GetDataTable();
                Gridnames.DataSource = dt;
                Gridnames.DataBind();

            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SPWeb site = SPContext.Current.Web;
            SPList list = site.Lists["GridList"];
            SPListItemCollection items = list.Items;
            //SPListItem item = list.GetItemById(1);
            SPListItem item = items.Add();
            item["Title"] = txtName.Text;
            item["Description"] = txtDescription.Text;
            item["IS Sharepoint 2010 Developer"] = chkbox.Checked;
            item.Update();
            site.AllowUnsafeUpdates = false;
            txtName.Text = "";
            txtDescription.Text = "";
             chkbox.Checked=false;
             items = list.Items;
             DataTable dt = items.GetDataTable();
             Gridnames.DataSource = dt;
             Gridnames.DataBind();
        }
    }
}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

I am not sure whats best but you can use SPQuery (only for reading) as well to get Data out of sharepoint, however if you want someone to look at your code and improve it then I would suggest you try this question on

http://codereview.stackexchange.com/

hope it helped ;)

Just realized your not disposing your SharePoint objects in code, I would recommend you to look at this MSDN article

Disposing SharePoint Objects

share|improve this answer
1  
added to your answer, you can add an item efficiently by following this blog: blog.robgarrett.com/2009/02/25/… –  Ignacio Aug 29 '12 at 8:01
    
There is no SharePoint objects in his code that he's allowed to dispose. Disposing the SPWeb from SPContext.Current.Web would be an error –  Per Jakobsen Aug 29 '12 at 19:57
    
your right, I just had a quick look before i left that time –  tereško Aug 29 '12 at 20:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.