Click here to Skip to main content
15,886,840 members

unable to add data in datatable through looping

shivani 2013 asked:

Open original thread
I have one form in which 5 textboxes for product id ,qty,price,amount..when i enter the data and press "add" button it is getting placed in gridview.....but when i add another product with some other qty it addd it and remove the previous.it is showing one data which is currently.i don't want to loose previous it should add row wise...how can i achieve this.following is the coding

C#
   protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtDetails = new DataTable();
        DataTable dt = new DataTable();
        dt = objprdt.getproducts();
        ddlprdcts.DataSource = dt;
        ddlprdcts.DataValueField = "Product_Id";
        ddlprdcts.DataTextField = "Product_Name";
        ddlprdcts.DataBind();
        ddlprdcts.Items.Add(new ListItem("- Please Select -", "-1"));
        ddlprdcts.SelectedValue = "-1";


         if (!this.IsPostBack)
            {
 dtDetails.Clear();
                if (dtDetails.Columns.Count == 0)
                {
                    dtDetails.Columns.Add("sl_no", typeof(int));
                    dtDetails.Columns.Add("productname", typeof(string));
                    dtDetails.Columns.Add("qty", typeof(double));
                    dtDetails.Columns.Add("price", typeof(decimal));
                    dtDetails.Columns.Add("amount", typeof(long));
                }
}

C#
public void filldtDetails()
       {
           dtDetails.Clear();
           if (dtDetails.Columns.Count == 0)
           {

                   //dtDetails.Columns.Add("sl_no", typeof(int));
                   dtDetails.Columns.Add("productname", typeof(string));
                   dtDetails.Columns.Add("qty", typeof(double));
                   dtDetails.Columns.Add("price", typeof(decimal));
                   dtDetails.Columns.Add("amount", typeof(double));


           }
           if (grdDetails.Rows.Count != 0)
           {
               for (int i = 0; i < Convert.ToInt32(ddlprdcts.Items.Count); i++)
               {
                   dtDetails.Rows.Add(grdDetails.Rows[i].Cells[1].Text, grdDetails.Rows[i].Cells[2].Text, grdDetails.Rows[i].Cells[3].Text, grdDetails.Rows[i].Cells[4].Text);

                   //dtDetails.Rows.Add(grdDetails.Rows[i].Cells[1].Text, Convert.ToDouble(grdDetails.Rows[i].Cells[2].Text), Convert.ToDecimal(grdDetails.Rows[i].Cells[3].Text), Convert.ToDouble(grdDetails.Rows[i].Cells[4].Text));

               }

           }
       }

on button click

C#
filldtDetails();
        DataRow dr1 = dtDetails.NewRow();
        //dr1[0] = TextBox2.Text;
        dr1[0] = TextBox2.Text;
        dr1[1] = TextBox4.Text;
        dr1[2] = TextBox3.Text;
        dr1[3] = TextBox5.Text;
        dtDetails.Rows.Add(dr1);
        grdDetails.DataSource = dtDetails;
        grdDetails.DataBind();


on source code
XML
Enter Email ID    <asp:TextBox ID="TextBox1" runat="server" 
        style="margin-bottom: 0px"></asp:TextBox>
 <br />
    <br />
Name                
    <asp:TextBox ID="TextBox2" runat="server" ontextchanged="TextBox2_TextChanged"></asp:TextBox>
                      
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Select" />
    <br />
 product            
    <asp:DropDownList ID="ddlprdcts" runat="server" 
        onselectedindexchanged="ddlprdcts_SelectedIndexChanged">    </asp:DropDownList>
                                    
    <asp:Button ID="select" runat="server" onclick="select_Click" Text="select" />
    <br />
    <br />
 product cost    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <br />
 Product qty      <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <br />
    <br />
 product total amount    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
     
    <asp:Button ID="Button3" runat="server" onclick="Button3_Click" 
        Text="calculate" />
                     
    <asp:Button ID="Button2" runat="server" Height="22px" onclick="Button2_Click" 
        Text="Add to cart" Width="79px" />
    <br />
    <asp:GridView ID="grdDetails" runat="server" Height="139px" Width="507px">
        <Columns>
            <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" />
            <asp:ButtonField ButtonType="Button" CommandName="Delete" Text="remove" />
        </Columns>
    </asp:GridView>
    <br />
    <br />
    <asp:Button ID="Button4" runat="server" Height="26px" Text="total amount" />
     
    <asp:Label ID="Label1" runat="server"></asp:Label>



i don't want to add it into database.i just want to show it runtime only
pls help
Tags: C# (C# 4.0), ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900