I can't seem to get a simple .NET application to recognize a databound control. Basically I have a .NET application, this time in Visual Basic, with an Entity build from an existing database. When I have tags like the following, I can't get it to do anything.
Since I'm between 3 different versions of VS, and I'm trying to use 2017 right now, I'd just need to know what I need to do to databound the textbox below to my database. The entity framework is already there and the field ServiceRequestDate is just a date
field that is in the database. I'm looking for some code that makes the Submit button I'm using take the text box below and update the database. I don't know where the databind needs to be.
<div>
<asp:TextBox ID="tbServiceRequestDate" runat="server" Text='<%# Bind("ServiceRequestDate") %>'></asp:TextBox>
</div>
Code behind (as you can see I'm trying to get this to bind anywhere, or everywhere even though I only want it done when I load the page or when I click Submit):
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
tbServiceRequestDate.DataBind()
Else
tbServiceRequestDate.DataBind()
End If
End Sub
Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
tbServiceRequestDate.DataBind()
End Sub
The Entity Model name is EHSShippingEntities and I already have a database for it. I get the following when attempting to run the code.
System.InvalidOperationException
HResult=0x80131509
Message=Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Source=App_Web_5tsnn3n3
StackTrace:
at ASP.default_aspx.__DataBindingtbServiceRequestDate(Object sender, EventArgs e) in C:\dev\EHSShipping\EHSShipping\Default.aspx:line 14
at _Default.Page_Load(Object Sender, EventArgs e) in C:\dev\EHSShipping\EHSShipping\Default.aspx.vb:line 7
Once I get this one TextBox to work, the rest of this stuff which I haven't looked at in about 10 years will come back to me. I have lots of text boxes on the form I'm actually trying to do and just need this one to work to get all the rest of the controls
on the page to work eventually.
Any help getting the code I'm trying above to do the required update when I click Submit on the page would be greatly appreciated. I just need this one to figure out the rest of them. If you have other questions about my code, please ask and I will get
back with you as soon as possible.
Thank you,
-D