Monday, 22 October 2012

Fetch a Data from database into a Login form


In this article I will describe, how a user can fetch a data from database using login Form. This is an elementary level of article for fetching data from database. By using this technique you can fetch entire data from the database to a web form.
  1. Open visual studio
  2. Drag a panel to web form for login box.
  3. Now drag some Textboxes ,Labels and Buttons on web form

    <asp:Panel ID="Panel1" runat="server"BackColor="Lime"BackImageUrl="~/App_Data/img1.JPG"BorderColor=
    "#00CC00"
    Height="224px"Style="margin-left: 287px"Width="488px">&nbsp;&nbsp;<br />&nbsp;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:LabelID="Label2"
    runat="server"Text="Username"></asp:Label>&nbsp;<asp:TextBox ID="TextBox1" runat="server"Height="22px"OnTextChanged="TextBox1_TextChanged1"Width="229px"
    AutoCompleteType="Disabled"></asp:TextBox><br/><br/>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:LabelID=
    "Label3"
    runat="server"Text="Password"></asp:Label>&nbsp;<asp:TextBoxID=
    "TextBox2"
    runat="server"Width="226px"
    TextMode="Password"></asp:TextBox>&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp
    ;&nbsp;&nbsp;&nbsp;&nbsp;
    <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;
    Email id&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBoxID="TextBox3"runat="server"
    Width="223px"AutoCompleteType ="Disabled"></asp:TextBox><br />&nbsp;&nbsp;&nbsp;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;
    <asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Sign In" /><br/>
  4. Now create database Using sqlserver.
  5. Create connection for database
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
string str;

protected void Page_Load(object sender, EventArgs e)

{
con =
new SqlConnection("Data source =MCNDESKTOP06 ;Initial Catalog=empinfo;UserID=sa;Password = wintellect");
con.Open();
}
6. Insert the data into the database which you are created
7. Now write the code on button click event
protected void Button1_Click(object sender, EventArgs e)
{
str =
"select * from login info  where name ='" + TextBox1.Text + "' and Password ='" + TextBox2.Text + "' and emailid ='" + TextBox3.Text + "'";
cmd =
new SqlCommand(str, con);
dr = cmd.ExecuteReader();
if (dr.Read())
{
Session[
"name"] = dr.GetString(0);
Response.Redirect(
"account.aspx");
}
else
{
Label4.Text =
"Invalid user";
}
}

Output of the application 

WCF Introduction


Introduction

This article demonstrates how to create a WCF service application. This article also covers basic information of all the contracts and a code demonstration.

What is WCF?

WCF is a combined feature of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication. It is a part of .Net 3.0.

What-is-WCF.jpg

Difference between WCF and Web service
  • In a web service we need to add the [WebService] attribute to the class.
    In WCF we need to add the [ServiceContract] attribute to the class.
     
  • Add the [WebMethod] attribute to the method in a web service.
    Add the [OperationContract] attribute to the method in WCF.
     
  • For serialization in a web service use the System.Xml.serialization namespace.
    WCF uses the System.Runtime.Serialization namespace for serialization.
     
  • We can host a web service in IIS.
    We can host WCF in IIS, WAS (Windows Activation Service), self-hosting and a Windows Service .
Let's see how to create a WCF service application step-by-step.

Step 1

Start Menu >> All Programs >> Microsoft Visual Studio 2010 >> Microsoft Visual Studio 2010

In that "File" >> "New" >> "Project..."

Microsoft-Visual-Studio-2010.jpg

Step 2
  • Select WCF from Installed Templates
  • Select .NET Framework 4 from dropdownlist
  • Select WCF Service Application
  • Give desired name and select the location
  • Click on OK button

    Select-WCF-Installed-Templates.jpg
Step 3

Now your Windows Communication Foundation service application is ready as a default service. You will see in Solution Explorer Service1.svc and IService1.cs

Open the IService1.cs file, as in:

Open-Service1.cs-file.jpg

In this file you willl find a ServiceContract, OperationContract and DataContract.

Service Contract

Service contract is an attribute applied to an interface i.e. IService1. It describes which operations the client can perform on the services.

Service-Contract.jpg

Operation Contract

Operation Contract is an attribute applied to methods in interfaces i.e. IService1. It is used to define a method in an interface.

Operation-Contract.jpg

Data Contract

DataContract defines which data types are passed to and from the service. It is used to define a class and the DataMember attribute is used to define the properties.

Data-Contract.jpg

WCF Contracts map directly to a corresponding web services standard:
  • ServiceContracts map to WSDL
  • DataContracts map to XSD
  • MessageContracts map to SOAP
Step 4

There is one method "GetData" which accepts parameters of type int.

WCF-Contracts-map.jpg

An implementation of this method is in the Service1.svc.cs file. This method returns a string with the value you passed as a parameter.

Service1-svc-cs.jpg

Step 5

Now let us run this service.

run-this-service.jpg

You can test your service in WCF Test Client. WCF Test Client is a GUI tool which enables us to enter parameters, invoke services with these parameters and view the responses returned by services.

Now double-click on the "GetData" method and enter a parameter value of type System.int32. Here I enter "25" as a parameter value and press the "Invoke" button. You will get "You entered: 25" as a response.

GetData-method.jpg

Insert, Select, Update and Delete Records in a Single Stored Procedure Using SQL Server


What is Stored Procedure?

A Stored Procedure is a group of logical SQL statements to perform a specific task such as insert, select, update and delete operations on a table and so on which is stored in a SQL database.
Creating a Stored Procedure
Before creating a Stored Procedure, we will create one table named employee in the SQL database which looks as in the following image.
I have set the primary key on the id column for the Identy specification.
 
creatingtbl.png
 

 

 

 

 



Now we have a table to perform these operations. Now let us start to create the Stored Procedure.
The Stored Procedure is created using the keyword Create Procedure followed by the procedure name. Let us create the Stored Prcedure named EmpEntry as given below.
create Procedure EmpEntry
(
 --variable  declareations

@Action
Varchar (10),                             --to perform operation according to string passed to this varible such as Insert,update,delete,select    
@id
int=null,                                   --id to perform specific task
@Fname Varchar (50)=null,                     -- for FirstName
@MName Varchar (50)=null,                    -- for MName
@Lname Varchar (50)=null                      -- for LastName
)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---exec EmpEntry @Action='delete' ,@Fname='S',@MName='R',@Lname='M',@id='13'  --added by vithal wadje on 18-10-2012 for Csharp contribution
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
as
Begin
  SET NOCOUNT ON;
If @Action='Insert'   --used to insert records
Begin
Insert Into
 employee (FirstName,MName,LastName)values(@Fname,@MName,@Lname)
End  
else if @Action='Select'   --used to Select records
Begin
select *from
employee
end

else if
@Action='Update'  --used to update records
Begin
 update employee set FirstName=@Fname,MName=@MName,LastName=@Lname where id=@id
 End
 Else If
@Action='delete'  --used to delete records
 Begin
 delete from employee where id=@id
 end
 End

In the above Stored Procedure throught comments I have clearly explained which block is used for which purpose, so I have briefly explained it again. I have used @Action variable and assigned the string to them and according to the parameter passed to the Stored Procedure the particular block will be executed because I have kept these blocks or conditions in nested If else if conditional statements.
 "The most important thing is that I have assigned null to each variable to avoid the effect on the parameter passed to the Stored Procedure because we are passing a different number of parameters but not the same number of parameters to the Stored Procedure to perform these tasks." 
After creating this Stored Procedure, now let us use it.
To execute the Stored Procedure EmpEntry that we created we need to use the keyword exec followed by the procedure name and the parameter list. I have explained how to use it below.
Inserting the Records into the Employee table that we created with the EmpEntry procedure; see:
exec EmpEntry @Action='Insert' ,@Fname='vithal',@MName='G',@Lname='Wadje'
After running this query the records will be inserted into the table employee. To see the records inserted into the table the run following query:
select * from employee

the output will be as shown in the following:
insert.png
 

 

Their are two records you have seen because I have executed the procedure two times.
·         Selecting Records From table
exec EmpEntry @Action='Select'
The output will be as follows:
 
insert.png
 


 
·         Updating Records of table
exec EmpEntry @Action='Update' ,@Fname='Manish',@MName='Kapil',@Lname='Sawant',@id=2
After executing the above query the id number 2 record will be updated in the table.
To see, run the query: select * from employee
The output will be as shown in the following:
update.png
·         Deleting the Records from table
exec EmpEntry @Action='delete' ,@id=2
After executing the above query the id number 2 record will be deleted from the table.
To see, run the query: select * from employee
The output will be as shown in the following:
delete.png