locked
Easy invoice form RRS feed

  • Question

  • User-1016229741 posted

    so i have learned some basics through basic struggle in asp.net C# and help of forums. it seems though, in my particular issue, i take one step forward and two steps back. i want to create a basic invoice form. header information such as company, etc. in the top part of the form. details in a list view so i can enter quantities, part numbers, dollar amounts. i can pass values from top/header of form to list view. i can also create a list view of what i want through a sql data source which ID of list view equals ID of one of the header text boxes. but i can't put the two together. i can't pass values from header portion to list view and also have the data source from sql (get the 2 data source error). so then i think i can just do a manual insert of text boxes (header) and columns of list view (detail) but can't figure that out.

    so basically trying to see if there is a solution. pass values from data table to list view AND have another data source? manual insert statement instead of the insert template of list view that i can grab values from text boxes outside of list view and also grab values of columns of list view?

    or is there just such a more simple way that i can forget what i have spent two weeks on; abandon those thoughts, and start anew with something else? frustrated. something i can't devote all my time to but it is a requirement i need completed in near future. can't use outside software or solutions other than asp.net/C#. any turn in the right direction would be great. thanks.

    Sunday, May 6, 2018 4:39 PM

Answers

  • User283571144 posted

    Hi osupratt1234,

    1st column would be auto-populated from text box on the form which i'll call header section. the 2nd and 3rd column of the list view would come from a SQL database query. it would have a WHERE clause to where the text box value which is also column 1 of the list view are equal. 1st column auto-populated and 2nd and 3rd would be data entry by end-user. there would be an insert button and add button at end of row. enter data click insert then add if there is another row to enter data for. pretty simple. i can auto-populate the 1st text box fine but can't get the rest of what should be normal to work.

    According to your description, I suggest you could bind the listview datasouce in the code-behind.

    You could create a datatable and store the datatble in the viewstate when the page first loaded.

    In the add button click event, you could select the database and add new row to the viewstate datatable and rebind the listview.

    More details, you could refer to below example:

    ASPX:

        <form id="form1" runat="server">
            <div>
                <asp:Label ID="lblOrderID" Text="Order ID" runat="server"></asp:Label>
                <asp:TextBox ID="txtOrderID" runat="server"></asp:TextBox>
                <asp:ListView ID="ListView1" runat="server" DataKeyNames="OrderID">
     
                    <LayoutTemplate>
                        <table runat="server">
                            <tr runat="server">
                                <td runat="server">
                                    <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                        <tr runat="server" style="">
                                            <th runat="server">OrderID</th>
                                            <th runat="server">ItemName</th>
                                            <th runat="server">Quantity</th>
                                
                                        </tr>
    
                                        <tr id="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr runat="server">
                                <td runat="server" style=""></td>
                            </tr>
                            <tfoot>
                                <tr>
                                    <td>
                                        <asp:Button ID="Button1" runat="server" Text="Add new row" OnClick="Button1_Click" />
                                        <asp:Button ID="Button2" runat="server" Text="Insert" OnClick="Button2_Click" />
                                    </td>
                                    <td runat="server" style=""></td>
                                    <td runat="server" style=""></td>
                                </tr>
                            </tfoot>
                        </table>
                    </LayoutTemplate>
                    <ItemTemplate>
                        <tr style="">
     
                            <td>
                                <asp:Label ID="OrderID" runat="server" Text='<%# Eval("OrderID") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="Quantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:TextBox>
                             </td>
                            <td>
                               <asp:TextBox ID="ItemNameLabel" runat="server" Text='<%# Eval("ItemName") %>'></asp:TextBox>
                            </td>
                        </tr>
                    </ItemTemplate>
    
                </asp:ListView>
    
            </div>
        </form>

    Code-behind:

           protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                  BindListView("");
                }
            }
    
            public void BindListView(string OrderId)
            {
    
                if (string.IsNullOrEmpty(OrderId))
                {
                    DataTable  dt = new DataTable();
                    dt.Columns.Add("ItemName");
                    dt.Columns.Add("Quantity");
                    dt.Columns.Add("OrderID");
                    ViewState["table"] = dt;
                    dt.Rows.Add(GetNewDataRow("1"));
                    ListView1.DataSource = dt;
                    ViewState["table"] = dt;
                    ListView1.DataBind();  
                }
                else
                {
                  var d1 =GetNewDataRow(OrderId);
                    var dt = (DataTable)ViewState["table"];
                    dt.Rows.Add(d1);
                    ListView1.DataSource = dt;
                    ListView1.DataBind();
                }
    
            }
    
            public  DataRow GetNewDataRow(string OrderId)
            {
                var dt = (DataTable)ViewState["table"];
                DataRow d1 =   dt.NewRow();
                string connectionString =
    @"Data Source=(localdb)\MSSQLLocalDB;aaaaa;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                string queryString = "SELECT ItemName, Quantity, OrderID From orderlist Where OrderID =@OrderID";
    
                int paramValue;
                if (int.TryParse(OrderId, out paramValue))
                {
                    using (SqlConnection connection =
                 new SqlConnection(connectionString))
                    {
                        // Create the Command and Parameter objects.
                        SqlCommand command = new SqlCommand(queryString, connection);
                        command.Parameters.AddWithValue("@OrderID", paramValue);
                        connection.Open();
                        SqlDataReader reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            d1["ItemName"] = reader[0];
                            d1["Quantity"] = reader[1];
                            d1["OrderID"] = reader[2];
                        }
                        reader.Close();
                    }
                }
                else
                {
    
                }
                return d1;
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                BindListView(txtOrderID.Text);
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                foreach (ListViewItem itemRow in  ListView1.Items)
                {
                    string Quantity = ((TextBox)(itemRow.FindControl("Quantity"))).Text;
                        string ItemNameLabel = ((TextBox)(itemRow.FindControl("ItemNameLabel"))).Text;
                    //Then you could write sql query to insert the listview item to database
                }
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 14, 2018 8:53 AM

All replies

  • User475983607 posted

    There is no reason why you can't have two data bound controls on a page where one is a header and the other, details of some sort.

    so basically trying to see if there is a solution. pass values from data table to list view AND have another data source?

    You have total control over retrieving data sets an can have as many data bound controls on a page as you like. 

    Can you explain the problem you are having specifically.   Are you having a problem using point and click features like the Sql Data source?  Writing queries?

    Sunday, May 6, 2018 5:09 PM
  • User-1016229741 posted

    ideally i want a text box value (actually multiple text box values) to pass to the list view. this is a primary key of the header and a foreign key of the details or list view. i would like to have the list view behave like the insert template and show as many rows as the end user enters or creates. then of course have all of this details info be  inserted into the back-end SQL table. how do i create an insert statement to where i grab the text box values from header part and also column/row data out of list view? the foreign key is again auto-populated in list view but should also be part of the insert template and insert statement and i can't figure it out. the way i am currently passing this value is from data table:

    DataTable dt = new DataTable();
    dt.Columns.Add("txtFieldTicketDate", typeof(DateTime));
    dt.Columns.Add("txtFieldTicketNumber", typeof(string));
    dt.Columns.Add("txtSONumber", typeof(int));

    DataRow r = dt.NewRow();
    r["txtFieldTicketDate"] = txtFieldTicketDate.Text;
    r["txtFieldTicketNumber"] = txtFieldTicketNumber.Text;
    r["txtSONumber"] = txtSONumber.Text;
    dt.Rows.Add(r);
    ListView1.DataSource = dt;
    ListView1.DataBind();

    Sunday, May 6, 2018 11:54 PM
  • User-1016229741 posted

    Maybe easier way to explain this is i want list view values to be inserted into back-end SQL database. simple example would be three columns in list view. 1st column would be auto-populated from text box on the form which i'll call header section. the 2nd and 3rd column of the list view would come from a SQL database query. it would have a WHERE clause to where the text box value which is also column 1 of the list view are equal. 1st column auto-populated and 2nd and 3rd would be data entry by end-user. there would be an insert button and add button at end of row. enter data click insert then add if there is another row to enter data for. pretty simple. i can auto-populate the 1st text box fine but can't get the rest of what should be normal to work.

    Monday, May 7, 2018 12:05 AM
  • User283571144 posted

    Hi osupratt1234,

    1st column would be auto-populated from text box on the form which i'll call header section. the 2nd and 3rd column of the list view would come from a SQL database query. it would have a WHERE clause to where the text box value which is also column 1 of the list view are equal. 1st column auto-populated and 2nd and 3rd would be data entry by end-user. there would be an insert button and add button at end of row. enter data click insert then add if there is another row to enter data for. pretty simple. i can auto-populate the 1st text box fine but can't get the rest of what should be normal to work.

    According to your description, I suggest you could bind the listview datasouce in the code-behind.

    You could create a datatable and store the datatble in the viewstate when the page first loaded.

    In the add button click event, you could select the database and add new row to the viewstate datatable and rebind the listview.

    More details, you could refer to below example:

    ASPX:

        <form id="form1" runat="server">
            <div>
                <asp:Label ID="lblOrderID" Text="Order ID" runat="server"></asp:Label>
                <asp:TextBox ID="txtOrderID" runat="server"></asp:TextBox>
                <asp:ListView ID="ListView1" runat="server" DataKeyNames="OrderID">
     
                    <LayoutTemplate>
                        <table runat="server">
                            <tr runat="server">
                                <td runat="server">
                                    <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                        <tr runat="server" style="">
                                            <th runat="server">OrderID</th>
                                            <th runat="server">ItemName</th>
                                            <th runat="server">Quantity</th>
                                
                                        </tr>
    
                                        <tr id="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr runat="server">
                                <td runat="server" style=""></td>
                            </tr>
                            <tfoot>
                                <tr>
                                    <td>
                                        <asp:Button ID="Button1" runat="server" Text="Add new row" OnClick="Button1_Click" />
                                        <asp:Button ID="Button2" runat="server" Text="Insert" OnClick="Button2_Click" />
                                    </td>
                                    <td runat="server" style=""></td>
                                    <td runat="server" style=""></td>
                                </tr>
                            </tfoot>
                        </table>
                    </LayoutTemplate>
                    <ItemTemplate>
                        <tr style="">
     
                            <td>
                                <asp:Label ID="OrderID" runat="server" Text='<%# Eval("OrderID") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="Quantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:TextBox>
                             </td>
                            <td>
                               <asp:TextBox ID="ItemNameLabel" runat="server" Text='<%# Eval("ItemName") %>'></asp:TextBox>
                            </td>
                        </tr>
                    </ItemTemplate>
    
                </asp:ListView>
    
            </div>
        </form>

    Code-behind:

           protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                  BindListView("");
                }
            }
    
            public void BindListView(string OrderId)
            {
    
                if (string.IsNullOrEmpty(OrderId))
                {
                    DataTable  dt = new DataTable();
                    dt.Columns.Add("ItemName");
                    dt.Columns.Add("Quantity");
                    dt.Columns.Add("OrderID");
                    ViewState["table"] = dt;
                    dt.Rows.Add(GetNewDataRow("1"));
                    ListView1.DataSource = dt;
                    ViewState["table"] = dt;
                    ListView1.DataBind();  
                }
                else
                {
                  var d1 =GetNewDataRow(OrderId);
                    var dt = (DataTable)ViewState["table"];
                    dt.Rows.Add(d1);
                    ListView1.DataSource = dt;
                    ListView1.DataBind();
                }
    
            }
    
            public  DataRow GetNewDataRow(string OrderId)
            {
                var dt = (DataTable)ViewState["table"];
                DataRow d1 =   dt.NewRow();
                string connectionString =
    @"Data Source=(localdb)\MSSQLLocalDB;aaaaa;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                string queryString = "SELECT ItemName, Quantity, OrderID From orderlist Where OrderID =@OrderID";
    
                int paramValue;
                if (int.TryParse(OrderId, out paramValue))
                {
                    using (SqlConnection connection =
                 new SqlConnection(connectionString))
                    {
                        // Create the Command and Parameter objects.
                        SqlCommand command = new SqlCommand(queryString, connection);
                        command.Parameters.AddWithValue("@OrderID", paramValue);
                        connection.Open();
                        SqlDataReader reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            d1["ItemName"] = reader[0];
                            d1["Quantity"] = reader[1];
                            d1["OrderID"] = reader[2];
                        }
                        reader.Close();
                    }
                }
                else
                {
    
                }
                return d1;
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                BindListView(txtOrderID.Text);
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                foreach (ListViewItem itemRow in  ListView1.Items)
                {
                    string Quantity = ((TextBox)(itemRow.FindControl("Quantity"))).Text;
                        string ItemNameLabel = ((TextBox)(itemRow.FindControl("ItemNameLabel"))).Text;
                    //Then you could write sql query to insert the listview item to database
                }
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 14, 2018 8:53 AM