ExecuteNonQuery executes a SQL command(Insert,Update or Delete commands) against the connection and returns the number of rows affected.
Example :
Consider you have a " Employee " table in your database.
Now , you want to insert a record into your Employee table.
Consider you have to enter two values i.e., FirstName and LastName into your table. For that , write this code into your aspx(Name.aspx) page..
The above form contains two textboxes to enter FirstName and LastName and a Button to Submit the data.
Note: Note the onClick attribute of Button. The event btnSubmit_Click is called when you click on the button.
In your code behind file(Name.aspx.cs) write this code...
In this way, we can add records to our DataBase table(Using ExecuteNonQuery()..)
Example :
Consider you have a " Employee " table in your database.
Now , you want to insert a record into your Employee table.
Consider you have to enter two values i.e., FirstName and LastName into your table. For that , write this code into your aspx(Name.aspx) page..
<form id="form1" runat="server">
<table>
<tr>
<td>FirstName</td>
<td><asp:TextBox runat="server" ID="txtFirstName"> </asp:TextBox> </td>
</tr>
<tr>
<td>LastName</td>
<td><asp:TextBox runat="server" ID="txtLastName"> </asp:TextBox></td>
</tr>
<tr>
<td><asp:Button runat="server" ID="btnSubmit" Text="SUBMIT" OnClick="btnSubmit_Click"/> </td>
<td></td>
</tr>
</table>
</form>
<table>
<tr>
<td>FirstName</td>
<td><asp:TextBox runat="server" ID="txtFirstName"> </asp:TextBox> </td>
</tr>
<tr>
<td>LastName</td>
<td><asp:TextBox runat="server" ID="txtLastName"> </asp:TextBox></td>
</tr>
<tr>
<td><asp:Button runat="server" ID="btnSubmit" Text="SUBMIT" OnClick="btnSubmit_Click"/> </td>
<td></td>
</tr>
</table>
</form>
The above form contains two textboxes to enter FirstName and LastName and a Button to Submit the data.
Note: Note the onClick attribute of Button. The event btnSubmit_Click is called when you click on the button.
In your code behind file(Name.aspx.cs) write this code...
//Add these two namespaces
using System;
using System.Data.SqlClient;
// Add btnSubmit_Click event(which is fired when you click on button) below Page_Load() event..
protected void btnSubmit_Click(object sender, EventArgs e)
{
using (SqlConnection connection=new SqlConnection("Write your connection string here.."))
{
connection.Open();
SqlCommand cmd = new SqlCommand("insert into Employee(FirstName,LastName) values(@FName,@LName)", connection);
//then add parameters to your command
cmd.Parameters.AddWithValue("@FName", txtFirstName.Text());
cmd.Parameters.AddWithValue("@LName", txtLastName.Text());
int result= cmd.ExecuteNonQuery(); //here you will get the no.of rows effected
if(result > 0) // It means atleast one row has been effected
{
response.write("Employee added");
}
else
{
response.write("Error in adding Employee");
}
connection.Close(); //Don't forget to close the connection when you are done with the connection.
}
}
using System;
using System.Data.SqlClient;
// Add btnSubmit_Click event(which is fired when you click on button) below Page_Load() event..
protected void btnSubmit_Click(object sender, EventArgs e)
{
using (SqlConnection connection=new SqlConnection("Write your connection string here.."))
{
connection.Open();
SqlCommand cmd = new SqlCommand("insert into Employee(FirstName,LastName) values(@FName,@LName)", connection);
//then add parameters to your command
cmd.Parameters.AddWithValue("@FName", txtFirstName.Text());
cmd.Parameters.AddWithValue("@LName", txtLastName.Text());
int result= cmd.ExecuteNonQuery(); //here you will get the no.of rows effected
if(result > 0) // It means atleast one row has been effected
{
response.write("Employee added");
}
else
{
response.write("Error in adding Employee");
}
connection.Close(); //Don't forget to close the connection when you are done with the connection.
}
}
In this way, we can add records to our DataBase table(Using ExecuteNonQuery()..)
Great Explanation of ExecuteNonQuery() Example
ReplyDelete