error with webpart
hey all
i have create custom webpart with visual studio i have deployed my web part and it show in my sharepoint site but it gives error (Object reference not set to an instance of an object.:)
here is my code
SPSite site = new SPSite("http://wasi");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["EmployeeList"];
SPListItem item = list.Items.Add();
item["FisrtName"] = TextBox1.Text;
item["LastName"] = TextBox2.Text;
item["SurName"] = TextBox3.Text;
item["FatherName"] = TextBox4.Text;
item.Update();
and my web part code here
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
Control mycontrol;
string error;
public WebPart1()
{
}
protected override void Render(HtmlTextWriter writer)
{
try {
mycontrol.RenderControl(writer);
}
catch(Exception exx)
{
writer.Write(exx.Message + ":" + error);
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
this.Controls.Clear();
mycontrol = this.Page.LoadControl("\\_layouts\\Control.ascx");
this.Controls.Add(mycontrol);
}
catch (Exception e)
{
}
// TODO: add custom rendering code here.
// Label label = new Label();
// label.Text = "Hello World";
// this.Controls.Add(label);
}
}
and when i debuge my code it give that (
System.ArgumentException: Value does not fall within the expected range. )
what is that i dont understand please any one help me regarding this problem
i will be very thankful........
thanks reagrds wasikhan
wasikhan junior software ENG
February 3rd, 2011 12:33pm
Not sure if this is the issue.Check the spelling of the columnname, it's incorrect
item["FisrtName"] = TextBox1.Text;
My SharePoint Blog
http://dhireny.blogspot.com
Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2011 1:07pm
Hi Wasi,
Here are some possible scenarios
1) Permission Issue: This can be a permissions issue as the user running the web part may not have enough permission to the List/Site. You can probably trying using
RunWithElevatedPrivileges if it works for you.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
http://firstblogofvarun.blogspot.com/2009/06/how-to-use-runwithelevatedprivileges-in.html
http://blog.qumsieh.ca/2008/02/28/configure-your-code-to-run-with-elevated-privileges/
2) Check your column name
3) Check the data type of the column (as per your example this should be Single Line of Text)
4) Check the Site name if it correct (http://wasi, try to provide FQDN in the site name)
5) While debugging which line in your code is throwing error.
HTH
Thanks,
Skancharla
February 3rd, 2011 1:42pm