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.ToLong DateString
Dim ddl2 As DropDownList = FormView1.FindControl("Dro pdownlist2 ")
'This will populate the Drop Down List with the store numbers...
Dim connectionString As String = "Data Source=iwcrex1;Initial Catalog=IWCR75COMP01;Persi st Security Info=True;User ID=webuser;Password=passwo rd"
Dim dbcon As New SqlConnection(connectionSt ring)
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
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.ToLong
Dim ddl2 As DropDownList = FormView1.FindControl("Dro
'This will populate the Drop Down List with the store numbers...
Dim connectionString As String = "Data Source=iwcrex1;Initial Catalog=IWCR75COMP01;Persi
Dim dbcon As New SqlConnection(connectionSt
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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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()
From reading the error message I assume you have specified a "SelectedValue" for the dropdown declaratively like:
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.