Skip Ribbon Commands
Skip to main content

Robin | zevenseas | SharePoint Blog

:

The zevenseas Community > Blogs > Robin | zevenseas | SharePoint Blog > Posts > Fields hidden from the NewForm should have defaultValues set otherwise..
October 21
Fields hidden from the NewForm should have defaultValues set otherwise..

When querying on them you don’t get the results you were hoping for.. well at least I didn’t. Let me tell you what I was doing and what problems I discovered .. To begin, I created a custom list with some columns that I marked as hidden on the newform and the editform page using this piece code..

Guid listGuid = createdWeb.Lists.Add("AcceptedList", "", SPListTemplateType.GenericList)
SPList list = createdWeb.Lists[listGuid];
customList.Fields.Add("Accepted", SPFieldType.Boolean, false);
list.Update();

SPField field = customList.Fields["Accepted"];
field.ShowInNewForm = false;
field.ShowInViewForms = true;
field.ShowInEditForm = false;
field.Update(); 

Next thing I wanted to do was having two SPGridView’s, one showing all the items where the custom field was set to ‘true’ and the other one was set to ‘false’. To gather the items I’m using the SPDataSource using a CAML query to retrieve them.

ds = new SPDataSource();
ds.SelectCommand = "<Query><Where><Eq><FieldRef Name='Accepted' /><Value Type='Bool'>0</Value></Eq></Where><OrderBy /></Query>";
ds.List = currentWeb.Lists[listGuid]; 
ds.DataSourceMode = SPDataSourceMode.List;

ds.EnableViewState = true;
ds.IncludeHidden = true;
ds.Scope = SPViewScope.Recursive;

gridViewAccepted.DataSource = ds;
gridViewAccepted.DataBind();

ds = new SPDataSource();
ds.SelectCommand = "<Query><Where><Eq><FieldRef Name='Accepted' /><Value Type='Bool'>1</Value></Eq></Where><OrderBy /></Query>";
ds.List = currentWeb.Lists[listGuid]; ;
ds.DataSourceMode = SPDataSourceMode.List;

ds.EnableViewState = true;
ds.IncludeHidden = true;
ds.Scope = SPViewScope.Recursive;

gridViewNotAccepted.DataSource = ds;
gridViewNotAccepted.DataBind();

Now when I added new items, nothing appeared in my two Grid’s.. very weird because I specifically queried for both options right? I decided to remove the whole SelectCommand line and all of a sudden the newly added items appeared in both Grid’s! Aargh.. I tried to go through the items using a foreach loop and guess what.. it crashed! It gave the well known ‘"Object reference not set to an instance of an object."’ error. How come? Well.. it seems that when you define a field as hidden in the newform and you didn’t specify a default value for the field, the value of the field is not set.

How to solve this? Just by simply adding the defaultvalue of the field to the code like this :

field.DefaultValue = "0";
field.ShowInNewForm = false;
field.ShowInViewForms = true;
field.ShowInEditForm = false;
field.Update(); 

Hope this helps someone ;)

Comments

Waldek Mastykarz

Have you tried setting Nullable="TRUE" at FieldRef Name="Accepted"?
Robin MeureNo presence information on 16/11/2008 07:41

Robin

Nope.. haven't tried it. Gonna do it now! :)
Robin MeureNo presence information on 16/11/2008 07:41
 

 Statistics

 
Views: 1638
Comments: 2
Tags:
Published:1645 Days Ago