简体   繁体   中英

Put rows into gridView one by one whenever on click event fires in asp.net

I want to add data(or row) in the grid whenever I click on button.

So code behind of button is like whenever a user click on button a parameterize the stored procedure called which gets the data from SQL Server. So each time it will get only one record.

Problem is that when user click on button it successfully get first row and then again when user click on button it will override the data present in row[0].

I want whenever user click another time. the data comes in next row and data present in previous row remains same.

On Click button method:

protected void Button5_Click(object sender, EventArgs e)
{
    getLotteryApplications();
}

private void getLotteryApplications()
{
        try
        {
            int serialNumber = 0, preference = 0;

            if (SerialNum.Text != "0")
            {
                serialNumber = Int32.Parse(SerialNum.Text.ToString());
            }

            if (ddlPreferenceLottery.SelectedValue != "0")
            {
                preference = Int32.Parse(ddlPreferenceLottery.SelectedValue.ToString());
            }

            DataTable dt = DL_School_Detail.getLotteryApplications(serialNumber, preference);
            selectedStudentView.DataSource = dt;
            selectedStudentView.DataBind();
        }
        catch (Exception ex)
        {
        }
}

Calling class:

public static DataTable getLotteryApplications(int serial,int preference)
{
    DataTable dt = new DataTable();

    try
    {
        using (SqlConnection con = new SqlConnection(Connect.getConnection()))
        {
            using (SqlCommand cmd = new SqlCommand("[dbo].[GetLotterySelectedCandidates]", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@serialNumber", serial);
                cmd.Parameters.AddWithValue("@preference", preference);

                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    using (DataSet ds = new DataSet())
                    {
                        da.Fill(ds);
                        dt = ds.Tables[0];
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
    }

    return dt;
}
private void AddNewRowToGrid()
    {
           try
          {
            int serialNumber = 0, preference = 0;
            if (SerialNum.Text != "0")
            {
                serialNumber = Int32.Parse(SerialNum.Text.ToString());
            }
            if (ddlPreferenceLottery.SelectedValue != "0")
            {
                preference = Int32.Parse(ddlPreferenceLottery.SelectedValue.ToString());
            }
            DataTable griddt;
            DataTable griddt2;
            if (ViewState["Row"] != null)
            {
                griddt = (DataTable)(ViewState["Row"]);
                DataRow dr = null;
                if (griddt.Rows.Count > 0)
                {
                    griddt2 = DL_School_Detail.getLotteryApplications(serialNumber, preference);
                    griddt.Merge(griddt2, true, MissingSchemaAction.Ignore);
                    if (griddt.Rows.Count > 0)
                    {
                        ViewState["Row"] = griddt;
                        ContentPlaceHolder1_selectedStudentView.DataSource = griddt;

                        ContentPlaceHolder1_selectedStudentView.DataBind();
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('कृपया सही सीरियल नंबर और  प्राथमिकता चुने !')", true);
                    }
                }

            }
            else
            {
                DataTable dt = DL_School_Detail.getLotteryApplications(serialNumber, preference);
                //dt.Merge(griddt, true, MissingSchemaAction.Ignore);
                if (dt.Rows.Count > 0)
                {
                    ViewState["Row"] = dt;
                    ContentPlaceHolder1_selectedStudentView.DataSource = ViewState["Row"];

                    ContentPlaceHolder1_selectedStudentView.DataBind();
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('कृपया सही सीरियल नंबर और  प्राथमिकता चुने !')", true);
                }
            }
        }
        catch (Exception ex)
        {

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM