Hi alya14,
Refer below sample.
HTML
<asp:Button Text="Save" runat="server" OnClick="Save" />
<br />
<asp:Label ID="lblQuery" runat="server" />
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
protected void Save(object sender, EventArgs e)
{
var query = "SELECT Query FROM StoreQuery";
GetName(query);
lblQuery.Text = GetName(query).Replace("\"", "").Replace("+", "").Replace("PLN", "aly");
}
private string GetName(string query)
{
string name = "";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
name = cmd.ExecuteScalar().ToString();
con.Close();
}
}
return name;
}
VB.Net
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim query = "SELECT Query FROM StoreQuery"
GetName(query)
lblQuery.Text = GetName(query).Replace("""", "").Replace("+", "").Replace("PLN", "aly")
End Sub
Private Function GetName(ByVal query As String) As String
Dim name As String = ""
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
name = cmd.ExecuteScalar().ToString()
con.Close()
End Using
End Using
Return name
End Function
Screenshot