Pages

Monday, 20 June 2011

Constraints in SQL Server.

1 comments
 

Hi Friends, in this post I would like to explain Constraints in SQL Server.

* Constraint is a mechanism which can be activated automatically when the specified event is occurred.

* Constraint is a role it can be defined on the selected columns in order to prevent invalid data.

* It is one of the data integrity concepts to enforce integrity explicitly.

Different types of constraints:

1) Not Null: It does not allow null values in the specified column.

2) Check: It is used to validating user conditions.

3) Unique: It doesn’t allow duplicate values in the specified column & it allows only single NULL value.

4) Primary Key: It doesn’t allow duplicate & NULL values.

5) Foreign Key: This is for establishing relation between 2 tables. One table will act as Parent & another will act as Child. And it is associated with either Primary key (or) Unique Constraints.

* For establishing the relation between the tables should maintain at least one common column.

* Foreign key allows duplicate & Null values.

* Only one Primary Key allowed for table.

* Maximum 253 Foreign Keys allowed for table.

* We can define only Primary Key/Unique Key on a single column.

6) Default: It is for defining user default values instead of storing system default values.

Syntax for defining constraints:

Create table tableName

(Column1 datatype Constraint1 Constraint2 ....,

Column2 datatype Constraint1 Constraint2 ....,

.........)

For Example:

Create table Department

(DeptId int PrimaryKey,

DeptName varchar(20) Unique NotNull,

Location varchar(20) Default ‘Hyderabad’)

Thank You…

Readmore...

Java Script function for Printing Div content.

1 comments
 

Hi Friends, in this post I would like to explain Java Script function for Printing Div content.

Here I took one Button control for printing the content.

JavaScript Function:

<script type ="text/javascript" language ="javascript">

function Print(elementId)

{

var printContent = document.getElementById(elementId);

var windowUrl = 'about:blank';

var uniqueName = new Date();

var windowName = 'Print' + uniqueName.getTime();

var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');

printWindow.document.write(printContent.innerHTML);

printWindow.document.close();

printWindow.focus();

printWindow.print();

printWindow.close();

}

script>

Div content:

<div id="printDivContent" >

Here we can place any content like controls,text……

div>

Calling div from the Button control:

<asp:Button ID="btnPrint" runat="server" onclientclick="javascript:Print('printDivContent');" Text="Print"

Width="50px" />

Thank You…

Readmore...

Java Script function for entering only Integers values in Textbox.

2 comments
 

Hi Friends, in this post I would like to explain Java Script function for entering only Integers values in Textbox.

JavaScript Function:

<script type="text/javascript" language="javascript">

function isNumberKey()

{

if(!(((event.keyCode>=48&&event.keyCode<=57) || (event.keyCode>=96&&event.keyCode<=105))||(event.keyCode==8) || (event.keyCode==9) || (event.keyCode==37) || (event.keyCode==39) || (event.keyCode==46) || (event.keyCode==190)))

event.returnValue=false;

}

script>

Calling the above function from Textbox control:

<asp:TextBox ID="txtNumericValue" runat="server" Width="120px" onkeydown="isNumberKey();">asp:TextBox>

So that we can enter only Integer Values in the TextBox.


Thank You…

Readmore...
Sunday, 12 June 2011

Windows application for converting Text into Speech.

1 comments
 
Hi Friends, in this post I would like to explain how to convert Text into Speech.
· Open windows application.
· Place a TextBox & a Button control on to the form.
· Goto ProjectMenu---> Add Reference---> COM---> MicrosoftSpeechObjectLibrary (Add this reference).
· Add the following namespace: using SpeechLib;
Please go through the code under button click event:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SpeechLib;

namespace TextToSpeech
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnClick_Click(object sender, EventArgs e)
{
SpVoice voice = new SpVoice();//Where SpVoice is the Speakers Voice.
voice.Speak(textBox1.Text,SpeechVoiceSpeakFlags.SVSFDefault);
}
}
}


Thank You...
Readmore...

Deleting files from the system in ASP.Net.

1 comments
 
Hi Friends, in this post I would like to explain how to delete the file existing on the system.
For this I took 1 FileUpload, 1 Button & 1 label control on to the page.
Please find the source code below:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>

</head>
<body>
<form id="form1" runat="server">
<div>
<table width="300px">
<tr>
<td>
<asp:FileUpload ID="fuLoad" runat="server" />
</td>
<td>
<asp:Button ID="btnDelete" runat="server" Text="Delete File"
onclick="btnDelete_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblDisplayMessage" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


Then please find the Code for deleting the file:

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnDelete_Click(object sender, EventArgs e)
{
string filePath = fuLoad.PostedFile.FileName;
if (File.Exists(filePath.Trim()))
{
File.Delete(filePath.Trim());
lblDisplayMessage.Text="File Deleted Successfully";
}
else
{
lblDisplayMessage.Text = "File does not exist.";
}

}
}


Thank You...
Readmore...

Creating dynamic controls in asp.net web application.

0 comments
 

Hi Friends,In this example i would like to explain how to create controls(Label,TextBox,DropDownList) dynamically.


For this i took 1 button(Access) & place holder control on the form.

Code under page load:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

}
else
{
createDynamicControls();
}
}


Code for method createDynamicControls():

protected void createDynamicControls()
{

Label lblCompanyName = new Label();
lblCompanyName.ID="lblCName1";
lblCompanyName.Style.Add("left", "15px");
lblCompanyName.Text = "Company Name:";
pholder1.Controls.Add(lblCompanyName);

TextBox txtCompanyName = new TextBox();
txtCompanyName.ID = "txtCName1";
txtCompanyName.Style.Add("left", "60px");

pholder1.Controls.Add(txtCompanyName);
Label lblCountry= new Label();

lblCountry.ID="lblCountry1";
lblCountry.Style.Add("left", "495px");
lblCountry.Text = "Country:";
pholder1.Controls.Add(lblCountry);


DropDownList ddlCountry= new DropDownList();
ddlCountry.ID = "ddlCountry1";
ddlCountry.Style.Add("left", "540px");
ddlCountry.Style.Add("width", "120px");
ddlCountry.Items.Add("Select");
ddlCountry.Items.Add("India");
ddlCountry.Items.Add("Srilanka");
ddlCountry.Items.Add("Bangladesh");

pholder1.Controls.Add(ddlCountry);

}


Code under AccessButton click event:

protected void AccessButton_Click(object sender, EventArgs e)
{


TextBox txtCompany = pholder1.FindControl("txtCName1") as TextBox;

DropDownList ddlCountry = pholder1.FindControl("ddlCountry1") as DropDownList;

}
Note:

* We can't access controls directly.For this we can add controls to PlaceHolder.From this PlaceHolder we can access controls.


Thank You...
Readmore...
Saturday, 11 June 2011

Sending email with attachments in ASP.Net.

1 comments
 
Hi friends,in this post I would like to explain how to send email with attachments in Asp.Net.

For this I am taking 3 TextBoxes(For ToAddress,Subject & Body),FileUpload control,label for displaying message and a Button control.

For working with emails you need to add the following namespace:

using System.Net.Mail;


Then code under Send Button Click Event:


protected void btnSendEmail_Click(object sender, EventArgs e)
{

MailMessage mssg = newMailMessage();

mssg.From = new MailAddress("yourgmailid@gmail.com");

mssg.To.Add(txtToAddtess.Text);

mssg.Subject = txtSubject.Text;

mssg.IsBodyHtml = true;

mssg.Body = txtBody.Text;

mssg.Priority = MailPriority.High;


string strFName = null;

strFName = Path.GetFileName(fulAttach.PostedFile.FileName);

fulAttach.PostedFile.SaveAs(Server.MapPath(strFName ));

Attachment attach = new Attachment(Server.MapPath(strFName ));

mssg.Attachments.Add(attach);

System.Net.Mail.SmtpClient client = new system.Net.Mail.SmtpClient();

client.UseDefaultCredentials = false;

client.Credentials = new System.Net.NetworkCredential("yourgmailid@gmail.com", "yourgmailpassword");

client.Port = 587;

client.Host = "smtp.gmail.com";

client.EnableSsl = true;

object userstate = mssg;

client.Send(mssg);

lblDisplayMessage.Text = "Mail Sent Successfully.";

}

Note: where fulAttach is file upload control ID.

Then it will send email along with attachment.


Thank You...


Readmore...

DDL Statements in SQL Server.

1 comments
 
Hi friends,in this post i would like to explain DDL commands in SQL Server.

There are 3 types of DDL commands exist:

1)CREATE 2)ALTER 3)DROP

1)CREATE : For creating databases & database objects.

Syntax for creating database:

CREATE DATABASE DataBaseName

For Ex:

Create DataBase College


Syntax for creating Table:

CREATE TABLE TableName
(Column1 datatype,
Column2 datatype,
.......
.......)

For Ex:

Create Table Employee
(EmpId int,EmpName varchar(50),Salary money)


2)ALTER : For altering the exsisting tables.

Syntax for adding columns:

Alter Table TableName Add Column1 datatype,Column2 datatype,.....

For Ex:

Alter Table Employee Add DateOfJoin datetime,Address varchar(300)

Syntax for changing column datatype:

Alter Table TableName Alter Column ColumnName NewDataType

For Ex:

Alter Table Employee Alter Column EmpId BigInt

Syntax For Droping Columns:

Alter Table TableName Drop Column Column1,Column2,.....

For Ex:

Alter Table Employee Drop Column DateOfJoin,Address


3)DROP : is used for droping database & database objects.

Syntax for droping Tables:

Drop Table table1,table2,...

For Ex:

Drop Table Employee

Syntax for droping databases:

Drop DataBase database1,database2,...

For Ex:

Drop DataBase College

Note:

* We can't drop current(working) database.
* We can't drop system databases(i.e Master,Model,Msdb,Tempdb).



Thank You...
Readmore...
Wednesday, 8 June 2011

Sending email in ASP.Net.

0 comments
 
Hi friends,in this post I would like to explain how to send email in Asp.Net.

For this I am taking 3 TextBoxes(For ToAddress,Subject & Body) and a Button control.


For working with emails you need to add the following namespace:
using System.Net.Mail;
Then code under Send Button Click Event:


protected void btnSendEmail_Click(object sender, EventArgs e) {
MailMessage mssg = newMailMessage(); mssg.From = new MailAddress("yourgmailid@gmail.com"); mssg.To.Add(txtToAddtess.Text); mssg.Subject = txtSubject.Text; mssg.IsBodyHtml = true; mssg.Body = txtBody.Text; mssg.Priority = MailPriority.High; System.Net.Mail.SmtpClient client = new system.Net.Mail.SmtpClient(); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("yourgmailid@gmail.com", "yourgmailpassword"); client.Port = 587; client.Host = "smtp.gmail.com"; client.EnableSsl = true; object userstate = mssg; client.Send(mssg); lblDisplayMessage.Text = "Mail Sent Successfully.";//label for displaying message. }
Then email will be sent to destination person



Thank You...
Readmore...
Tuesday, 7 June 2011

Loading multiple tables in a DataSet ?

0 comments
 
Hi friends,in this post i would like to explain Loading multiple tables in a DataSet ?



sqlCommand.CommandText = "Employee"
sqlDataAdapter.Fill(sqlDataSet, " Employee ")
sqlCommand.CommandText = "Colleges"
sqlDataAdapter.Fill(sqlDataSet, " Colleges ")

In the above code,it will shows how to load multiple tables in single DataSet.

Firstdata.DataSource = sqlDataSet.Tables("Employee ").DefaultView

In order to get first table(Employee) data , use Tables collection of DataSet and the Defaultview object will give you the necessary output.

Thank You...
Readmore...

Difference between Dataset.Copy and Dataset.clone ?

0 comments
 
Dataset .Copy: - It copies both structure and data.

Dataset .Clone: - It only copies structure, does not copy data.


Thank You...
Readmore...
Saturday, 4 June 2011

Types of Name Spaces.

0 comments
 
Hi friends,in this post i would like to explain Types of Name Spaces.

* To work with ADO.Net Microsoft provided Name Spaces.

* Name Spaces are divided into 4 groups:

1)Common Name Spaces:
System.Data;
System.Data.Common;
System.Data.SQLTypes;

2)Unmanaged Name Spaces:
System.Data.OLEDB;

3)Managed Name Spaces:
System.Data.SQLClient;
System.Data.OracleClient;

4)ODBC Name Spaces:
System.Data.ODBC;

Thank You...
Readmore...

About Form in C#.Net.

0 comments
 
Hi friends,in this post i would like to explain about Form in C#.Net.

* Form works like a container,in which we can place any number of controls.

* Form is a class,which must be inherited from Form class.

* Every form will be associated with 3 files:

1)form1.cs(contains design & logic)
2)form1.Designer.cs(contails only declarations)
3)form1.Resx(contains Globalization information)

Properties of Form:

1)Name
2)Back Color
3)Fore Color
4)Font
5)BackGroundImage
6)BackGroundImageLayout
7)FormBorderStyle
8)Opacity
.
.
.
.


Thank You...
Readmore...

StreamReader & StreamWriter classes in C#.Net.

2 comments
 
Hi friends,in this post i would like to explain StreamReader & StreamWriter classes in C#.Net.

* StreamReader is used to read data from the file.

* StreamWriter is used to write data into the file.
* Directly we are not write data into the file.First we should write into RAM & then through flush() it will be   written into the file.

StreamReader methods:

1)Read() : Reads single character.
2)ReadLine() : Reads single line.
3)ReadToEnd() : Reads full file.

For Example:

StreamReader str=new StreamReader(file1);
txtBox1.Text=str.ReadToEnd();
str.Close();


StreamWriter methods:

1)Write()
2)WriteLine()

For Example:

StreamWriter stw=new StreamWriter(file1);
stw.Write(txtBox1.Text);
stw.Flush();
stw.Close();

Note:

* Where file1 is the name of the file.


Thank You...
Readmore...

Example on Switch Condition.

0 comments
 
Hi friends,in this post i would like to explain Example on Switch Condition in C#.Net.

* Open windows application.
* Place a TextBox & a Button on to the form.

* Code under button_Click event:


{
int j=int.Parse(textBox1.Text);
Switch(j)
{
Case 1 : this.BackColor=Color.Green;
              break;

Case 2 : ColorDialog clrd=new ColorDialog();
              clrd.ShowDialog();
              this.BackColor=cd.Color;
              break;

Case 3 : Application.Exit();
              break;

default : MessageBox.Show("Please enter 1,2 or 3");
             break;

}//End of switch.
}//End of button_Click event.

Readmore...

About Converting in C#.Net.

0 comments
 
Hi friends,in this post i would like to explain Converting.in C#.Net.

* Working with Convert class is called as Converting.
* Methods under Convert class:

Convert.ToChar(...)
Convert.ToByte(...)
Convert.ToString(...)
Convert.ToBoolean(...)
.
.
.
Convert.ToInt16(...)
Convert.ToInt32(...)
Convert.ToInt64(...)

Example on Converting:

Static void main(...)
{
int i= 50;
Char c=Convert.ToChar(i);
Console.WriteLine(c);
}


Thank You...
Readmore...

Type casting in c#.Net.

0 comments
 
Hi friends,in this post i would like to explain type casting concept in C#.Net.

* Type casting is the concept of converting one datatype to another datatype.

* C#.Net supports 2 types of type casting:
1)Implicit type casting.
2)Explicit type casting.

* Implicit type casting is under control or CLR.
* Explicit type casting is under control of programmer.

* Converting from Lower data types into Higher data types is called as Implicit type casting.
For Example:
Byte---->Long(Implicit)

* Converting from Higher data types into Lower data types is called as Explicit type casting.
For Example:
long---->int(Explicit)

* C#.Net supports 4 types of Explicit Type Casting:
1)C++ style of TypeCasting
2)Converting
3)Parsing
4)Boxing & UnBoxing.


Thank You.
Readmore...