Miscellaneous - Class
Is operator
class zzz
{
public static void Main()
{
yyy a = new yyy();
xxx b= new xxx();
object o = a;
if ( o is yyy)
System.Console.WriteLine("o is yyy");
else
System.Console.WriteLine("o is not yyy");
o = b;
if ( o is yyy)
System.Console.WriteLine("o is yyy");
else
System.Console.WriteLine("o is not yyy");
}
}
class xxx
{
}
class yyy
{
}
o is yyy
o is not yyy
All classes under .net are derived from the class object. We are allowed to set a lower class to a higher class and this is why the line o = a gives us no error. The variable o is of type object but set to a yyy type. The is keyword allows us to get at the real data type of the variable we are working with. Lots of times we equate a derived class to a base class, using the is keyword we can figure the highest class our variable belongs to.
Creating Mdi forms
We create a simple Windows application in visual studio.net. We then click on the form and choose menu option View, properties menu. In the tab Window Style there will be a property IsMdiContainer which is a Boolean property and we set its value to be true from the default false. The windows colours change.
We then activate the solution explorer, click on name of solution, right mouse button and choose Add then Windows Form where we click on OK at the dialog box. We get a new form called form2.cs added to our project. We then add a button to this form form2. We then choose from the menu tab of the toolbox a menu strip control into our mdi form form1.cs. We then create a menu name vijay and below that a label one and click on it so that the following code gets activated.
private void
oneToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
As our from is called Form2 we have a class called Form2 which we instantiate. When we call the show function we are actually displaying the form. Thus we run our program , click on menu vijay and then on menu one and we see our form. The problem is that it is displayed as an independent form, not as part of the mdi form.
Form2 f = new Form2();
f.MdiParent = this;
f.Show();
We take the MdiParent property of the form and set it to this which represents the form form1 which is set to be a mdi parent. Now this form form2 displays within our MDI parent. Create a form Form3 and repeat the above process.
Lock keyword
using System.Threading;
public class zzz
{
public static void Main()
{
yyy a = new yyy();
Thread t1 = new Thread(a.abc);
Thread t2 = new Thread(a.abc);
t1.Start();
t2.Start();
}
}
public class yyy
{
public void abc()
{
lock(this)
{
for ( int i = 0 ; i <= 100 ; i ++)
{
System.Console.Write("abc {0} ",i);
}
}
}
}
We are creating two threads that call the same function abc using the Start method of the thread. As we have commented out the lock keyword, the two functions output Is intermingled. On our machine the first call of the thread executes the for loop till 65 and then executes the second abc for loop. By placing the lock keyword, we are making sure that unless all the code of abc does net get called, no other thread is allowed to call the code of abc. Thus the first abc now finishes before the second can get called.
using System.Messaging;
using System.Text;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class zzz : Form
{
Button b1;
Button b2;
public zzz()
{
b1 = new Button();
b2 = new Button();
b1.Text = "Send Payment";
b1.Click += new System.EventHandler(abcsend);
b1.Location = new Point( 60,90);
b2.Text = "Process Payment";
b2.Click += new System.EventHandler(abcrecv);
Controls.Add(b2);
Controls.Add(b1);
}
static void Main()
{
Application.Run(new zzz());
}
private void abcsend(object sender, System.EventArgs e)
{
System.Messaging.Message m = new System.Messaging.Message();
m.Body="vijay Mukhi";
MessageQueue mq =new MessageQueue(".\\Private$\\sonal");
mq.Send(m);
}
private void abcrecv(object sender, System.EventArgs e)
{
MessageQueue m = new MessageQueue(".\\Private$\\sonal");
String s =(String)m.Receive().Body;
MessageBox.Show(s, "Message Received!");
}
}
<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.