Column Name
|
Data Type
|
Allow Nulls
|
UserName
|
varchar(50)
|
Yes
|
LastName
|
varchar(50)
|
Yes
|
Location
|
Varchar(50)
|
Yes
|
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Example of ExecuteNonQuery in asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Button ID="btnSubmit"
runat="server"
Text="Submit"
onclick="btnSubmit_Click"
/><br
/>
<b>First Row First Column Value: </b><asp:Label ID="lblDetails" runat="server" />
</div>
</form>
</body>
</html>
|
using System;
using System.Data.SqlClient;
|
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
btnSubmit_Click(object sender, EventArgs e)
{
using (SqlConnection
con = new SqlConnection("Data Source=SureshDasari;Integrated
Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select
UserName,LastName,Location FROM UserInformation", con);
string result = (string)cmd.ExecuteScalar();
if (!string.IsNullOrEmpty(result))
{
lblDetails.Text = result;
}
else
{
lblDetails.Text =
"No value Selected" ;
}
con.Close();
}
}
|
Imports System.Data.SqlClient
Partial Class
ExecuteScalar
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
EventArgs)
End Sub
Protected Sub
btnSubmit_Click(ByVal sender As Object, ByVal e As
EventArgs)
Using con As New SqlConnection("Data
Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("Select
UserName,LastName,Location FROM UserInformation", con)
Dim result As String = DirectCast(cmd.ExecuteScalar(),
String)
If Not String.IsNullOrEmpty(result) Then
lblDetails.Text = result
Else
lblDetails.Text =
End If
con.Close()
End Using
End Sub
End Class
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|
|

Subscribe by RSS
Subscribe by Email
1 comments :
Thanks for your grateful informations, this blogs will be really help for SQL tutorial.