Custom Webpart – Inserts items into sharepoint custom list

Here is the simple custom webpart which inserts items into (Title, Employee Name, Designation) Sharepoint custom list, before executing the webpart first of all you have to create custom list and required fields manually through UI as said below:

Create a custom list, name it as Custom List (you can also choose different name, but make sure to modify the same in the code too) then create columns as mentioned…..

Title [Single line text] (this is by default available, no need of re-creating)

Employee Name [Single line text]

Designation [Single line text]

Insert Into Custom List - Input Form

Insert Into Custom List – Input Form

Download complete source code

Insert Into Custom List - Record Inserted

Insert Into Custom List – Record Inserted

List view displays records after inserting

List view displays records after inserting

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace InsertIntoList
    public class InsertIntoList : System.Web.UI.WebControls.WebParts.WebPart
        TextBox oTextTitle;
        TextBox oTextName;
        TextBox oTextDesignation;
        Label oLabelMessage;
        Button oButtonSubmit;
        protected override void CreateChildControls()

            oTextTitle = new TextBox();

            oTextName = new TextBox();

            oTextDesignation = new TextBox();

            oLabelMessage = new Label();

            oButtonSubmit = new Button();
            oButtonSubmit.Text = "Submit";
            oButtonSubmit.CssClass = "ms-ButtonHeightWidth";

            oButtonSubmit.Click += new EventHandler(oButtonSubmit_Click);

        void oButtonSubmit_Click(object sender, EventArgs e)
            if (oTextTitle.Text.Trim() == "" || oTextName.Text.Trim() == "" || oTextDesignation.Text.Trim() == "")
                oLabelMessage.Text = "You must specify a value for this required field";
                SPSite mySite = SPContext.Current.Site;
                SPWeb myWeb = SPContext.Current.Web;
                SPList myList = myWeb.Lists["Custom List"];
                SPListItem myListItem = myList.Items.Add();
                myListItem["Title"] = oTextTitle.Text.ToString();
                myListItem["Employee Name"] = oTextName.Text.ToString();
                myListItem["Designation"] = oTextDesignation.Text.ToString();
                myWeb.AllowUnsafeUpdates = true;
                myWeb.AllowUnsafeUpdates = false;
                oLabelMessage.Text = "Recorded inserted";