Insert data into Database in ASP.NET using C#

Insert data into Database in ASP.NET using C#


Posted in : ASP.Net Posted on : December 9, 2010 at 6:05 PM Comments : [ 0 ]

In this article we will introduce How can we Insert data into Database in ASP.NET using C#

Insert data into Database in ASP.NET using C#

In this example we made a form which is used for insert the employee information in database. You fill all the details of employee and click on submit button. The data will be inserted into database table.

InsertDataBase.aspx (Design Page):

InsertDataBase.aspx (source code):

View source code Click Here

InsertDataBase.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class InsertDataBase : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
private string s, gender, date;
private int i;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
day_DropDownList.Items.Insert(0, new ListItem("DD", "DD"));
month_DropDownList.Items.Insert(0, new ListItem("MM", "MM"));
year_DropDownList.Items.Insert(0, new ListItem("YY", "YY"));
for (i = 1; i < 32; i++)
{
day_DropDownList.Items.Add(i.ToString());
}
for (i = 1; i < 13; i++)
{
month_DropDownList.Items.Add(i.ToString());
}
for (i = 1950; i < 2011; i++)
{
year_DropDownList.Items.Add(i.ToString());
}
employeeName_Txt.Focus();
}
}
// for gender
protected void gender_RadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
{
if (gender_RadioButtonList.SelectedIndex == 0)
{
gender = "Male";
}
else
{
gender = "Female";
}
}
// for insert values
protected void submit_Button_Click(object sender, EventArgs e)
{
s = WebConfigurationManager.ConnectionStrings["ChartDatabaseConnectionString"].ConnectionString;
con = new SqlConnection(s);
con.Open();
date = day_DropDownList.Text.ToString() + "/" + month_DropDownList.Text.ToString() + "/" + year_DropDownList.Text.ToString();
cmd = new SqlCommand("insert into Employee values('" + employeeName_Txt.Text + "','" + date + "','" + gender + "','" + post_Txt.Text + "','" + city_Txt.Text + "','" + country_Txt.Text + "','" + mobileno_Txt.Text + "')", con);
cmd.ExecuteNonQuery();
Label1.Text = "Information submitted successfully";
con.Close();
clear();
}
// for clear textboxes
public void clear()
{
employeeName_Txt.Text = "";
day_DropDownList.SelectedIndex = 0;
month_DropDownList.SelectedIndex = 0;
year_DropDownList.SelectedIndex = 0;
post_Txt.Text = "";
city_Txt.Text = "";
country_Txt.Text = "";
mobileno_Txt.Text = "";
}
protected void clear_Button_Click(object sender, EventArgs e)
{
clear();
Label1.Text = "";
}
}

Output:

First of all  fill all the details of employee. You can see the following figure.

Now click on submit button. The data will be submitted  successfully.

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics