locked
.NET Data Entry Form to Add New Record RRS feed

  • Question

  • User-179998394 posted

    I am looking for some help with a data entry screen I've built. The form will be used to add new records to an existing DB. With the form already built, I'm looking for some help with the code I need to commit (insert) the values from the form to the DB. The DB is SQL SERVER.

    I am currently using Web Forms with Entity Framework to build the app and I want the data entry form to be available from a link on the default page. I've looked at GridView and FormView as options, but they don't meet my needs. Admittedly I am new with asp.net, c# and EntityFramework so I may not know how to best manipulate those views.

    Can someone tell me the code I would need to use the form to add new records to the database?

    I'm also open to suggestions of any easier/better methods.

    Thursday, May 30, 2019 10:13 PM

Answers

  • User-1038772411 posted

    Hii eldonlhale,

    Here Based on your description, I have no idea about your data entry screen fields or UI . But here i will suggest you some references with form examples to reach at your milestones. I hope this will help you. 

    Please refer the following links

    1) https://docs.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/introducing-aspnet-web-pages-2/entering-data
    2) https://stackoverflow.com/questions/4762381/asp-net-using-a-form-to-insert-data-into-an-sql-server-table
    3) https://www.c-sharpcorner.com/UploadFile/009464/insert-data-into-database-using-Asp-Net-C-Sharp-and-stored-proced/

    Thank you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 31, 2019 5:53 AM
  • User665608656 posted

    Hi eldonlhale,

    According to your description, I suggest that you display the original data in the gridview, and add a FooterTemplate to the last column of data in the GridView to put the link on the default page.

    Then create a RowsAdd.aspx page which is to enable you to entry data to the textbox controls in the form, and add a submit form button to store the input data.

    You could get the input data in the onclick event of the button, and stores it in the corresponding database.

    All information in the form is wrapped in <form></form>. Wrap all <input> boxes with <form> and when you click submit button, you will submit all <input>boxes wrapped by <form> to an address in the code behind.

    You could refer to: https://www.w3schools.com/html/html_forms.asp

    For more details , you could also refer to my code as below:

    Default page:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" ShowFooter="True">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
                      <asp:TemplateField HeaderText="Country" SortExpression="Country">
                          <ItemTemplate>
                              <asp:Label ID="Label1" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
                          </ItemTemplate>
                          <FooterTemplate>
                            <asp:HyperLink ID="Add" runat="server"
                                NavigateUrl='<%# "Javascript:window.open(\"RowsAdd.aspx\",\"\",\"Scrollbars=no,Toolbar=no,Location=no,Direction=no,Resizeable=no, Width=500 ,Height=300\")"  %>' Text="AddRow"></asp:HyperLink>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Test]"></asp:SqlDataSource>

    RowsAdd page:

      <form id="form1" runat="server">
            <div>
                <table style="width:300px;">
                    <tr>
                        <td>Name:</td>
                        <td>
                            <asp:TextBox ID="Name1" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Gender:</td>
                        <td>
                            <asp:TextBox ID="Gender1" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Country:</td>
                        <td>
                            <asp:TextBox ID="Country1" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                </table>
            </div>
            <asp:Button ID="Button1" runat="server" Text="Save"
                OnClick="Button1_Click" />
        </form>

    RowsAdd  code behind:

     protected void Button1_Click(object sender, EventArgs e)
            {
                string Name = Name1.Text.ToString();
                string Gender = Gender1.Text.ToString();
                string Country = Country1.Text.ToString();
                try
                {
                    using (var conn = new SqlConnection())
                    {
                        conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        conn.Open();
    
                        var insertCommand = new SqlCommand("INSERT INTO [Test] (Name, Gender, Country) VALUES (@Name, @Gender, @Country); ", conn);
    
                        insertCommand.Parameters.Add(new SqlParameter("Name", Name));
                        insertCommand.Parameters.Add(new SqlParameter("Gender", Gender));
                        insertCommand.Parameters.Add(new SqlParameter("Country", Country));
                        insertCommand.ExecuteNonQuery();
                        conn.Close();
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>window.opener.location.reload(true);self.close();</script>");
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    
            }

    The result of my work demo:

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 31, 2019 7:51 AM
  • User-2054057000 posted

    Since you are using Entity Framework, Web Forms and SQL Server to design an entry form. Therefore I recommend you to consult this tutorial:

    How to perform CRUD Operations in Entity Framework and ASP.NET Web Forms

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 31, 2019 12:09 PM

All replies

  • User-1038772411 posted

    Hii eldonlhale,

    Here Based on your description, I have no idea about your data entry screen fields or UI . But here i will suggest you some references with form examples to reach at your milestones. I hope this will help you. 

    Please refer the following links

    1) https://docs.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/introducing-aspnet-web-pages-2/entering-data
    2) https://stackoverflow.com/questions/4762381/asp-net-using-a-form-to-insert-data-into-an-sql-server-table
    3) https://www.c-sharpcorner.com/UploadFile/009464/insert-data-into-database-using-Asp-Net-C-Sharp-and-stored-proced/

    Thank you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 31, 2019 5:53 AM
  • User665608656 posted

    Hi eldonlhale,

    According to your description, I suggest that you display the original data in the gridview, and add a FooterTemplate to the last column of data in the GridView to put the link on the default page.

    Then create a RowsAdd.aspx page which is to enable you to entry data to the textbox controls in the form, and add a submit form button to store the input data.

    You could get the input data in the onclick event of the button, and stores it in the corresponding database.

    All information in the form is wrapped in <form></form>. Wrap all <input> boxes with <form> and when you click submit button, you will submit all <input>boxes wrapped by <form> to an address in the code behind.

    You could refer to: https://www.w3schools.com/html/html_forms.asp

    For more details , you could also refer to my code as below:

    Default page:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" ShowFooter="True">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
                      <asp:TemplateField HeaderText="Country" SortExpression="Country">
                          <ItemTemplate>
                              <asp:Label ID="Label1" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
                          </ItemTemplate>
                          <FooterTemplate>
                            <asp:HyperLink ID="Add" runat="server"
                                NavigateUrl='<%# "Javascript:window.open(\"RowsAdd.aspx\",\"\",\"Scrollbars=no,Toolbar=no,Location=no,Direction=no,Resizeable=no, Width=500 ,Height=300\")"  %>' Text="AddRow"></asp:HyperLink>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Test]"></asp:SqlDataSource>

    RowsAdd page:

      <form id="form1" runat="server">
            <div>
                <table style="width:300px;">
                    <tr>
                        <td>Name:</td>
                        <td>
                            <asp:TextBox ID="Name1" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Gender:</td>
                        <td>
                            <asp:TextBox ID="Gender1" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Country:</td>
                        <td>
                            <asp:TextBox ID="Country1" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                </table>
            </div>
            <asp:Button ID="Button1" runat="server" Text="Save"
                OnClick="Button1_Click" />
        </form>

    RowsAdd  code behind:

     protected void Button1_Click(object sender, EventArgs e)
            {
                string Name = Name1.Text.ToString();
                string Gender = Gender1.Text.ToString();
                string Country = Country1.Text.ToString();
                try
                {
                    using (var conn = new SqlConnection())
                    {
                        conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        conn.Open();
    
                        var insertCommand = new SqlCommand("INSERT INTO [Test] (Name, Gender, Country) VALUES (@Name, @Gender, @Country); ", conn);
    
                        insertCommand.Parameters.Add(new SqlParameter("Name", Name));
                        insertCommand.Parameters.Add(new SqlParameter("Gender", Gender));
                        insertCommand.Parameters.Add(new SqlParameter("Country", Country));
                        insertCommand.ExecuteNonQuery();
                        conn.Close();
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>window.opener.location.reload(true);self.close();</script>");
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    
            }

    The result of my work demo:

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 31, 2019 7:51 AM
  • User-2054057000 posted

    Since you are using Entity Framework, Web Forms and SQL Server to design an entry form. Therefore I recommend you to consult this tutorial:

    How to perform CRUD Operations in Entity Framework and ASP.NET Web Forms

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 31, 2019 12:09 PM
  • User-179998394 posted

    AddWeb, YongQing & YogYogi,

    Thank you! These resources look very promising!! I will go over them and see what best applies. I appreciate the references, it's a tremendous help. This forum has been the best place for help. Thank you again!

    eldon

    Saturday, June 1, 2019 4:24 PM