Saturday 3 August 2013

How to insert value into a table in ASP.net (Simple Way)

Hi friends Today i tell about very common and simple method for insert value into a table using ASP.NET for absolute beginners.
The table definition is shown below









Then the code is shown below








-->

    <form id="form1" runat="server">
    <div>
        Name:&nbsp;&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        Email&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        Age&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            style="height: 26px" Text="Button" />
    </div>
    </form>

 Double click on button and insert the below code into .cs page.

 protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=JISHNU\\SQLEXPRESS;Initial Catalog=dreamtheweb;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into account (name,email,age)values ('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"')  ");
            cmd.Connection = con;
            int i = cmd.ExecuteNonQuery();
            con.Close();
        }

Result:







Thank you..Our next section is insert values into table using Stored procedure (SP).

0 comments:

Post a Comment