473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

DataSource and FormView questions

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.SqlDataSourceStatusEvent Args) 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
Aug 8 '06 #1
2 2753
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.SqlDataSourceStatusEvent Args) 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
Aug 8 '06 #2
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" <cl*****@discussions.microsoft.comwrote in message
news:E3**********************************@microsof t.com...
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:

Aug 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rabbit | last post by:
Hi, All, I have a FormView, in which the InsertItemTemplate has a dropdownlist that I need to set the datasource as my self created dataset. I have been trying various syntax like following at...
1
by: sck10 | last post by:
Hello, I have a GridView that has AutoGenerateSelectButton="true". When the "Select" link is clicked, it opens a FormView with the appropriate data. Below is the following SelectParameter that...
3
by: Mike Gaab | last post by:
I am trying to use some of the new controls in VS 2005 but am having some difficulty. My business logic layer returns Collections of my business objects. I am used to just assigning these...
2
by: H5N1 | last post by:
Hi there First of all excuse me posting such simple (I guess) question, but I didn't find the answer in tutorials. I have a formView presenting records from some table. one of the fields is...
2
by: myzm | last post by:
Hi, I have a FormView looks like this <asp:FormView> <ItemTemplate> <asp:Table> <asp:TableRow> <asp:TableCell> <asp:TextBox ID="textBox1"> </asp:TableCell>
1
by: cisco | last post by:
I've been trying to figure out how the client of a DetailsView or FormView should handle the ItemUpdating event when setting the datasource programatically. I want to do something simple like...
0
by: TJHerman | last post by:
I have a dropdown list in a Formview that includes a number of fields in the SQLDatasource. I want to be able to get the value of one of the fields in the Datasource that is not the...
0
by: Tarren | last post by:
Hi: I have a formview and I want a gridview to show at the bottom of the page beneath the formview. The thing is that the datasource for the gridview had ControlParameters which need to pull...
5
by: Ken Varn | last post by:
I have just started using VS.NET 2005 after using VS.NET 2003. One of the things that I noticed is that in ASP.NET, the DataSource property for TextBoxes and other web controls is no longer...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.