Total Pageviews

20 April 2012

How to insert data into database with help of asp.net



About.aspx=>
<%@ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="About.aspx.cs" Inherits="About" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <style type="text/css">
    .style1
    {
        width: 100%;
    }
    .style2
    {
        width: 148px;
    }
</style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  
    <table class="style1">
    <tr>
        <td class="style2">
            User Name</td>
        <td>
            <asp:TextBox ID="t1" runat="server" Width="192px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2">
            password</td>
        <td>
            <asp:TextBox ID="t2" runat="server" TextMode="Password" Width="190px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2">
            retype password</td>
        <td>
            <asp:TextBox ID="t3" runat="server" TextMode="Password" Width="190px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2">
            Address</td>
        <td>
            <asp:TextBox ID="t4" runat="server" ontextchanged="t4_TextChanged"
                Width="188px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2">
            Mobile number</td>
        <td>
            <asp:TextBox ID="t5" runat="server" Width="188px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2">
            &nbsp;</td>
        <td>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
        </td>
    </tr>
</table>
  
</asp:Content>
About.aspx.cs=>
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.Configuration;
using System.Data.SqlClient;
public partial class About : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    String s = ConfigurationManager.ConnectionStrings["cmd"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(s);
        con.Open();
        cmd = new SqlCommand("Insert into login values(@User_name,@pass,@address,@Mob_num)", con);
        cmd.Parameters.AddWithValue(@"User_name", t1.Text);
        cmd.Parameters.AddWithValue(@"pass", t2.Text);
        cmd.Parameters.AddWithValue(@"address", t4.Text);
            cmd.Parameters.AddWithValue(@"Mob_num", t5.Text);
            int x=cmd.ExecuteNonQuery();
            if (x > 0)
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Data Insert')", true);
            }
            con.Close();
      
    }
    protected void t4_TextChanged(object sender, EventArgs e)
    {
    }
}

No comments:

Post a Comment

Contact Form

Name

Email *

Message *