Link to home
Create AccountLog in
Avatar of Wildone63
Wildone63

asked on

How do I find a control in Formview Insert Mode

Hi,
I am using VS-2008 VB and MS-SQL 2005. I am using the Insert Item Template to do this.

I have an ASPX page with a Formview and a dropdown list on it. My Goal is to populate the dropdown list from a table in a database.

This works IF it is not in a formview.

When I try this I get the error....

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = System.DateTime.Now.ToLongDateString
        Dim ddl2 As DropDownList = FormView1.FindControl("Dropdownlist2")

        'This will populate the Drop Down List with the store numbers...
        Dim connectionString As String = "Data Source=iwcrex1;Initial Catalog=IWCR75COMP01;Persist Security Info=True;User ID=webuser;Password=password"
        Dim dbcon As New SqlConnection(connectionString)
        dbcon.Open()
        Dim dbcmd As New SqlCommand
        dbcmd.CommandText = "select * from arcadr where custno = 'LES-01' order by case when isnumeric(cshipno)=1 then cshipno*1 else 0 end"

        dbcmd.Connection = dbcon
        ddl2.DataTextField = "cshipno"
        ddl2.DataValueField = "id_col"
        ddl2.DataSource = dbcmd.ExecuteReader()
        ddl2.DataBind()
        ddl2.Items.Insert(0, New ListItem("", "NULL"))
        dbcmd.Dispose()
        dbcon.Dispose()

    End Sub
Avatar of carlnorrbom
carlnorrbom
Flag of Sweden image

Hi,

From reading the error message I assume you have specified a "SelectedValue" for the dropdown declaratively like:
        <asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("SomeDataField") %>' />

Open in new window


If so that is what is causing your error. It is most likely caused by the field name not matching or a null value coming through or something like that.

/Carl.
ASKER CERTIFIED SOLUTION
Avatar of mr_nadger
mr_nadger
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer

Dim ddl2 As DropDownList = FormView1.FindControl("DropDownList2")

dbcmd.Connection = dbcon
ddl2.DataTextField = "cshipno"
ddl2.DataValueField = "id_col"
ddl2.DataSource = dbcmd.ExecuteReader()
ddl2.DataBind()
ddl2.Items.Insert(0, New ListItem("", "NULL"))
dbcmd.Dispose()
dbcon.Dispose()

Open in new window