DataSource and FormView questions

K

Kelly

I'm fairly new to ASP.NET2, but I have an ASP 3.0 background, and I've been
experimenting with the DataSource and FormView widgets. I wound up having
some questions along the way which I hope you all can address to help me
understand events with these better.

I've been reading up on inserted/inserting also deleted/deleting, but where
is it better to run call those events from via formview or datasource?

Also, I have been trying to figure out how to do you compare in the database
before posting.

I've figured that I would have to run the select() sub function while in the
inserting() sub function, but I just can't figure out how to call it so I
can compare what the user has entered before writing to the database.

Ideas?

I'm using mysql odbc connector and the code looks like this:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:LocalConnection %>"
ProviderName="<%$ ConnectionStrings:LocalConnection.ProviderName %>"
InsertCommand="INSERT INTO newsletter_TBL(ID, name, email, serialno, Verify)
VALUES ('', ?, ?, ?, 0)"
SelectCommand="SELECT `email` FROM `newsletter_TBL` WHERE theEmail = email">
<SelectParameters>
<asp:FormParameter FormField="emailTextBox" Name="theEmail" />
</SelectParameters>
</asp:SqlDataSource>

<asp:FormView ID="FormView1" CssClass="signuptable" runat="server"
DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnPageIndexChanging="FormView1_PageIndexChanging"
OnInserting="FormView1_ItemInserting" DefaultMode="Insert" Width="288px"
Height="251px" CellPadding="10">

<InsertItemTemplate>
<span class="signuplabel">name:</span><br />
<asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'
Width="250px" AutoCompleteType="FirstName"></asp:TextBox><br />
<span class="signuplabel">email:</span><br />
<asp:TextBox ID="emailTextBox" runat="server" Text='<%# Bind("email") %>'
Width="250px" AutoCompleteType="Email"></asp:TextBox><br />

<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server" ControlToValidate="emailTextBox"
ErrorMessage="Invalid email, please try again"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Width="250px"></asp:RegularExpressionValidator>
<asp:TextBox ID="serialnoTextBox" runat="server" Text='<%# Bind("serialno")
%>' Visible="False" Height="0px"
OnLoad="serialnoTextBox_Load">MakeRandom()</asp:TextBox><br />
<asp:Button ID="InsertButton" runat="server" CausesValidation="true"
CommandName="Select" Text="Sign me up"
UseSubmitBehavior="false" OnClick="InsertButton_Click" />
</InsertItemTemplate>
</asp:FormView>

.... then on the attached code page I have this

Protected Sub InsertButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

Try
SqlDataSource1.Insert()
Catch except As Exception
'MsgBox(except.Message)
End Try
End Sub

Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewInsertEventArgs) Handles
FormView1.ItemInserting

Dim MessageLabel As String = ""
Dim itemArray(e.Values.Count - 1) As DictionaryEntry
e.Values.CopyTo(itemArray, 0)
Dim entry As DictionaryEntry
For Each entry In itemArray
MessageLabel &= entry.Key.ToString() & "= " & entry.Value & "<br/>"
Next
MsgBox(MessageLabel)
End Sub

Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles
SqlDataSource1.Selected
MsgBox("Select just ran + " & e.AffectedRows)
End Sub

Finally, I've been looking for information on <%%> tags because I've noticed
they've changed. You either do <%$ %> or <%# %> what are the new specialized
tag versions? Did the standard <% %> tags go away?



Thanks,



Kelly
 
G

Guest

First piece of advice i would give you is try to forget how you did
everything in classic ASP, asp.net kind of work back to front in comparison.
Aslo you should try to avoid inline code in <% %> tags as much as possible, i
only ever really use them for two way data binding in TemplateFields.

You don't call events they just happen, and you hook them up to event
handlers, where you execute code. E.g. You can't call a page load event to
load a page, you load a page by clicking a link, typing the URL in the
address bar etc, then the code in the page load event handler executes. You
are confusing events with object methods, which you do call.

If you have defined an InsertCommand proerty for your SQLDataSource, and you
are entering the data using a FormView then you do not need to call the
insert() method of the SQL datasource to insert the data, you simply need a
LinkButton or similar with a CommandName="Insert" property. When this button
is clicked the insert method of the SQLDataSource will be called
automatically without writing any code. The Inserting event will occur just
before the data is inserted and the Inserted event just after. If you want
to check something before the data goes in, wire up an Inserting event
handler to the SQLDataSource and put your code in there.

DataSource controls are really for binding data to data bound controls, if
you want to check a value in the database prior to inserting any new data,
you should really use ADO.net data acess methods.



Kelly said:
I'm fairly new to ASP.NET2, but I have an ASP 3.0 background, and I've been
experimenting with the DataSource and FormView widgets. I wound up having
some questions along the way which I hope you all can address to help me
understand events with these better.

I've been reading up on inserted/inserting also deleted/deleting, but where
is it better to run call those events from via formview or datasource?

Also, I have been trying to figure out how to do you compare in the database
before posting.

I've figured that I would have to run the select() sub function while in the
inserting() sub function, but I just can't figure out how to call it so I
can compare what the user has entered before writing to the database.

Ideas?

I'm using mysql odbc connector and the code looks like this:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:LocalConnection %>"
ProviderName="<%$ ConnectionStrings:LocalConnection.ProviderName %>"
InsertCommand="INSERT INTO newsletter_TBL(ID, name, email, serialno, Verify)
VALUES ('', ?, ?, ?, 0)"
SelectCommand="SELECT `email` FROM `newsletter_TBL` WHERE theEmail = email">
<SelectParameters>
<asp:FormParameter FormField="emailTextBox" Name="theEmail" />
</SelectParameters>
</asp:SqlDataSource>

<asp:FormView ID="FormView1" CssClass="signuptable" runat="server"
DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnPageIndexChanging="FormView1_PageIndexChanging"
OnInserting="FormView1_ItemInserting" DefaultMode="Insert" Width="288px"
Height="251px" CellPadding="10">

<InsertItemTemplate>
<span class="signuplabel">name:</span><br />
<asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'
Width="250px" AutoCompleteType="FirstName"></asp:TextBox><br />
<span class="signuplabel">email:</span><br />
<asp:TextBox ID="emailTextBox" runat="server" Text='<%# Bind("email") %>'
Width="250px" AutoCompleteType="Email"></asp:TextBox><br />

<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server" ControlToValidate="emailTextBox"
ErrorMessage="Invalid email, please try again"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Width="250px"></asp:RegularExpressionValidator>
<asp:TextBox ID="serialnoTextBox" runat="server" Text='<%# Bind("serialno")
%>' Visible="False" Height="0px"
OnLoad="serialnoTextBox_Load">MakeRandom()</asp:TextBox><br />
<asp:Button ID="InsertButton" runat="server" CausesValidation="true"
CommandName="Select" Text="Sign me up"
UseSubmitBehavior="false" OnClick="InsertButton_Click" />
</InsertItemTemplate>
</asp:FormView>

.... then on the attached code page I have this

Protected Sub InsertButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

Try
SqlDataSource1.Insert()
Catch except As Exception
'MsgBox(except.Message)
End Try
End Sub

Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewInsertEventArgs) Handles
FormView1.ItemInserting

Dim MessageLabel As String = ""
Dim itemArray(e.Values.Count - 1) As DictionaryEntry
e.Values.CopyTo(itemArray, 0)
Dim entry As DictionaryEntry
For Each entry In itemArray
MessageLabel &= entry.Key.ToString() & "= " & entry.Value & "<br/>"
Next
MsgBox(MessageLabel)
End Sub

Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles
SqlDataSource1.Selected
MsgBox("Select just ran + " & e.AffectedRows)
End Sub

Finally, I've been looking for information on <%%> tags because I've noticed
they've changed. You either do <%$ %> or <%# %> what are the new specialized
tag versions? Did the standard <% %> tags go away?



Thanks,



Kelly
 
K

Kelly

Clickon,

Thanks! I remember the rule about ASP tags.

Towards your ADO response. This is where I'm kind of lost. I'm having a hard
time making that separation from widgets to background code. In this
particular case with the SQLdatasource, would I try to call the ado
datareader (or similar) within the inserting event? Also, how does one grab
the item from the page that I need to compare to? I'm slowly grasping this.

Thanks for all your help click on,

Kelly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top