Is there any best way to insert and retrieve data from sp list
-
09-12-2019 - |
Question
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();
}
}
}
Solution
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
https://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
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange