View Full Version : Best practice for adding new record?
Sergeant 01-14-2006, 07:57 AM I'm trying to get acquainted to .NET (2.0), using VWD Express 2005.
It's not obvious how you'd go about adding a new record. Like I'm looking at my GridView, and a particular person is not loaded...how do I get from there to looking at the GridView with the new person listed in it?
Kodo, I expect you to be chiming in here to make me forget about my old ASP ways and make me see how brilliant ASP.NET is. Don't be shy!
grab some coffee and a bagel
http://www.microsoft.com/events/series/essentialaspnet.mspx
These videos are much better than me typing it all out.
Sergeant 01-14-2006, 11:44 AM OK I'm >halfway through module one.
Very good presentation! I am learning quite a bit already.
I recommend this for anyone who asks those questions about how to put their data on the web.
Thanks for the link Kodo!
btw, in answer to my question...
I didn't imagine that I could put a details view on the same page as the gridview. The details view allows edit and insert methods right on the same page, without writing any code.
OK I'm >halfway through module one.
Very good presentation! I am learning quite a bit already.
I recommend this for anyone who asks those questions about how to put their data on the web.
Thanks for the link Kodo!
btw, in answer to my question...
I didn't imagine that I could put a details view on the same page as the gridview. The details view allows edit and insert methods right on the same page, without writing any code.
Bingo.. and It would be difficult to explain without writing a short essay on it. Better to watch than to have me re-write it :)
Sergeant 01-15-2006, 06:08 AM What if I wanted to use individual text input boxes to add a record?
ok, lets say you have a registration page (there is a control for this but lets pretend otherwise). You can make a class that has all the properties of the registration and when you submit, it creates an object and populates these properties and then sends this object to a data layer which contains the sql commands to insert into the database. OR you can just pass the text straight to the data layer insert method (that you create). I vote for a class this way you can encapsulate any other necessary logic into the class before you pass it to the data layer class.
Your data layer class is just another class like
Namespace SergeantDataLayer
public class DataAcess
public function InsertRegistration(byval Reg as Registration)
'do sql commands here
end function
end class
End Namespace
Sergeant 01-15-2006, 08:17 AM Not very good with layers and classes.
Playing around with it, I got the detailsView to do what I need for now. I set its default view to Insert.
One thing I can't figure is this:
How can I pre-fill a date field with today's date (default value), but still allow the user to enter a date.
for classes, take a look at my examples in the VB.NET forum.
As for default values for the FormDetail you'll need to go into code to do this.
Protected Sub FormView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
Dim ThisControl As TextBox = FormView1.FindControl("StartDate")
If ThisControl.Text = String.Empty Then
ThisControl.Text = Today
End If
End Sub
Sergeant 01-15-2006, 11:05 AM I just tried that (modified for DetailsView1), and it didn't work.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
It's not really clear how to refer to the control.<asp:BoundField DataField="NewsDt" HeaderText="Date" SortExpression="NewsDt" />
Here is what I tried (In my code-behind page MyPage.aspx.vb)...
Protected Sub detailsView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.PreRender
Dim ThisControl As TextBox = DetailsView1.FindControl("NewsDt")
If ThisControl.Text = String.Empty Then
ThisControl.Text = Today
End If
End Sub
In the detailsview control, I have changed the field header to "Date", doubt if it matters.
that means it can't find the control in the FindControl method because you're using a bound field. Make that field into a template column and and set the ID to NewsDt and then try the code again.
Sergeant 01-15-2006, 01:42 PM OK, I added a templateField TextBox to the DetailsView, and I got it to pre-fill with today's date. Don't know how to use that textbox in the insert action. There is no option for dataBinding of the text box.
when you changed it to a template, it should have put <%#Bind("NewsDt")%> for the text value. If you go to the insert item template edit view in the designer, and you click on the text box for the NewsDt field you should see a little black arrow on the top right corner of the textbox control. Click on that and you should be able to edit the databinding properties and you should see Bind("NewsDt") for the custom databound text value.
one more thing. If you plan on going from edit (readonly) to insert mode then you will get an error so you should check the mode of the detailsview by doing this:
If DetailsView1.CurrentMode = DetailsViewMode.Insert
'do code
'end if
because the NewDt textbox control doesn't exist in the readonly mode.
Sergeant 01-15-2006, 02:58 PM Kodo, you've been such a big help!
I added the TemplateField TextBox in source view, so it didn't add the Bind thingy.
I went in and added the text property of ...
Text=<%#Bind("NewsDt")%>
...and it works fine now.
I miss writing ASP code already (I am a problem solver by nature), but I think I could quite get into this.
BTW, this DetailsView would only be for adding records, so no worries on the CurrentMode.
BTW, this DetailsView would only be for adding records, so no worries on the CurrentMode.
that's cool, but you should know about it anyway :)
I tell you I had a hard time moving from ASP to .NET as well because I had direct interaction with everything displayed. I miss that in a sense but the interaction level has been moved into a control object and once you get a hang of the controls, it's so much easier to build sites. For example, I can build a user control (like an include) hook up it's properties and drag and drop that sucker on a page. It's just so much faster:) and I do love the code behind (separation of presentation and code) because I surely do not miss spaghetti code at all!
johnlbw 08-11-2006, 12:54 AM I spent ages trying to find out where I had gone wrong and then noticed this
Protected Sub detailsView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.PreRender
I double clicked my details view to go to codebehind and chose the databinding event - and you get
Protected Sub detailsView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.databinding
but its all working now :D
|