JAW73

Okay, I've been at this all day and I have to be missing something. I'm working with WSS 3.0 and VS 2005

1. I created a SPList called "Audit Name List."

2. I added 1 column called "Audit Name."

3. I created a web part that should update the list but it just doesn't. I'm pulling my hair out! (as if I had any)

Any help would be GREATLY appreciated!

Here is my code:

---------------------------------------------------------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace CustomWebParts
{
public class ContactsSettings : WebPart

{
public ContactsSettings() { }

protected DropDownList ddlAuditNames;
protected Button btnSave;

protected override void CreateChildControls()
{
base.CreateChildControls();
ddlAuditNames = new DropDownList();
ddlAuditNames.Items.Add("AuditName1");
ddlAuditNames.Items.Add("AuditName2");


btnSave = new Button();
btnSave.Text = "Save";
btnSave.Click += new EventHandler(btnSave_Click);
}

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.Write("<table>");
writer.Write("<tr>");
writer.Write("<td>");
ddlAuditNames.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td>");
btnSave.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</table>");
}

void btnSave_Click(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
SPList auditNameList = web.Lists["Audit Name List"];

SPListItem newAuditNameEntry = auditNameList.Items.Add();
newAuditNameEntry["Audit Name"] = ddlAuditNames.SelectedValue; //even tried to hard code something here
newAuditNameEntry.Update();
auditNameList.Update();
}

}
}

---------------------------------------------------------------------------------------------------------------------------------



Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

smc750

Are you missing any "required" fields






Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

DKeeling

You do not need: auditNameList.Update();

This would make an update to your list (like its columns etc.)

Are you getting an error message, like allow unsafe updates are not turned on

You might have to wrap you item update in,

web.AllowUnSafeUpdates = true;

newAuditNameEntry.Update();

web.AllowUnSafeUpdates = false;

I would also suggest to wrap most processing into a try-catch block, and writer.Write(exception) just in case.






Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

JAW73

Thanks so much for your reply. I tried as you suggested:

1. I removed auditNameList.Update();
2. I wrapped the Update

3. I added a try/catch

..to no avail. It still does not update the list and I do not get an error. Below is my code:

void btnSave_Click(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
SPList auditNameList = web.Lists["Audit Name List"];

SPListItem newAuditNameEntry = auditNameList.Items.Add();
newAuditNameEntry["Title"] = "some title";
newAuditNameEntry["Audit Name"] = "some value";
//newAuditNameEntry["Created By"] = "someone";
//newAuditNameEntry["Modified By"] = "someone";
try
{
web.AllowUnsafeUpdates = true;
newAuditNameEntry.Update();
web.AllowUnsafeUpdates = false;
//auditNameList.Update();

}
catch (Exception)
{
lblError.Visible = true;
lblError.Text = "Error";
}
}





Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

DKeeling

I have a feeling now that the event is not being fired, or the eventhandler btnSave_Click is not catching the event. Try commenting most of the code out and just have a Response.Write("I caught the event"); to make sure that this is truely catching the button click event.






Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

JAW73

I feel silly for asking this but when I use "response.write" I get "Response does not exist in the current context." I see what you mean I just don't know how to get something to output here. How can I output

void btnSave_Click(object sender, EventArgs e)

{

Response.write("Event Fired!");

}

I'm also going to try to place this control inside a WebPartManager so that I can try testing it outside of sharepoint. Thanks for your help!





Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

mikes0n

HttpContext.Current.Response.Write(...);





Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

JAW73

Thanks for the post did as you suggested, and guess what, the event is NOT firing:

protected override void CreateChildControls()
{
base.CreateChildControls();
ddlAuditNames = new DropDownList();
ddlAuditNames.Items.Add("Conagra07");
ddlAuditNames.Items.Add("ActiveMedia");

lblError = new Label();
lblError.Visible = false;

btnSave = new Button();
btnSave.Text = "Save";
btnSave.Click += new EventHandler(btnSave_Click);
}

//I get no output when I click

void btnSave_Click(object sender, EventArgs e)
{
HttpContext.Current.Response.Write("Event Was Fired");

}

Any ideas





Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

JAW73

Thanks mikesOn, that got me going. I have determined that my event is not firing. Thanks your input.





Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

DKeeling

Try adding a base.render in your overridden render method, you might also need an initialize function (setup an ID etc. for some controls.)






Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

DKeeling

Actually you still need to add the new control to the controls collection I think,

Try this:

Code Snippet

protected override void CreateChildControls()
{
base.CreateChildControls();
ddlAuditNames = new DropDownList();
ddlAuditNames.Items.Add("AuditName1");
ddlAuditNames.Items.Add("AuditName2");

ddlAuditNames.ID = "ddlAuditNames";

Controls.Add(ddlAuditNames);

btnSave = new Button();

btnSave.ID = "saveButton";

btnSave.Text = "Save";

btnSave.Click += new EventHandler(btnSave_Click);

Controls.Add(btnSave);

}

And see if you get better results.




Re: SharePoint - Development and Programming !! Updating List Data -- Please Help!!

JAW73

Thanks so much for your time and effort! That was exactly the problem. I have it working now!