Click here to Skip to main content
15,905,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 Dropdownlist and 3 Textboxes and I want to add data from textboxes and after a button click they will be added to GridView

What I have tried:

C#
DataRow dr;
        DataTable dt = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        { dt.Columns.Add(new DataColumn("itemname", typeof(string)));
            dt.Columns.Add(new DataColumn("itemprice", typeof(double)));
            dt.Columns.Add(new DataColumn("quantity", typeof(int)));
            dt.Columns.Add(new DataColumn("tax", typeof(double)));
            dt.Columns.Add(new DataColumn("finalamount", typeof(double))); 
          
        }
 protected void Button2_Click(object sender, EventArgs e)
        {
            dr = dt.NewRow();
            dr["itemname"] = DropDownList1.SelectedItem.Text;
            dr["itemprice"] = TextBox1.Text;
            dr["quantity"] = DropDownList2.Text;
            dr["tax"] = TextBox2.Text;
            dr["finalamount"] = TextBox3.Text;
            dt.Rows.Add(dr);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

my code is this
whan i click button "Column 'itemname' does not belong to table " this error is comeing please help me
Posted
Updated 4-May-16 0:31am
v3
Comments
kedar001 4-May-16 6:16am    
check your grid view Bound Field have same Data Field as you have taken in data table...
i.e.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<columns>
<asp:BoundField DataField="itemname" />
<asp:BoundField DataField="itemprice" />
CHill60 4-May-16 6:16am    
I would have expected the error to be "A column named 'itemname' already belongs to this DataTable."
You should be checking isPostBack before adding the columns to the datatable
Nagarjuna99 4-May-16 6:28am    
i'am adding boundfield but error is not rectified
Karthik_Mahalingam 4-May-16 6:32am    
check my solution.
Nagarjuna99 4-May-16 6:34am    
ya i will checked .

Are you certain the the function Page_Load is executed?
Looks to me like your columns are never added to the DataTable.

Have you tried to set a break point inside Page_Load to see if the debugger stops there?

And to avoid that you try to add the columns twice you could change the code like this
C#
DataTable dt = null;
protected void Page_Load(object sender, EventArgs e)
{ 
    dt = new DataTable();
    dt.Columns.Add(new DataColumn("itemname", typeof(string)));
    dt.Columns.Add(new DataColumn("itemprice", typeof(double)));
    dt.Columns.Add(new DataColumn("quantity", typeof(int)));
    dt.Columns.Add(new DataColumn("tax", typeof(double)));
    dt.Columns.Add(new DataColumn("finalamount", typeof(double)));
}

or
C#
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
    if (dt.Columns.Count == 0)
    {
        dt.Columns.Add(new DataColumn("itemname", typeof(string)));
        dt.Columns.Add(new DataColumn("itemprice", typeof(double)));
        dt.Columns.Add(new DataColumn("quantity", typeof(int)));
        dt.Columns.Add(new DataColumn("tax", typeof(double)));
        dt.Columns.Add(new DataColumn("finalamount", typeof(double)));
    }
}


[UPDATE]
Navigating through Code with the Debugger[^]
How to: Set a Simple Breakpoint[^]
 
Share this answer
 
v3
Comments
Nagarjuna99 4-May-16 6:32am    
how to set break point please tell me i'am fresher
George Jonsson 4-May-16 6:39am    
See my updated answer for links.
Using the debugger is one thing you really should learn.
This is how you understand how the code is executed and in what order.
You can check the value of a variable to see if it is null or not.
Learn it and learn it now. The debugger will become one of your best friends.
corrected version:
slowly debug and check what you have missed.

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dtTemp = new DataTable(); ;
                dtTemp.Columns.Add(new DataColumn("itemname", typeof(string)));
                dtTemp.Columns.Add(new DataColumn("itemprice", typeof(double)));
                dtTemp.Columns.Add(new DataColumn("quantity", typeof(int)));
                dtTemp.Columns.Add(new DataColumn("tax", typeof(double)));
                dtTemp.Columns.Add(new DataColumn("finalamount", typeof(double)));               
                Session["Data"] = dtTemp;
            }

        }

         

        protected void Button2_Click1(object sender, EventArgs e)
        {
            var dataTableFromSession = Session["Data"] as DataTable; 
                double itemPrice, tax, finalamount;
                int quantity;
                double.TryParse(TextBox1.Text, out itemPrice);
                int.TryParse(DropDownList2.Text, out quantity);
                double.TryParse(TextBox2.Text, out tax);
                double.TryParse(TextBox3.Text, out finalamount);
               var  dataRow = dataTableFromSession.NewRow();
                dataRow["itemname"] = DropDownList1.SelectedItem.Text;
                dataRow["itemprice"] = itemPrice;
                dataRow["quantity"] = quantity;
                dataRow["tax"] = tax;
                dataRow["finalamount"] = finalamount;
                dataTableFromSession.Rows.Add(dataRow); 
                Session["Data"] = dataTableFromSession; 
            GridView1.DataSource = dataTableFromSession;
            GridView1.DataBind();
        }
 
Share this answer
 
Comments
Nagarjuna99 4-May-16 6:38am    
ya its working sir,i have another small doubt that is header name was not come,what i do?
Nagarjuna99 4-May-16 6:43am    
i will add headertext .
thanks @karthik sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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