Click here to Skip to main content
15,917,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
when i m adding a new row that is not appearing in my grid-view. Again when i am debugging it is appearing.

C#
public partial class Default4 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con"]);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
            trlogin.Visible = false;
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        trlogin.Visible = true;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //Create stringbuilder to store multiple DML statements 
        StringBuilder strSql = new StringBuilder(string.Empty);

        //Create sql connection and command
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con"]);
        SqlCommand cmd = new SqlCommand();

        //Loop through gridview rows to find checkbox 
        //and check whether it is checked or not 
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkUpdate = (CheckBox)
               GridView1.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkUpdate != null)
            {
                if (chkUpdate.Checked)
                {
                    // Get the values of textboxes using findControl
                    string strID = GridView1.Rows[i].Cells[1].Text;
                    string strName = ((TextBox)
                        GridView1.Rows[i].FindControl("Vendor_Name")).Text;

                    string strAsset_Type = ((TextBox)
                        GridView1.Rows[i].FindControl("Asset_Type")).Text;
                    string Status = ((TextBox)
                     GridView1.Rows[i].FindControl("Status")).Text;

                    string Date_Of_Registration = ((TextBox)
                     GridView1.Rows[i].FindControl("Date_Of_Registration")).Text;
                    string Phone_Number = ((TextBox)
                     GridView1.Rows[i].FindControl("Phone_Number")).Text;
                    string Email = ((TextBox)
                     GridView1.Rows[i].FindControl("Email")).Text;




                    try
                    {
                        string strUpdate = "UPDATE [VandorDetail] SET [Vendor_Name] = @Vendor_Name,  [Asset_Type] = @Asset_Type,[Status]=@Status,[Date_Of_Registration]=@Date_Of_Registration,[Phone_Number]=@Phone_Number,[Email]=@Email WHERE [ID] = @ID";
                        strSql.Append(strUpdate);

                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = strUpdate.ToString();
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@Vendor_Name", strName);
                        cmd.Parameters.AddWithValue("@Asset_Type", strAsset_Type);
                        cmd.Parameters.AddWithValue("@Status", Status);
                        cmd.Parameters.AddWithValue("@Date_Of_Registration", Date_Of_Registration);
                        cmd.Parameters.AddWithValue("@Phone_Number", Phone_Number);
                        cmd.Parameters.AddWithValue("@Email", Email);
                        cmd.Parameters.AddWithValue("@ID", strID);
                        cmd.Connection = con;
                        con.Open();
                        cmd.ExecuteNonQuery();
                        strSql.Append(strUpdate);
                    }


                    catch (SqlException ex)
                    {
                        string errorMsg = "Error in Updation";
                        errorMsg += ex.Message;
                        throw new Exception(errorMsg);
                    }
                    finally
                    {
                        con.Close();
                    }

                    UncheckAll();
                }
            }
        }
    }
   
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        StringCollection idCollection = new StringCollection();
        string strID = string.Empty;

        //Loop through GridView rows to find checked rows 
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkDelete = (CheckBox)GridView1.Rows[i].
                             Cells[0].FindControl("chkSelect");
            if (chkDelete != null)
            {
                if (chkDelete.Checked)
                {
                    strID = GridView1.Rows[i].Cells[1].Text;
                    idCollection.Add(strID);
                }
            }
        }
        if (idCollection.Count > 0)
        {
            //Call the method to Delete records 
            DeleteMultipleRecords(idCollection);

            // rebind the GridView
            GridView1.DataBind();
        }
        else
        {
            Label5.Text = "Please select any row to delete";
        }

    }
    private void DeleteMultipleRecords(StringCollection idCollection)
    {
        //Create sql Connection and Sql Command
        
        SqlCommand cmd = new SqlCommand();
        string IDs = "";

        foreach (string id in idCollection)
        {
            IDs += id.ToString() + ",";
        }

        try
        {
            string test = IDs.Substring
                          (0, IDs.LastIndexOf(","));
            string sql = "Delete from VandorDetail WHERE ID = '" + test + "'";
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            string errorMsg = "Error in Deletion";
            errorMsg += ex.Message;
            throw new Exception(errorMsg);
        }
        finally
        {
            con.Close();
        }
    }
    protected void chkSelect_CheckedChanged
                            (object sender, EventArgs e)
    {
        CheckBox chkTest = (CheckBox)sender;
        GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
        TextBox txtVname = (TextBox)grdRow.FindControl
                                            ("Vendor_Name");
        TextBox txtAType = (TextBox)grdRow.FindControl
                                          ("Asset_Type");
        TextBox txtStatus = (TextBox)grdRow.FindControl
                                            ("Status");
        TextBox txtDOR = (TextBox)grdRow.FindControl
                                          ("Date_Of_Registration");
        TextBox txtPNo = (TextBox)grdRow.FindControl
                                            ("Phone_Number");
        TextBox txtEmail = (TextBox)grdRow.FindControl
                                          ("Email");
        if (chkTest.Checked)
        {
            txtVname.ReadOnly = false;
            txtAType.ReadOnly = false;
            txtStatus.ReadOnly = false;
            txtDOR.ReadOnly = false;
            txtPNo.ReadOnly = false;
            txtEmail.ReadOnly = false;
        }
        else
        {
            txtVname.ReadOnly = true;
            txtAType.ReadOnly = true;
            txtStatus.ReadOnly = true;
            txtDOR.ReadOnly = true;
            txtPNo.ReadOnly = true;
            txtEmail.ReadOnly = true;
        }
    }
    private void UncheckAll()
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkUncheck = (CheckBox)
                         row.FindControl("chkSelect");
            TextBox txtVname = (TextBox)row.FindControl
                                            ("Vendor_Name");
            TextBox txtAType = (TextBox)row.FindControl
                                              ("Asset_Type");
            TextBox txtStatus = (TextBox)row.FindControl
                                                ("Status");
            TextBox txtDOR = (TextBox)row.FindControl
                                              ("Date_Of_Registration");
            TextBox txtPNo = (TextBox)row.FindControl
                                                ("Phone_Number");
            TextBox txtEmail = (TextBox)row.FindControl
                                              ("Email");

            chkUncheck.Checked = false;
            txtVname.ReadOnly = true;
            txtAType.ReadOnly = true;
            txtStatus.ReadOnly = true;
            txtDOR.ReadOnly = true;
            txtPNo.ReadOnly = true;
            txtEmail.ReadOnly = true;
        }
    }


    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
      


        con.Open();

        string qryInsertProduct = "insert into VandorDetail(Vendor_Name,Asset_Type,Status,Date_Of_Registration,Phone_Number,Email) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox6.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
        SqlCommand comInsertProduct = new SqlCommand(qryInsertProduct, con);
        comInsertProduct.ExecuteNonQuery();
        
        GridView1.Visible = true;

        Populategridview();

        
           
           
    



    }

    protected void Populategridview()
    {

       
        string com = "Select * from VandorDetail";
        SqlCommand comm = new SqlCommand(com, con);
        comm.ExecuteNonQuery();
        con.Close();
        //GridView1.Visible = true;

    }


    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        TextBox6.Text = Calendar1.SelectedDate.ToShortDateString();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        trlogin.Visible = false;
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}
Posted
Comments
PJ003 3-Mar-14 3:09am    
You have to rebind the gridview in method btnUpdate_Click, if you want to reflect new changes to the gridview.
Member 10578683 3-Mar-14 3:38am    
Thanks

1 solution

Call the method gridview_Databind() on page_load....
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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