Hi rajeesh,
Check this sample. now take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:Button ID="btnPrice" Text="Get Price" runat="server" OnClick="OnPrice" /><br /><br />
<asp:Label ID="lblPrice" Text="" runat="server" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void OnPrice(object sender, EventArgs e)
{
DataTable dt = GetData("SELECT SUM(CONVERT(NUMERIC(18,2),ISNULL(Freight,0))) AS Price FROM Orders");
decimal totalPrice = 0;
totalPrice = Convert.ToDecimal(dt.Rows[0][0]);
lblPrice.Text = Convert.ToString(totalPrice);
}
private DataTable GetData(string query)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub OnPrice(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetData("SELECT SUM(CONVERT(NUMERIC(18,2),ISNULL(Freight,0))) AS Price FROM Orders")
Dim totalPrice As Decimal = 0
totalPrice = Convert.ToDecimal(dt.Rows(0)(0))
lblPrice.Text = Convert.ToString(totalPrice)
End Sub
Private Function GetData(ByVal query As String) As DataTable
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot