Hi nauna,
Refer below sample.
HTML
<asp:Label ID="lblName" runat="server" />
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
CS.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
ConnectionClass connection = new ConnectionClass();
string name = "";
string query = "get_CustomersData";
SqlCommand cmd = connection.getCommand(query);
cmd.Parameters.AddWithValue("@CustomerId", 1);
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
name = dr["Name"].ToString();
}
cmd.Connection.Close();
lblName.Text = "The Name is : <b>" + name + "</b>";
}
ConnectionClass.cs
public class ConnectionClass
{
public SqlCommand getCommand(string query)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.StoredProcedure;
return cmd;
}
}
VB.Net
VB.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim connection As ConnectionClassVB = New ConnectionClassVB()
Dim name As String = ""
Dim query As String = "get_CustomersData"
Dim cmd As SqlCommand = connection.getCommand(query)
cmd.Parameters.AddWithValue("@CustomerId", 1)
cmd.Connection.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.Read() Then
name = dr("Name").ToString()
End If
cmd.Connection.Close()
lblName.Text = "The Name is : <b>" & name & "</b>"
End Sub
ConnectionClassVB.vb
Public Class ConnectionClassVB
Public Function getCommand(ByVal query As String) As SqlCommand
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.StoredProcedure
Return cmd
End Function
End Class
Output
The Name is : John Hammond