DataSource and FormView questions

Discussion in 'ASP .Net' started by Kelly, Aug 8, 2006.

  1. Kelly

    Kelly Guest

    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
    Kelly, Aug 8, 2006
    #1
    1. Advertising

  2. 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" wrote:

    > 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
    >
    >
    >
    =?Utf-8?B?Y2xpY2tvbg==?=, Aug 8, 2006
    #2
    1. Advertising

  3. Kelly

    Kelly Guest

    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


    "clickon" <> wrote in message
    news:...
    > 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" wrote:
    >
    >> 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:
    >>
    Kelly, Aug 9, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nate Hekman

    asp.net 2.0, datasource/formview question

    Nate Hekman, Dec 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    474
    bruce barker
    Dec 29, 2004
  2. Rabbit
    Replies:
    0
    Views:
    1,708
    Rabbit
    Mar 20, 2006
  3. sck10

    FormView datasource (asp.net 2.0)

    sck10, Mar 29, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    5,135
    Steven Cheng[MSFT]
    Mar 30, 2006
  4. Mike Gaab

    formview and datasource as collection

    Mike Gaab, Apr 2, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    2,069
    Mike Gaab
    Apr 3, 2006
  5. Stuart Ferguson
    Replies:
    1
    Views:
    4,977
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Apr 3, 2006
Loading...

Share This Page