Hi indradeo,
Check this sample. now take its reference.
HTML
<script type="text/javascript">
window.onload = function () {
var userId = document.getElementById("lblUserID").innerHTML;
alert('User ID is ' + userId);
}
</script>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblUserID" Text="" runat="server" Style="display: none" />
</div>
</form>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT UserID FROM Users WHERE UserName='Test' AND Password='pass'", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
int UserID = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
lblUserID.Text = UserID.ToString();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT UserID FROM Users WHERE UserName='Test' AND Password='pass'", con)
cmd.CommandType = CommandType.Text
con.Open()
Dim UserID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
lblUserID.Text = UserID.ToString()
End Using
End Using
End If
End Sub
Screenshot