How do I find a control in Formview Insert Mode

Posted on 2010-11-16
Last Modified: 2012-05-10
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)
        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.Items.Insert(0, New ListItem("", "NULL"))

    End Sub
Question by:Wildone63
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 18

Expert Comment

ID: 34153099

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.


Accepted Solution

mr_nadger earned 500 total points
ID: 34154296

        dbcmd.Connection = dbcon
        ctype(FormView1.FindControl("Dropdownlist2"),dropdwn).datasource= dbcmd.ExecuteReader()
        ctype(FormView1.FindControl("Dropdownlist2"),dropdwn).DataTextField = "cshipno"
        ctype(FormView1.FindControl("Dropdownlist2"),dropdwn).DataValueField = "id_col"

this should force the code to find the right control on the page, if not could you post the html as well please
LVL 15

Expert Comment

ID: 34155875

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

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

Open in new window


Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question