kapil.tandon 0 Newbie Poster
private void AddRecordToGrid()
        {
            try
            {

datatable dt = new datable();
                    if (Session["SampleDataTable"] == null)
                    {
                    dt.Columns.Add(new DataColumn("ID", typeof(string)));
                    dt.Columns.Add(new DataColumn("Qtr", typeof(string)));

                    dt.Columns.Add(new DataColumn("Exp", typeof(string)));
                    dt.Columns.Add(new DataColumn("Partner", typeof(string)));
                    dt.Columns.Add(new DataColumn("Net", typeof(string)));
                    dr = dt.NewRow();
                        }
                    else
                    {
                        dt= (DataTable)Session["SampleDataTable"];
                    }

                                       dr = dt.NewRow();


                    if (dr.IsNull(0))
                    {
                        //dr = dt.NewRow();
                        dr[0] = txtD.Text;
                        dr[1] = cbQuarter.SelectedItem.Text;

                        dr[2] =Exp.Text;
                        dr[3] = txtPartner.Text;
                        dr[4] = lblNet.Text;
                        dt.Rows.Add(dr);
                        DataView dv = new DataView(dt);
                        grdBudget.DataSource = dv;
                        grdBudget.DataBind();
                    }
                    for (int i = 0; i < grdBudget.Rows.Count; i++)
                    {

                        if (!dr.IsNull(0))
                        {
                            if (txtBudgetID.Text != dr[0].ToString())
                            {
                                dr = dt.NewRow();
                                dr[0] = txtD.Text;
                                dr[1] = cboQuarter.SelectedItem.Text;

                                dr[2] = Exp.Text;
                                dr[3] = txtPartner.Text;
                                dr[4] = lblNet.Text;
                                dt.Rows.Add(dr);
                                DataView dv = new DataView(dt);
                                grdBudget.DataSource = dv;
                                grdBudget.DataBind();
                            }
                        }
                    }
                    Session["SampleDataTable"] = dt;

                }

            
            catch (Exception ex)
            {
            }

the problem i m facing wid this code id that the new data row is added every time when this function is called and it enters ito the loop of the if(dr.isnull(0)) . i want only the values with unique id shud be entered. if the value wid the existin id id entere, it shud not be allowed to enter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.