Solved

How do I find a control in Formview Insert Mode

Posted on 2010-11-16
3
574 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:Wildone63
3 Comments
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 34153099
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.
0
 
LVL 7

Accepted Solution

by:
mr_nadger earned 500 total points
ID: 34154296

try
        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"
        ctype(FormView1.FindControl("Dropdownlist2"),dropdwn).DataBind()

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

Expert Comment

by:aibusinesssolutions
ID: 34155875

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

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 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