Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding DropDownListValue to ListView insert event

Status
Not open for further replies.

stinkybee

Programmer
May 15, 2001
218
GB
For a new site I am developing I have a listview which has a dropdownlist populated from another datasource. I need to pass the value of this dropdownlist to the insert event of the listview. I have done this previously in VB.NET with the following code so my question is simply, can anyone convert this into C#?

Code:
Sub insertRecipeIngredient(ByVal sender As Object, ByVal e As ListViewInsertEventArgs)
        Dim ddl As DropDownList
        ddl = e.Item.FindControl("dd_ingredientInsert")
        If Not IsDBNull(ddl) Then
            e.Values("ddl") = ddl.SelectedValue
            SqlDataSource4.InsertParameters(0).DefaultValue = ddl.SelectedValue
        End If
End Sub

Thanks
 
Ok, thanks for the tip, I hadn't actually thought about converters. I have tried to put it through several but none seem to work. Here is the code after converting:

Code:
void insertRecipeIngredient(object sender, ListViewInsertEventArgs e) {
        DropDownList ddl;
        ddl = e.Item.FindControl("dd_ingredientInsert");
        if (!IsDBNull(ddl)) {
            e.Values["ddl"] = ddl.SelectedValue;
            SqlDataSource4.InsertParameters(0).DefaultValue = ddl.SelectedValue;
        }
}

it throws up this error:

Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.DropDownList'.

on this line
Code:
ddl = e.Item.FindControl("dd_ingredientInsert")

Anyone else able to convert this?
 
that's right. you need to explicitly convert the object.
Code:
var ddl = e.Item.FindControl("dd_ingredientInsert") as DropDownList;
if(ddl is null) return;

var value = ddl.SelectedValue;
e.Values["ddl"] = value
SqlDataSource4.InsertParameters(0).DefaultValue = value;

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for that. Just needed to change the isdbnull check and change () for [] an now it works. The full code is:

Code:
void insertRecipeIngredient(object sender, ListViewInsertEventArgs e) {
   		
    var ddl = e.Item.FindControl("dd_ingredientInsert") as DropDownList;
	    if (System.Convert.IsDBNull(ddl)) return;
	    var value = ddl.SelectedValue;
	    e.Values["ddl"] = value;
	    SqlDataSource4.InsertParameters[0].DefaultValue = value;
	    
    }

Thanks again.
 
your logic will always be false DbNull is not null, they are 2 very different things. null is null; nothing; un-quantifiable. DbNull is an object that represents null from the database.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top