none
How to develop master detail from in asp.net c# web form? RRS feed

  • Question

  • i need help urgently please that how to develop Master Detail form in asp.net c# with crud Operation here is picture which i created on window form but do not know how to developed on web form c#

    here is my data  

    CREATE TABLE #ItemMasterFile(CodeItem INT,Descriptionitem VARCHAR(50)) INSERT INTO #ItemMasterFile VALUES(1,'A') INSERT INTO #ItemMasterFile VALUES(2,'B') INSERT INTO #ItemMasterFile VALUES(3,'C')

    ----Master Table---- CREATE TABLE #Pack (PID INT,PName VARCHAR(50)) INSERT INTO #Pack VALUES(1,'INDIA')

    -----Child Table---- CREATE TABLE #PackDetails (PDID INT,PID int,CodeItem int,QTY int) INSERT INTO #PackDetails VALUES(1,1,1,1) INSERT INTO #PackDetails VALUES(2,1,2,1) INSERT INTO #PackDetails VALUES(3,1,3,1)


    • Moved by CoolDadTx Wednesday, August 7, 2019 1:42 PM ASP.NET related
    Tuesday, August 6, 2019 6:48 PM

Answers

  • here is sample for Master Detail Form....

    HTML

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MasterDetails.aspx.cs" Inherits="WebApplication1.MasterDetails" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Master Detail Form</title>
        </head>
    <body>
      <form id="form1" runat="server">
        <div>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Master Detail
            <br />
            <br />
            Customer Name : <asp:TextBox runat="server" ID="txtName" Height="16px" Width="124px" />
    
            <br />
            <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    
            Child Detail
            <br />
            <br />
            CodeItems : <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
            Qty :
    <asp:TextBox runat="server" ID="txtqty" Height="16px" Width="53px" />
            
    
            <asp:Button Text="Insert" runat="server" OnClick="GVadd_Click" />
    
        
    <br />
    
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
              <asp:BoundField DataField="Codeitem" HeaderText="Codeitem" Visible="False"/>
                   <asp:BoundField DataField="_PID" HeaderText="PID" Visible="False"/>
            <asp:BoundField DataField="Descriptionitem" HeaderText="Descriptionitem" />
    
            <asp:BoundField DataField="QTY" HeaderText="QTY" />
        </Columns>
    </asp:GridView>
            <asp:Button ID="Save" runat="server" Text="SaveDB" OnClick="Save_Click" />
            
    <br />
    
        </div>
        </form>
    </body>
    </html>
    

    here is c# code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    
    
    namespace WebApplication1
    {
        public partial class MasterDetails : System.Web.UI.Page
        {
            int IPID = 0;
            SqlConnection con = new SqlConnection("Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=SPS;MultipleActiveResultSets=True;");
    
            DataTable dt = new DataTable();
            DataRow dr;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    dt.Columns.Add("Descriptionitem");
                    dt.Columns.Add("Codeitem");
                    dt.Columns.Add("_PID");
    
                    dt.Columns.Add("QTY");
                    ViewState["dt"] = dt;
                    itemload();
                }
            }
    
            private void itemload()
            {
                con.Open();
                SqlDataAdapter adpr1 = new SqlDataAdapter("select * from ItemMasterFile ", con);
                DataSet dspr1 = new DataSet();
                adpr1.Fill(dspr1);
                DropDownList1.DataSource = dspr1.Tables[0];
                DropDownList1.DataTextField = "Descriptionitem";
                DropDownList1.DataValueField = "Codeitem";
                DropDownList1.DataBind();
            }
    
    
            //Save Data Into Gridview//
    
            protected void GVadd_Click(object sender, EventArgs e)
            {
                dt = ViewState["dt"] as DataTable;
                dr = dt.NewRow();
                //  dr["Codeitem"] = DropDownList1.SelectedValue;
    
                dr["Descriptionitem"] = DropDownList1.SelectedItem.Text.Trim();
                dr["Codeitem"] = DropDownList1.SelectedItem.Value;
                dr["QTY"] = txtqty.Text;
                dt.Rows.Add(dr);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                clear();
            }
    
            private void clear()
            {
                // Codeitem.Text = "";
                txtqty.Text = "";
            }
    
    
            //Save Data into Database//
    
            protected void Save_Click(object sender, EventArgs e)
            {
    
    
               // int _PID = 0;
                using (SqlCommand cmd = new SqlCommand("Packinsert", con))
                {
    
    
                    cmd.CommandType = CommandType.StoredProcedure;
         
                    cmd.Parameters.AddWithValue("@PName", txtName.Text);
    
                    con.Open();
                    _PID = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();
    
                    DataTable dt = (DataTable)ViewState["dt"];
                    int codeitem, qty;
    
                    foreach (DataRow row in dt.Rows)
                    {
    
                        //pid = int.Parse(row["_PID"].ToString());
    
                        qty = int.Parse(row["QTY"].ToString());
                        codeitem = int.Parse(row["Codeitem"].ToString());
    
                        this.InsertRows(codeitem, qty);
                    }
                }
            }
            private void InsertRows(int codeitem, int qty)
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand("Insert_PackDetail", con))
                {
                  
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@PID", _PID);
                    cmd.Parameters.AddWithValue("@CodeItem", codeitem);
                    cmd.Parameters.AddWithValue("@QTY", qty);
                   
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
    
            public int _PID { get; set; } 
        }
    
    
    }
    
    
    
    

    • Marked as answer by Akhterhussain Wednesday, August 7, 2019 5:34 PM
    Wednesday, August 7, 2019 5:34 PM

All replies

  • You might consider asking in the data forums for ASP.NET and also consider using Entity Framework Core.

    https://forums.asp.net/24.aspx/1?Web+Forms+Data+Controls


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, August 6, 2019 6:59 PM
  • Hi Akhterhussain,

    Thank you for posting here.

    According to your description,  I couldn’t understand your requirement clearly.

    Do you mean you want to know how to CURD the child table in asp.net web form?
    If this is your requirement, I suggest you could try to use asp.net web form gridview to achieve  your requirement.

    Details about how to use gridview to CRUD the data table, you could refer to below article:
    https://www.aspsnippets.com/Articles/GridView-CRUD-Select-Insert-Edit-Update-Delete-using-Single-Stored-Procedure-in-ASPNet.aspx

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 7, 2019 2:18 AM
  • Hi Jack 

    thanks for reply ...

    Do you mean you want to know how to CURD the child table in asp.net web form?

    Yes i want to know that how data will insert into master and child table using web form c# and 

    yes i will use gridview ,but i want to add data in gridview from textbox and dropdownlist .

    Here is html i have tried effort 

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            CodeItems :
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
            Qty :
    <asp:TextBox runat="server" ID="txtqty" Height="16px" Width="53px" />
            
    
            <asp:Button Text="Insert" runat="server" OnClick="GVadd_Click" />
    
        
    <br />
    
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Codeitem" HeaderText="Code" />
             <%--<asp:BoundField DataField="Descriptionitem" HeaderText="Name" />--%>
            <asp:BoundField DataField="QTY" HeaderText="QTY" />
        </Columns>
    </asp:GridView>
            
    <br />
    
        </div>
        </form>
    </body>
    </html>
    
    here is c# 
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace WebApplication1
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            SqlConnection con = new SqlConnection("Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=SPS;MultipleActiveResultSets=True;");
    
            DataTable dt = new DataTable();
            DataRow dr;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    dt.Columns.Add("Codeitem");
                   // dt.Columns.Add("Descriptionitem");
                    dt.Columns.Add("QTY");
                    ViewState["dt"] = dt;
                    itemload();
                }
            }
    
            private void itemload()
            {
                con.Open();
                SqlDataAdapter adpr1 = new SqlDataAdapter("select * from ItemMasterFile ", con);
                DataSet dspr1 = new DataSet();
                adpr1.Fill(dspr1);
                DropDownList1.DataSource = dspr1.Tables[0];
                DropDownList1.DataTextField = "Descriptionitem";
                DropDownList1.DataValueField = "Codeitem";
                DropDownList1.DataBind();
            }
    
            protected void GVadd_Click(object sender, EventArgs e)
            {
                dt = ViewState["dt"] as DataTable;
                dr = dt.NewRow();
                dr["Codeitem"] = DropDownList1.SelectedValue;
               // dr["Descriptionitem"] = DropDownList1.SelectedItem.Text.Trim();
    
                dr["QTY"] = txtqty.Text;
                dt.Rows.Add(dr);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                clear();
            }
    
            private void clear()
            {
               // Codeitem.Text = "";
                txtqty.Text = "";
            }
        }
    }

    Note: i want to insert data datatextfield and datavaluefield from dropdownlist to gridview .... then i will save multiple data from gridview to database?

    • Edited by Akhterhussain Wednesday, August 7, 2019 4:58 AM More clearification
    Wednesday, August 7, 2019 4:50 AM
  • Hi Akherhussian,

    Thanks for the feedback.

    I have modified the code, it could add the data from dropdownlist and textbox to datagridview.

    Html:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            CodeItems :
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
            Qty :
    <asp:TextBox runat="server" ID="txtqty" Height="16px" Width="53px" />
            
    
            <asp:Button Text="Insert" runat="server" OnClick="BtnInsert_Click"  />
    
        
    <br />
    
    
        <asp:GridView ID="GridView1" runat="server" >
        
    </asp:GridView>
            
    <br />
    
        </div>
        </form>
    </body>
    </html>

    Code:

    public partial class WebForm2 : System.Web.UI.Page
        {
            SqlConnection con = new SqlConnection(@"string");
    
            DataTable dt = new DataTable();
            DataRow dr;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    itemload();
                    DropDownList1.Items.Add("Test1");
                    DropDownList1.Items.Add("Test2");
                    DropDownList1.Items.Add("Test3");
                    DropDownList1.Items.Add("Test4");
                }
            }
            private void itemload()
            {
                con.Open();
                SqlDataAdapter adpr1 = new SqlDataAdapter("select * from ItemMasterFile ", con);
                DataSet dspr1 = new DataSet();
                adpr1.Fill(dspr1);
                GridView1.DataSource= dspr1;
                GridView1.DataBind();
                con.Close();
    
            }
    
            protected void BtnInsert_Click(object sender, EventArgs e)
            {
                string sql = string.Format("insert into ItemMasterFile (Codeitem,Qty) values('{0}','{1}')", DropDownList1.SelectedValue, txtqty.Text);
                SqlCommand command = new SqlCommand(sql, con);
                con.Open();
                command.ExecuteNonQuery();
                con.Close();
    
                GridView1.DataSource = null;
                itemload();
                clear();
    
            }
            private void clear()
            {
                txtqty.Text = " ";
            }
        }

    Database:

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 7, 2019 6:39 AM
  • Hi Jack ,

    i told you that i will not insert text from dropdownlist into database ,i will call data in dropdownlist like below

      private void itemload()
            {
                con.Open();
                SqlDataAdapter adpr1 = new SqlDataAdapter("select * from ItemMasterFile ", con);
                DataSet dspr1 = new DataSet();
                adpr1.Fill(dspr1);
                DropDownList1.DataSource = dspr1.Tables[0];
                DropDownList1.DataTextField = "Descriptionitem";
                DropDownList1.DataValueField = "Codeitem";
                DropDownList1.DataBind();
            }

    then i will insert codeitem int value into database means that 

    DropDownList1.DataValueField = "Codeitem";

    will get insert ..

    thanks for supporting alot

    Wednesday, August 7, 2019 7:56 AM
  • Hi Akhterhussian,

    Thanks for the feedback.

    I want to know what is your current problem. Do you mean convert code developed on windows form to web form?

    If not, please tell me what do you want to do.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 7, 2019 8:23 AM
  • Hi Jack...

    Yes i want convert into webform

    Wednesday, August 7, 2019 9:33 AM
  • Hi Akhterhussian,

    Thanks for the feedback.

    >>Yes i want convert into webform

    I have to say that it is a large project.  Therefore, I suggest that you could post it in the asp.net forums.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 7, 2019 9:49 AM
  • ok Jack thanks...
    Wednesday, August 7, 2019 10:01 AM
  • ASP.NET issues can be discussed at the ASP.NET forums.

    http://forums.asp.net/

    Wednesday, August 7, 2019 11:49 AM
  • Please post questions related to ASP.NET in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, August 7, 2019 1:42 PM
  • here is sample for Master Detail Form....

    HTML

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MasterDetails.aspx.cs" Inherits="WebApplication1.MasterDetails" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Master Detail Form</title>
        </head>
    <body>
      <form id="form1" runat="server">
        <div>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Master Detail
            <br />
            <br />
            Customer Name : <asp:TextBox runat="server" ID="txtName" Height="16px" Width="124px" />
    
            <br />
            <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    
            Child Detail
            <br />
            <br />
            CodeItems : <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
            Qty :
    <asp:TextBox runat="server" ID="txtqty" Height="16px" Width="53px" />
            
    
            <asp:Button Text="Insert" runat="server" OnClick="GVadd_Click" />
    
        
    <br />
    
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
              <asp:BoundField DataField="Codeitem" HeaderText="Codeitem" Visible="False"/>
                   <asp:BoundField DataField="_PID" HeaderText="PID" Visible="False"/>
            <asp:BoundField DataField="Descriptionitem" HeaderText="Descriptionitem" />
    
            <asp:BoundField DataField="QTY" HeaderText="QTY" />
        </Columns>
    </asp:GridView>
            <asp:Button ID="Save" runat="server" Text="SaveDB" OnClick="Save_Click" />
            
    <br />
    
        </div>
        </form>
    </body>
    </html>
    

    here is c# code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    
    
    namespace WebApplication1
    {
        public partial class MasterDetails : System.Web.UI.Page
        {
            int IPID = 0;
            SqlConnection con = new SqlConnection("Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=SPS;MultipleActiveResultSets=True;");
    
            DataTable dt = new DataTable();
            DataRow dr;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    dt.Columns.Add("Descriptionitem");
                    dt.Columns.Add("Codeitem");
                    dt.Columns.Add("_PID");
    
                    dt.Columns.Add("QTY");
                    ViewState["dt"] = dt;
                    itemload();
                }
            }
    
            private void itemload()
            {
                con.Open();
                SqlDataAdapter adpr1 = new SqlDataAdapter("select * from ItemMasterFile ", con);
                DataSet dspr1 = new DataSet();
                adpr1.Fill(dspr1);
                DropDownList1.DataSource = dspr1.Tables[0];
                DropDownList1.DataTextField = "Descriptionitem";
                DropDownList1.DataValueField = "Codeitem";
                DropDownList1.DataBind();
            }
    
    
            //Save Data Into Gridview//
    
            protected void GVadd_Click(object sender, EventArgs e)
            {
                dt = ViewState["dt"] as DataTable;
                dr = dt.NewRow();
                //  dr["Codeitem"] = DropDownList1.SelectedValue;
    
                dr["Descriptionitem"] = DropDownList1.SelectedItem.Text.Trim();
                dr["Codeitem"] = DropDownList1.SelectedItem.Value;
                dr["QTY"] = txtqty.Text;
                dt.Rows.Add(dr);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                clear();
            }
    
            private void clear()
            {
                // Codeitem.Text = "";
                txtqty.Text = "";
            }
    
    
            //Save Data into Database//
    
            protected void Save_Click(object sender, EventArgs e)
            {
    
    
               // int _PID = 0;
                using (SqlCommand cmd = new SqlCommand("Packinsert", con))
                {
    
    
                    cmd.CommandType = CommandType.StoredProcedure;
         
                    cmd.Parameters.AddWithValue("@PName", txtName.Text);
    
                    con.Open();
                    _PID = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();
    
                    DataTable dt = (DataTable)ViewState["dt"];
                    int codeitem, qty;
    
                    foreach (DataRow row in dt.Rows)
                    {
    
                        //pid = int.Parse(row["_PID"].ToString());
    
                        qty = int.Parse(row["QTY"].ToString());
                        codeitem = int.Parse(row["Codeitem"].ToString());
    
                        this.InsertRows(codeitem, qty);
                    }
                }
            }
            private void InsertRows(int codeitem, int qty)
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand("Insert_PackDetail", con))
                {
                  
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@PID", _PID);
                    cmd.Parameters.AddWithValue("@CodeItem", codeitem);
                    cmd.Parameters.AddWithValue("@QTY", qty);
                   
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
    
            public int _PID { get; set; } 
        }
    
    
    }
    
    
    
    

    • Marked as answer by Akhterhussain Wednesday, August 7, 2019 5:34 PM
    Wednesday, August 7, 2019 5:34 PM