- Open Visual Studio .NET.
- On the File menu, click New and then click Project. Under Project types click Visual C# Projects, then click ASP.NET Web Service under Templates. Type MathService in the Location text box to change the default name (WebService1) to MathService.
- Change the name of the default Web service that is created from Service1.asmx to MathService.asmx.
- Click Click here to switch to code view in the designer environment to switch to code view.
- Define methods that encapsulate the functionality of your
service. Each method that will be exposed from the service must be flagged with
a WebMethod attribute in front of it. Without this attribute, the method will
not be exposed from the service.
NOTE: Not every method needs to have the WebMethod attribute. It is useful to hide some implementation details called by public Web service methods or for the case in which the WebService class is also used in local applications. A local application can use any public class, but only WebMethod methods will be remotely accessible as Web services.
Add the following method to the MathServices class that you just created:[WebMethod] public int Add(int a, int b) { return(a + b); } [WebMethod] public System.Single Subtract(System.Single A, System.Single B) { return (A - B); } [WebMethod] public System.Single Multiply(System.Single A, System.Single B) { return A * B; } [WebMethod] public System.Single Divide(System.Single A, System.Single B) { if(B == 0) return -1; return Convert.ToSingle(A / B); }
- Click Build on the Build menu to build the Web service.
- Browse to the MathService.asmx Web service page to test the
Web service. If you set the local computer to host the page, the URL is
http://localhost/MathService/MathService.asmx.
The ASP.NET runtime returns a Web Service Help Page that describes the Web service. This page also enables you to test different Web service methods.
Tuesday, 4 February 2014
Write a Simple .asmx Web Service
Sql Connections in asp.net
Opening and Closing Connections
Case in point: managing of sql database connection resources. How many of us learned to write something like this:
Sure it’s easy to follow, but if you deploy that on a moderately busy server you are going to make your client very unhappy.
Insert Data into Database-
Case in point: managing of sql database connection resources. How many of us learned to write something like this:
// Create a new SQL Connection object
SqlConnection conn = new SqlConnection( connectionString );
// Open the connection to the database
conn.Open();
// Create a new SQL Command
SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn );
// Execute the command
cmd.ExecuteNonQuery();
// Close the database connection
conn.Close();
Sure it’s easy to follow, but if you deploy that on a moderately busy server you are going to make your client very unhappy.
Insert Data into Database-
Asp.Net-Connection String
ASP.NET Connection String
A connection string provides the information that a
provider needs to communicate with a particular database. The Connection
String includes parameters such as the name of the driver, Server name
and Database name , as well as security information such as user name
and password.
An ADO.NET Data Provider is a class that can
communicate with a specific type of database or data store. Usually Data
Providers use a connection string containing a collection of parameters
to establish the connection with the database through applications. The
.NET Framework provides mainly three data providers, they are
Microsoft SQL Server
OLEDB
ODBC
Here you can see how to make a connection string to the following ADO.NET Data Providers.
Microsoft SQL Server Connection String
connetionString="Data Source=ServerName;Initial Catalog=Databasename;
User ID=UserName;Password=Password";
OLEDB Data Provider Connection String
connetionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourdatabasename.mdb;"
ODBC Connection String
connetionString="Driver={Microsoft Access Driver (*.mdb)};DBQ=yourdatabasename.mdb;"
You have to provide the necessary connection information to the Connection String attributes
Web.config and ConnectionString
You can store connection strings in the Web.config
file and reference the configuration entries in data source controls.
You can create connectionStrings element within the Element element, by create a child element named and place your connection strings there.
<configuration>
<connectionStrings>
<add name="RamaConnectionString"
connectionString="Server=Rama-PC; Database=Test; User Id=sa; password= Admin123"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
You can access the connectionstring value at run time in your ASP.NET application as shown in the following example.
C#
string conn = ConfigurationManager.ConnectionStrings["RamaConnectionString"].ToString();
ASP.NET Sql Server Connection
The SqlConnection Object is Handling the
part of physical communication between the ASP.NET application and the
SQL Server Database . An instance of the SqlConnection class in ASP.NET
is supported the Data Provider for SQL Server Database.
C#
string conn = ConfigurationManager.ConnectionStrings["RamaConnectionString"].ToString();
When the connection
is established , SQL Commands will execute with the help of the Command
Object and retrieve or manipulate the data in the database. Once the
Database activities is over , Connection should be closed and release
the Data Source resources .
The Close() method in SqlConnection Class is used
to close the Database Connection. The Close method rolls back any
pending transactions and releases the Connection from the SQL Server
Database.
The following ASP.NET program connect to a database server and display the message in the Label control.
web.config
<?xml version="1.0"?>
<configuration> <connectionStrings>
<add name="RamaConnectionString"
connectionString="Server=Rama-PC; Database=Test; User Id=sa; password= Admin123"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
You have to fill appropriate web.config database parameters
Default.aspx
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
default.aspx.cs
using System;
using System.Data ;
using System.Data.SqlClient ;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["RamaConnectionString"].ToString();
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
Label1.Text = "Connected to Database Server !!";
connection.Close();
}
}
Subscribe to:
Posts (Atom)