Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

ExecuteNonQuery Example in Asp.Net Using C# and VB.NET

Sep 20, 2012
Introduction:

In this article I will explain executenonquery example in asp.net using C#.net and VB.NET.

Description:

In previous post I explained differences between ExecuteNonQuery, ExecuteReader and ExecuteScalar in asp.net. Now I will explain ExecuteNonQuery concept with one example in asp.net using C#.net, VB.NET.

ExecuteNonQuery

ExecuteNonQuery method will return number of rows effected with INSERT, DELETE or UPDATE operations. This ExecuteNonQuery method will be used only for insert, update and delete, Create, and SET statements.

Before implement this example first design one table UserInformation in your database as shown below

Column Name
Data Type
Allow Nulls
UserName
varchar(50)
Yes
LastName
varchar(50)
Yes
Location
Varchar(50)
Yes
Once table designed in database write the following code in your aspx page


<html xmlns="http://www.w3.org/1999/xhtml">
<head 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>Number of Rows Effected: </b><asp:Label ID="lblDetails" runat="server" />
</div>
</form>
</body>
</html>
Now add the following namespaces in code behind

C# Code


using System;
using System.Data.SqlClient;
After add namespaces write the following code in code behind

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("insert into UserInformation(UserName,FirstName,LastName,Location) values(@Name,@FName,@LName,@Location)", con);
cmd.Parameters.AddWithValue("@Name", "Suresh Dasari");
cmd.Parameters.AddWithValue("@FName", "Suresh");
cmd.Parameters.AddWithValue("@LName", "D");
cmd.Parameters.AddWithValue("@Location","Chennai");
int result= cmd.ExecuteNonQuery();
if(result>=1)
{
lblDetails.Text =  result.ToString();
}
else
{
lblDetails.Text = "0" ;
}
con.Close();
}
}
VB.NET Code


Imports System.Data.SqlClient

Partial Class VBCode
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("insert into UserInformation(UserName,FirstName,LastName,Location) values(@Name,@FName,@LName,@Location)", con)
cmd.Parameters.AddWithValue("@Name", "Suresh Dasari")
cmd.Parameters.AddWithValue("@FName", "Suresh")
cmd.Parameters.AddWithValue("@LName", "D")
cmd.Parameters.AddWithValue("@Location", "Chennai")
Dim result As Integer = cmd.ExecuteNonQuery()
If result >= 1 Then
lblDetails.Text = result.ToString()
Else
lblDetails.Text = "0"
End If
con.Close()
End Using
End Sub
End Class
Demo

If you observe above output whenever we click on button one new record inserting into table UserInformation and returning number records inserted.

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 RSS subscribe by email Subscribe by Email

10 comments :

Manish said...

Hi Suresh,

Do you any stuff for Gantt Chart using SSRS.

Rushmic Tuesday said...

Is it just me or I can't follow the reason for the if statement when one is to display the results in the label.

Suresh Dasari said...

@Rushmic Tuesday...
The number of rows effected with ExecuteNonQuery statement that result I am displaying using lable for our better understanding.

Anonymous said...

sir i just want to know that how we can store multiple table in the dataset with example......

please sir reply soon....


thanks
Rakesh kumar

krishna said...

its really awesome

Anonymous said...

very useful. thank u

Gaurav Bisht said...

Execute Non Query means return the number of rows affected..

sakthivel23ponnusamy said...

its really very usefull sir but i want how to import update query in executenonquery

Unknown said...

THANK YOU !!!!
Your ExecuteNonQuery example solved my problem which I was sitting with for 2 and 1/5 days.
I still would like to know what was wrong with my Coding

Anonymous said...

good

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.