Hi nauna,
Refer below sample.
HTML
Name :
<asp:TextBox runat="server" ID="txtName" />
<br />
<asp:Button Text="Search" runat="server" OnClick="Fetch" />
<br />
<asp:Label ID="lblId" runat="server" />
<br />
<asp:Label ID="lblName" runat="server" />
<br />
<asp:Label ID="lblCountry" runat="server" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Fetch(object sender, EventArgs e)
{
DataSet ds = fetch(txtName.Text);
lblId.Text = "Id is : <b>" + ds.Tables[0].Rows[0]["CustomerId"].ToString() + "</b>"; ;
lblName.Text = "Name is : <b>" + ds.Tables[0].Rows[0]["Name"].ToString() + "</b>";
lblCountry.Text = "Country is : <b>" + ds.Tables[0].Rows[0]["Country"].ToString() + "</b>";
}
public DataSet fetch(string username)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers WHERE Name = @Name", con))
{
cmd.Parameters.AddWithValue("@Name", username);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
VB.Net
Protected Sub Fetch(ByVal sender As Object, ByVal e As EventArgs)
Dim ds As DataSet = Fetch(txtName.Text)
lblId.Text = "Id is : <b>" & ds.Tables(0).Rows(0)("CustomerId").ToString() & "</b>"
lblName.Text = "Name is : <b>" & ds.Tables(0).Rows(0)("Name").ToString() & "</b>"
lblCountry.Text = "Country is : <b>" & ds.Tables(0).Rows(0)("Country").ToString() & "</b>"
End Sub
Public Function fetch(ByVal username As String) As DataSet
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers WHERE Name = @Name", con)
cmd.Parameters.AddWithValue("@Name", username)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
da.Fill(ds)
Return ds
End Using
End Using
End Function
Screenshot