all groups > asp.net webcontrols > february 2006 >
You're in the

asp.net webcontrols

group:

Dependent Dropdownlists


Dependent Dropdownlists sanderscp
2/28/2006 5:25:26 AM
asp.net webcontrols:
I know this topic has probably been talked about to death...however I'm
having a problem with my dependent dropdownlists in the edittemplates within
a detailsview. I am trying to databind my dropdownlists once the selected
index has changed of the first dropdownlist has changed. The update of the
other dropdownlist works nicely, however, when I click update, I get the
following error:

"Cannot insert the value NULL into column 'dply_team_mbr_id', table
'25_Dev.dbo.EQP_ASGN'; column does not allow nulls. UPDATE fails.
The statement has been terminated."
From the error it appears the value of the dropdownlist is not being
translated to the field value of dply_team_mbr_id. How do I bind the
dropdownlist to that value? Thanks!

Here is my code:

protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
Label lbl = DetailsView1.FindControl("LblFirstHide") as Label;
Label lblFirst = DetailsView1.FindControl("Label5") as Label;
Label lblLast = DetailsView1.FindControl("Label6") as Label;
DropDownList ddlTeam = DetailsView1.FindControl("ddlTeamID") as
DropDownList;

lbl.Text = ((DropDownList)sender).SelectedValue;

//SQL Connection string connects to 25_Dev
SqlConnection objCon = new SqlConnection("Data
Source=SQLTES;Initial Catalog=25_Dev;Integrated Security=True");

SqlDataAdapter da = new SqlDataAdapter("SELECT [empid],
[FirstName], [LastName] FROM [EMPLOYEES] WHERE empid='"+lbl.Text+"'", objCon);

DataSet ds = new DataSet();
da.Fill(ds);

//update first and last name in edititemtemplate of detailsview
lblFirst.Text = ds.Tables[0].Rows[0]["FirstName"].ToString();
lblLast.Text = ds.Tables[0].Rows[0]["LastName"].ToString();

SqlDataAdapter dB = new SqlDataAdapter("SELECT
DPLY_TEAM_MBR.dply_id, DPLY_TEAM_MBR.dply_team_mbr_id, DPLY_TEAM_MBR.empid,
DPLY.dply_team_name FROM DPLY_TEAM_MBR INNER JOIN DPLY ON
DPLY_TEAM_MBR.dply_id = DPLY.dply_id WHERE DPLY_TEAM_MBR.empid = '" +
lbl.Text + "'", objCon);
DataSet dsB = new DataSet();
dB.Fill(dsB);

ddlTeam.DataSource = dsB.Tables[0].DefaultView;
ddlTeam.DataTextField = "dply_team_name";
ddlTeam.DataValueField = "dply_team_mbr_id";
ddlTeam.DataBind();
RE: Dependent Dropdownlists Phillip Williams
2/28/2006 8:35:02 AM
You have posted the code that selects the records. The error you got however
is referring to updating the record with a null value. Check within the code
that you wrote for handling the DetailsView.ItemUpdating event, e.g.
void DetailsView1_ItemUpdating(Object sender,
DetailsViewUpdateEventArgs e)
{
//your code for implementing this handler
//then you must be setting the updated value to the dropdownlist
selectedvalue
//this is probably where the program is complaining that you are setting
a null
e.NewValues["dply_team_mbr_id"] = ddlTeam.SelectedValue;
//if so, then step through the code with a break point on this line to
see what value
//are you setting in this field
}

For a complete working sample:
http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


[quoted text, click to view]
AddThis Social Bookmark Button