Tuesday, 17 January 2012

Insert Data into Data Base Table using Class File(Properties) and Stored Procedure


Step 1:CREATE A CLASS FILE:   Demo.cs

public class Demo
{

public int roll{get;set;}
        public int age{get;set;}
        public DateTime InfDate{get;set;}

}
Step 2:Create Stored Procedure:

Create procedure fms.KC_Test
(
  @roll int,
  @age int,
  @InfDate date
  )
as
begin

insert into FMS_Demo(roll,age,InfDate) values(@roll,@age,@InfDate)
end

Step 3:Create Table:
 command:
Create Table FMS_Demo(roll int,age int,InfDate DateTime)

Step 4: Coding 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;
using System.Data.SqlClient;

public partial class DemoPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Demo dm = new Demo();
        dm.roll = int.Parse( TextBox1.Text);
        dm.age = int.Parse(TextBox2.Text);
        dm.InfDate = DateTime.Parse(Calendar1.SelectedDate.ToString());
        SqlConnection con = new SqlConnection(@"Data Source=  ABC   ;Initial Catalog=MYCampus;User ID=MY;pwd=My12554");
        con.Open();
        SqlCommand cmd = new SqlCommand("fms.KC_Test", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("roll", dm.roll);
        cmd.Parameters.AddWithValue("age", dm.age);
        cmd.Parameters.AddWithValue("InfDate", dm.InfDate);
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("Successfully");
    }
}



Step 5 Design Page


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

<!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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      RollNo  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      Age  <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
       Date <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </div>
    </form>
</body>
</html>







No comments:

Post a Comment