Friday, January 20, 2017

How to insert data into SQL server database using C# in ASP.NET

Introduction

Insert data into database is an important portion in Asp.Net. In this tutorial we use SQL-Server as backend. Here, we insert data from a Employees Information form into database using C# Asp.Net. There are three steps to insert data into database.

  1. Create table in database.
  2. Design .aspx page.
  3. Write Code on .aspx.CS page.

Step 1. Create table in Database

Table Name: - Employee















Query for creating a table












Step 2. Create/design .aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Inserdata.aspx.cs" Inherits="Inserdata" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align:center;">
    <table>
        <tr>
            <td colspan="2">
            <h3>Employee Information</h3>
                </td>
        </tr>
        <tr>
            <td>Employee Id :</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
         <tr>
            <td>Employee Name :</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
             </td>
        </tr>
         <tr>
            <td>Address :</td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
             </td>
        </tr>
         <tr>
            <td>Email Id :</td>
            <td>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
             </td>
        </tr>
         <tr>
            <td>Contact No :</td>
            <td>
                <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
             </td>
        </tr>
         <tr>
             <td colspan="2">
                &nbsp;
            </td>
        </tr>
         <tr>
            <td colspan="2" style="text-align:center;">
                <asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" />&nbsp;&nbsp;&nbsp;
                <asp:Button ID="btncancel" runat="server" Text="Cancel" />
            </td>
            
        </tr>
    </table>
    </div>
    </form>
</body>
</html>



Screen-shot














































Step 3. C# Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Inserdata : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btninsert_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd.CommandText = "insert into emp values(@id,@name,@address,@emailid,@contactno)";
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@id",TextBox1.Text);
        cmd.Parameters.AddWithValue("@name",TextBox2.Text);
        cmd.Parameters.AddWithValue("@address",TextBox3.Text);
        cmd.Parameters.AddWithValue("@emailid",TextBox4.Text);
        cmd.Parameters.AddWithValue("@contactno",TextBox5.Text);
        cmd.ExecuteNonQuery();
        Response.Write("<script>alert('Data Inserted...');</script>");
        con.Close();
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
    }
}



Explanation:




You are advised to change your SqlConnection string where web.config file

configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
  <connectionStrings>
    <add name="mycon" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\KRUNAL\Documents\BloggerData\App_Data\BloggerData.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>


Output:


























Thank You.....

No comments :

Post a Comment