Sunday 26 May 2013

  • insert data into database using asp.net C#


    How to store data in ASP.NET ?

    Note:-Don't copy red coloured text,its only for better explanation of code.


    1) Go to file and select New Website.

    2) In the Solution Explorer right click on the project an select Add New Item and select Web Form and give file name as you want for example (Default.aspx).

    3) In the Default.aspx page we can design layout/form which is displayed in front of the user where they can insert data.

    See below to write code to create layout/form.Here we design simple login form.

    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>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div style="text-align: center">
    <div style="text-align: center">

    <table>
    <tr>
    <td style="width: 100px">
    user name
    </td>
    <td style="width: 100px">
    <asp:TextBox ID="txtunm" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td style="width: 100px">
    password
    </td>
    <td style="width: 100px">
    <asp:TextBox ID="txtpwd" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" style="text-align: center">
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Insert" />
    </td>
    </tr>
    <tr>
    <td colspan="2" style="text-align: center">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
    </tr>
    </table>

    </div>
    </div>
    </form>
    </body>
    </html>


    Now we write the code to store data in to the database.

    Default.aspx.cs:-

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;  //don't forget to add

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

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Connection String"); 
    ---------------------------------------------------------------------------------------------------------
    //connection string will be provided when you create database.first go to solution explorer->right click on site->click on add new item->select Sql server database.

    //Then you can see your database in server explorer window.click on the database,so you can see connection string in the property window.Just copy it and paste in above code instead of "Connection String".

    //Then add @ before starting inverted coma "".So you will not see the redline below the connection string.
    --------------------------------------------------------------------------------------------------------

    string sqlstr;
    sqlstr="insert into Login values('"+txtunm.Text+"','"+txtpwd.Text+"')";

    SqlDataAdapter sda=new SqlDataAdapter(sqlstr,cn);

    DataSet dst = new DataSet();
    sda.Fill(dst);
    Label1.Text = "record is stored";
      }
    }


    That's it.Now run the website through F5 and see the result.


  • 1 comments:

    1. The Three Darksiders: Enhanced Edition for Titanium Men\'s Wedding Band
      Custom titanium easy flux 125 amp welder T-shirt, custom titanium jewelry for piercings design, and accessories for The apple watch series 6 titanium Three titanium cookware Darksiders: Enhanced Edition - Available at Tombola citizen super titanium armor Casino.

      ReplyDelete

    Copyright @ 2013 Programming Languages.