Friday, September 4, 2015

How to insert record into Sql server database using Entity framework in

In this article I am going to explain how to insert record into Sql server database using Entity framework in


How to insert record into Sql server database using Entity framework in asp.netIn this tutorial I will insert the record/data into sql server database using database first approach. I have created a table Movie in database.

To set up the entity framework project read the article How to set Entity Framework project or website in

After that add a webform to project/website.
HTML Markup of webform:
<fieldset style="width:450px;">
            <legend>Entity Framework Tutorial</legend>
            <td>Movie Name :</td>
            <td><asp:TextBox ID="txtname" runat="server" /></td>
            <td>Genre :</td>
            <td><asp:TextBox ID="txtgenre" runat="server" /></td>
            <td>Cost :</td>
            <td><asp:TextBox ID="txtcost" runat="server" /></td>
            <td>Upload Movie banner :</td>
                <asp:FileUpload ID="fubanner" runat="server" /></td>
            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" CausesValidation="false" /></td></tr>

Create the object of Entity connection

DemoEntities db = new DemoEntities();

Dim db As New DemoEntities

On button click write the code to insert the record and clear the control.
  protected void Button1_Click(object sender, EventArgs e)
            Movie tb = new Movie();
            tb.Name = txtname.Text;
            tb.Genre = txtgenre.Text;
            tb.Cost = Convert.ToInt32(txtcost.Text);

            string filepath = Server.MapPath("~/images/") + Guid.NewGuid() + fubanner.PostedFile.FileName;
            string fl = filepath.Substring(filepath.LastIndexOf("\\"));
            string[] split = fl.Split('\\');
            string newpath = split[1];
            string imagepath = "~/images/" + newpath;
            tb.Poster = imagepath;
            Response.Write("<script>alert('Record Insert Successfully');</script>");
        catch (Exception ex)
        { }
    public void Clear()
        txtname.Text = string.Empty;
        txtcost.Text = string.Empty;
        txtgenre.Text = string.Empty;

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim tb As New Movie
            tb.Name = txtname.Text
            tb.Genre = txtgenre.Text
            tb.Cost = txtcost.Text
            Dim filepath As String = Server.MapPath("~/images/") & Guid.NewGuid().ToString() & fubanner.PostedFile.FileName
            Dim fl As String = filepath.Substring(filepath.LastIndexOf("\"))
            Dim split As String() = fl.Split("\"c)
            Dim newpath As String = split(1)
            Dim imagepath As String = Convert.ToString("~/images/") & newpath
            tb.Poster = imagepath
            Response.Write("<script>alert('Record Insert Successfully');</script>")
        Catch ex As Exception
        End Try
    End Sub
    Public Sub Clear()
        txtname.Text = String.Empty
        txtcost.Text = String.Empty
        txtgenre.Text = String.Empty
    End Sub

Build the project and run. Test the application via insert the record.
How to insert record into Sql server database using Entity framework in

  In this article we have learn to how to insert record into sql server database using Entity Framework 6.0 in Visual studio 2013I hope you enjoyed this article. 

No comments:

Post a Comment