How to Insert Data into Database in Asp.net
Here is the good basic example for inserting data into database in Asp.net.
To implement this concept you need to follow the below steps :
Step1 :
First you need to design a table in Sql Database to save the records in
database.
Step2:
Create a new Asp.net website in Visual Studio and select a Default.aspx page.
Step3:
Now open the Default.aspx.cs page and write the following source code.
Default.aspx.cs Page :
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;
public partial class _Default :
System.Web.UI.Page
{
SqlConnection conn = new
SqlConnection("Data
Source=sridhar;Initial Catalog=employee;User ID=sa;Password=123");
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
Button1_Click(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = new
SqlCommand("insert
into emp values('" + TextBox1.Text + "','"
+ TextBox2.Text + "','" +
TextBox3.Text + "','" +
TextBox4.Text + "','" +
TextBox5.Text + "')", conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
Step4:
Now build the Solution and Debug it for the output.
Output Page :
No comments:
Post a Comment