
Inserting records in the database is the common operation in any application. The insertion of data is either into the database or to file. When inserting data into the database, following are the number of steps that one must have to remember:
Create a connection.
Open the database connection either declares the connection string in the web.config so that it can be used across the application or you can declare it in the .aspx page itself.
Create the command object and then assign the database connection to it.
Pass the number of parameters that you would like to insert using the parameter class of the command object.
Execute the command using the ExecuteNonQuery method that will be responsible for the insertion of records.
Checks for successful insertion
Handle errors using try catch block.
Example: Demonstrate Inserting Record in the Database.
InsertingRecords.aspx
<% @ Page Language ="C#" AutoEventWireup ="true" CodeFile ="InsertingRecords.aspx.cs" Inherits ="InsertingRecords" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns ="http://www.w3.org/1999/xhtml" >
<head runat ="server">
</head>
<body >
<form id ="form1" runat ="server">
<div >
<strong > Inserting Record in the Database < br />
</strong >
<br />
<table border ="0">
<tr><td>
<asp:Label ID ="lblProductName" runat ="server" Font-Italic ="True" Text ="Product Name"></asp:Label></ td >
<td>
<asp:TextBox ID ="txtProductName" runat ="server" BackColor ="Yellow"></asp:TextBox></ td >
<td style ="width: 3px"></td>
</tr>
<tr >
<td >
<asp:Label ID ="lblProdDesc" runat ="server" Font-Italic ="True" Text ="Product Description"></asp:Label></ td >
<td>
<asp:TextBox ID ="txtProductDesc" runat ="server" BackColor ="Yellow"></asp:TextBox ></ td >
< td style ="width: 3px"></ td >
</tr>
<tr><td>
<asp:Label ID ="lblCategoryName" runat ="server" Font-Italic ="True" Text ="Category Name"></asp:Label></ td >
<td>
<asp:TextBox ID ="txtCategoryName" runat ="server" BackColor ="Yellow"></ asp : TextBox ></td>
<td style ="width: 3px"></td >
</tr>
<tr >
<td style ="height: 26px">
<asp:Label ID ="lblQuantity" runat ="server" Font-Italic ="True" Text ="Product Quantity"></asp:Label></ td >
<td style ="height: 26px">
<asp:TextBox ID ="txtQuantity" runat ="server" BackColor ="Yellow"></ asp : TextBox ></td>
<td style ="width: 3px; height: 26px">
</td >
</tr >
<tr >
<td >
</td >
<td >
<asp:Button ID ="btnSubmit" runat ="server" OnClick ="btnSubmit_Click" Text ="Submit" /></ td >
<td style ="width: 3px">
</td >
</tr >
</table >
</div >
</form >
</body >
</html >
InsertingRecords.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class InsertingRecords : System.Web.UI. Page
{
protected void Page_Load( object sender, EventArgs e)
{
}
protected void btnSubmit_Click( object sender, EventArgs e)
{ //Inserting records in the Database
String strConn = "Data Source=localhost;Initial
Catalog=northwind;user id=sa;pwd=sa" ;
//Instantiating Command Object.
SqlConnection conn = new SqlConnection (strConn);
SqlCommand cmd = new SqlCommand ();
cmd.Connection = conn;
string strQuery = "Insert into tblproduct (productName,prodDesc,CategoryName,ProductQuantity) values
(@productName,@prodDesc,@categoryName,@productQuantity)" ;
cmd.CommandText = strQuery;
cmd.CommandType = CommandType .Text;
//Adding Parameters to the Command Object.
cmd.Parameters.AddWithValue( "@productName" , txtProductName.Text);
cmd.Parameters.AddWithValue( "@prodDesc" ,txtProductDesc.Text);
cmd.Parameters.AddWithValue( "@categoryName" ,txtCategoryName.Text);
cmd.Parameters.AddWithValue( "@productQuantity" ,Int32 .Parse(txtQuantity.Text));
//Connection Object Open
conn.Open();
cmd.ExecuteNonQuery();
//Connection Object Closed
conn.Close();
Response.Write( "One Record Inserted Successfully !!!!" );
}
}
Aspnet Discussion
- - Any idea??
- - How to Encode-Decode URL
- - Change IE settings using
- - Excel problem
- - Example of Server.Transfe




