Saturday, 5 April 2014

Insert the Data into DB in Asp.Net

Ans:-

Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>InsertDataInToDB</title>
   
   
</head>
<body>
    <form id="form1" runat="server">
    <div class="demo">
        <div class="ui-widget">
            <label for="tbAuto">
                Code:
            </label>
&nbsp;<asp:TextBox ID="txtCode" runat="server" ClientIDMode="Static" Width="202px"></asp:TextBox>
            <br />
            <br />Description: <asp:TextBox ID="txtDesc" runat="server" ClientIDMode="Static" Width="210px"></asp:TextBox>
            <br />
            <br />          
            <asp:Button ID="Button1" runat="server" Text="Button" ClientIDMode="Static"
                onclick="Button1_Click" />
        </div>
    </div>
    </form>
</body>
</html>

Default.aspx.cs:-
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Services;

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

    }
    
    public static string InsertMethod(int code, string desc)
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SubsDB;User ID=sa;Password=123");
        {
            SqlCommand cmd = new SqlCommand("Insert into toshfa values('" + code + "', '" + desc + "')", con);
            {
                con.Open();
                cmd.ExecuteNonQuery();
                return "True";
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int code =Convert.ToInt32(txtCode.Text);
        InsertMethod(code, txtDesc.Text);
    }
}

No comments:

Post a Comment