How to call database function in asp.net ?



Hi
There are 3 type functions in Sqlserver 2008. These are
1.Scalar function
2.Inline scalar Function
3.Table valued Function

Scalar Function: It is used to return the single value from database.
Inline Scalar Function:It is similar to scalar function but it doesn’t contain function body.It’s means doesn’t contain Begin/End block.
Table-Valued Function:It is used to return the table data type.

Here i m going to show to how to call simple scalar function in asp.net

Step1:Create the Scalar function in database like this

CREATE FUNCTION [dbo].[Multiply](@A int,@B int)

RETURNS int
AS
BEGIN
Return @A*@B
END

Step2: Create one simple page and write the code on click event like this

protected void Button1_Click(object sender, EventArgs e)
{
int a1 = Convert.ToInt32(TextBox1.Text);
int b1 = Convert.ToInt32(TextBox2.Text);
SqlConnection con = new SqlConnection("Data Source=.\\sqlExpress;Initial Catalog=Test;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select dbo.Multiply(@a,@b)", con);
cmd.Parameters.AddWithValue("@a", a1);
cmd.Parameters.AddWithValue("@b", b1);
con.Open();
Label1.Text = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}

Advertisement

One thought on “How to call database function in asp.net ?

  1. HEMANT UPADHYAY January 13, 2012 / 5:46 am

    can u send me code of pc based sms sending application without usin gateway Thnx in advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.