I'm following this one answer suggested by user Adil on here: How to insert data from Textboxes into GridView on button click in ASP.NET and tried to implement his code.

However, if GridView1 is already bound to an sqlDataSource, how do I go about preventing the Server Error: Both DataSource and DataSourceID are defined on GridView2. Remove one definition?

The code that I'm following (with the line of codes that raises the server error) is:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridVIEWData();
        GridView1.DataSource = dt1;   //This is a line that raises the server error mentioned above
        GridView1.DataBind();  //This is also a line that raises the server error mentioned above
    }
}

private void gridVIEWData() {
   dt1.Columns.Add("pName", typeof(string));
   dt1.Columns.Add("pCategory", typeof(string));
   dt1.Columns.Add("price", typeof(string));
   dt1.Columns.Add("pQuantity", typeof(string));
   dt1.Columns.Add("totalPrice", typeof(string));
   Session["dtInSession"] = dt1;     //Saving Datatable To Session 
}


protected void Button3_Click(object sender, EventArgs e)
{
        if(Session["dtInSession"] != null)
             dt1 = (DataTable)Session["dtInSession"]; //Getting datatable from session 

        Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
        DataRow dr = dt1.NewRow();
        dr["pName"] = DropDownList2.SelectedItem;
        dr["pCategory"] = DropDownList1.SelectedItem;
        dr["price"] = txt_price.Text;
        dr["pQuantity"] = txt_quantity.Text;
        dr["totalPrice"] = total;
        dt1.Rows.Add(dr);

        Session["dtInSession"] = dt1;     //Saving Datatable To Session 
        GridView1.DataSource = dt1;  //This is a line that raises the server error mentioned above
        GridView1.DataBind();  //This is also a line that raises the server error mentioned above

    }
 }

Related posts

Recent Viewed