Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there,

I have a datatable dtValues which takes values from ddl,label,textbox on a button(Add) click.this button adds these values to the grid. I have used data table dtValues as datasource to gridview.

Now when these values are filled in data table/grid,On button click Add
it takes the previous values again and again into the grid.

My code for btnAdd is :-

protected void btnAdd_Click(object sender, EventArgs e)
       {
           if (ddlSearchItems.SelectedIndex > 0)
           {
               DataRow drValues = dtValues.NewRow();
               drValues["Id"] = ddlSearchItems.SelectedItem.Value;
               drValues["Item"] = ddlSearchItems.SelectedItem.Text;
               drValues["Rate"] = lblActualPrice.Text;
               drValues["Qty"] = txtQuantity.Text;
               drValues["Amount"] = Convert.ToInt32(lblActualPrice.Text) * Convert.ToInt32(txtQuantity.Text);
               dtValues.Rows.Add(drValues);
               dtValues.AcceptChanges();
               gvOrders.DataSource = dtValues;
               gvOrders.DataBind();
               //gvOrders.Columns[0].Visible = false;
               gvOrders.Visible = true;
           }
       }

The values stays in the grid until they are submitted by a button click(btnSubmit) on which i resets the data table.

How can i avoid that user is not able to put the same previous order in database on btnAdd_click?

I am using this to save the records into database--

C#
private void InsertOrder(int productId, int quantity)
       {
           
           objSqlHelper.InsertOrderInDB(txtName.Text, txtAddress.Text, Convert.ToInt32(txtPhone.Text), DateTime.Now, productId, quantity);
       }

;
Posted
Updated 14-Sep-10 23:43pm
v3
Comments
nagendrathecoder 15-Sep-10 6:36am    
For this, you can clear all values of lable, textbox etc etc after Add Button click. So that same values won't be saved to grid

1 solution

I am not too clear what you want to achieve here, can you please explain a bit more. :doh:

If you don't want to see previous records in GridView on btnAdd click, then you need to clear DataTable before assigning a new row to it.
 
Share this answer
 
Comments
AmitChoudhary10 15-Sep-10 6:07am    
i have a grid which takes values from ddl,textbox and label on add click.now on pressing add again and again same values are saved to the grid(which is what i don't want).what i am trying to do is to add multiple orders to the grid and then save it to the database on submit button click.
nagendrathecoder 15-Sep-10 6:35am    
Ok, for this, you can clear all values of lable, textbox etc etc after Add Button click. So that same values won't be saved to grid.
AmitChoudhary10 15-Sep-10 6:40am    
After Add click?..did not quite understand where are you talking about.

if i add this under my click event

txtAddress.Text = string.Empty;
txtName.Text = string.Empty;
txtPhone.Text = string.Empty;
txtQuantity.Text = string.Empty;
ddlSearchItems.SelectedIndex = -1;
lblActualPrice.Text = string.Empty;

I am getting exception due to the parameters under InsertOrder my method shown above.
AmitChoudhary10 15-Sep-10 6:42am    
what i want is that all fields are empty on button click so user is able to place multiple order in one go,and also add button does'nt show previous values in grid on hitting again and again.
nagendrathecoder 15-Sep-10 7:20am    
Clear the values after adding row to datatable and assigning datatable to gridview.
Clear values after this line "gvOrders.DataBind();"

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