Home  • Programming • ASP.NET Core

How can I insert data in asp.net database

Create a table name(student) in databasemodel with field name: id (autoincreament), name, phone Create a table in insert.aspx : Write the following code into HeadContent contentplaceholder for css of table
    <style type="text/css">
        .style1
        {
            width: 205px;
        }
    </style>
Write the following code into MainContent contentplaceholder
 <table style="width: 100%;">
        <tr>
            <td class="style1">
                <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            </td>
         
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="Label2" runat="server" Text="Phone"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
            </td>
        </tr>

	<tr>
	    <td class="style1">
	    </td>
	    <td> <asp:Button ID="btnSave" runat="server" Text="Save" onclik="btnSave_Click"/>
	    </td>
	</tr>
    </table>
Write the following code in (code behind page) using DatabaseModel; (This is database namespace link)
protected void btnSave_Click(object sender, EventArgs e)
    {
	student u = new student();
       
        u.name = txtName.Text;
        u.phone = txtPhone.Text;

        DatabaseEntities de = new DatabaseEntities();
        de.AddObject("students",u);
        de.SaveChanges();

        txtId.Text = "";
        txtName.Text = "";
        txtPhone.Text = "";

    }
This code is written in EntityFramework View form :

Comments 3


fine
good job.
nice
About Author
Md. Nasir Uddin
Copyright © 2024. Powered by Intellect Software Ltd