ASP NET 2 0 (DOC download)

Document Sample
ASP NET 2 0 (DOC download) Powered By Docstoc
					<html>
<form id="form1" runat="server">
hi
<asp:DropDownList ID="d1" runat="server" AutoPostBack="True" DataSourceID="s1"
DataTextField="CompanyName" DataValueField="CustomerID" />
<asp:SqlDataSource ID="s1" runat="server"
ConnectionString="server=localhost;uid=sa;pwd=;database=Northwind"
SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers]" />
<asp:GridView ID="GridView1" runat="server" DataSourceID="s2" />
<asp:SqlDataSource ID="s2" runat="server"
ConnectionString="server=localhost;uid=sa;pwd=;database=Northwind"
SelectCommand="SELECT * FROM [Orders] WHERE [CustomerID] =
@CustomerID">
<SelectParameters>
<asp:ControlParameter ControlID="D1" Name="CustomerID"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
In the above program we first create a drop down list box that gets its data from a sql
data source control called s1. From now on we do not write C# code for fetching data, we
use a sql data source control instead. The AutoPostBack property is set to true so that
each time we choose a different item from the list box, the server should be notified. The
sql data source control s1 has a connection string property which is the same as what we
supplied to the constructor of the sqlconnection class. The select command is what
supplies the data to the data aware controls. A grid view control is also like a data grid,
they display data. The grid view gets its data from the sql data source s2 which selects
data from the orders table with a where clause. In the where clause, any word beginning
with the @ is a variable or a parameter that needs to be created by us. We use the Select
Parameters and the control parameter tags to create a parameter CustomerID. This
parameter gets its value from the list box d1 and the value will be the value of the
property SelectedValue. Thus each time we change the value of the list box, a new set of
data is displayed in the grid view.

<html>
<form runat="server">
hi1
<asp:TextBox ID="t1" runat="server" AutoPostBack="True"/>
<asp:TextBox ID="t2" runat="server" AutoPostBack="True" />
<asp:GridView runat="server" DataSourceID="s1" />
<asp:SqlDataSource ID="s1" runat="server" ConnectionString="Data
Source=localhost;Initial Catalog=Northwind;User ID=sa"
  SelectCommand="SELECT * FROM [Orders] WHERE [CustomerID] = @CID AND
[EmployeeID] >= @EID">
<SelectParameters>
<asp:ControlParameter ControlID="t1" Name="CID" Type="String" />
<asp:ControlParameter ControlID="t2" Name="EID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</html>
The next example is similar to the earlier one, except that the data comes from two text
boxes whose autopostback property is true. The text boxes are of data type string and
int32. The select statement can be as complex as we want it to be.

<html>
<form id="form1" runat="server">
hi
<asp:GridView ID="g1" runat="server" DataSourceID="s2"
AutoGenerateDeleteButton=true DataKeyNames="vno"/>
<asp:SqlDataSource ID="s2" runat="server"
ConnectionString="server=localhost;uid=sa;pwd=;database=Northwind"
  SelectCommand="SELECT * FROM zzz" DeleteCommand="delete from zzz where
vno=@original_vno" DataSourceMode="DataSet">
</asp:SqlDataSource>
</form>
</body>
</html>
In this example we are using the grid view to delete data. The property
AutogenerateDeleteButton displays a link with the word delete with each record. The
DataKeyNames property is set to the primary key of the table. The Sql data Source
control now hasaDeleteCommand property where we specify the delete command that we
want executed each time we click on the delete link. As the DataSourceMode is set to
DataSet the original_vno is a free variable available to us and it contains the voucher
number to be deleted. Thus each time we click on a link, that record gets deleted.

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<html>
<SCRIPT runat="server" language="C#">
void Deleting(Object source, SqlDataSourceCommandEventArgs e)
{
//e.Cancel = true;
Response.Write("Deleting " + e.Command.CommandText + "<br>");
IDbCommand cmd = e.Command;
foreach (SqlParameter param in cmd.Parameters)
{
Response.Write(param.ParameterName + " - " + param.Value.ToString() + "<br>");
  }
}
void Deleted(object source, SqlDataSourceStatusEventArgs e)
{
Response.Write("Deleted " + e.AffectedRows + "," + e.Command.CommandText +
"<br>");
}
</SCRIPT>
<form id="form1" runat="server">
hi
<asp:GridView ID="g1" runat="server" DataSourceID="s2"
AutoGenerateDeleteButton=true DataKeyNames="vno"/>
<asp:SqlDataSource ID="s2" runat="server"
ConnectionString="server=localhost;uid=sa;pwd=;database=Northwind"
  SelectCommand="SELECT * FROM zzz" DeleteCommand="delete from zzz where
vno=@original_vno" DataSourceMode="DataSet"
OnDeleting="Deleting" OnDeleted="Deleted" />
</form>
</body>
</html>

Amore complex program that does the same thing as the earlier program. We gt fine
control over the entire deleting process. The two function Deleting gets called just before
deleting and Deleted gets called after the records have been deleted. We can also get
access to the delete command being executed as well as the value of the parameters being
used and the number of records being deleted. If we set the Cancel property to true, we
are asking the entire process to be stopped.

<SCRIPT runat="server" language="c#">
void abc(Object s, EventArgs e)
{
s1.Delete();
}
</SCRIPT>
<HTML>
<FORM runat="server">h1
<asp:SqlDataSource id="s1" runat="server"
ConnectionString="Database=Northwind;Data Source=localhost;uid=sa;pwd=;"
  SelectCommand="SELECT * from zzz"
  DeleteCommand="DELETE FROM zzz WHERE vno=@ovno;DELETE FROM zzz1
WHERE vno=@ovno;">
<DeleteParameters>
<asp:ControlParameter Name="ovno" ControlId="d1" PropertyName="SelectedValue"
/>
</DeleteParameters>
</asp:SqlDataSource>
<asp:DropDownList id="d1" runat="server" DataTextField="name"
DataValueField="vno" DataSourceID="s1" />
<asp:Button runat="server" Text="Delete vno" OnClick="abc" />
</FORM>
</HTML>

Another way of deleting something. We display the records in a list box, and then press
the delete button. This calls the delete function from the sql data source class. As we have
select parameters we now have delete parameters. We can call as many delete statements
that we like, separated by a ;.

<Script runat="server" language="c#">
void abc (object s, EventArgs e)
{
Response.Write("In abc " + DateTime.Now + "<br>");
s1.Insert();
}
</Script>
<HTML>
<FORM runat="server">
<asp:sqldatasource id="s1" runat="server"
connectionstring="DataBase=NorthWind;Data Source=localhost;uid=sa;pwd=;"
  insertcommand="INSERT INTO zzz (vno,name) VALUES (@svno,@sname);INSERT
INTO zzz1 (vno,name) VALUES (@svno,@sname)">
<insertparameters>
<asp:formparameter name="svno" formfield="tvno" />
<asp:formparameter name="sname" formfield="tname" />
</insertparameters>
</asp:sqldatasource>
<asp:textbox id="tvno" runat="server" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="tvno"
ErrorMessage="Please enter a voucher no" />
<p>
<asp:textbox id="tname" runat="server" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="tname"
ErrorMessage="Please enter a name" />
<p>
<asp:button runat="server" text="Add" onclick="abc" />
</FORM>
</HTML>

Finally a insert. We key in our data into two text boxes, build our error checks and then
use the insert command to specify that our data goes into two tables. The
insertparameters tag is used to specify the names of the parameters and associate them
with text boxes.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:3/11/2012
language:
pages:4