Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
after Updating or Adding a record GridView does not appear on the screen.what should code i write.this is what i have written right now.


C#
public partial class StudentMaster : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter da;
    SqlCommand cmd;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
            bind();
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        CallProcedure("I");
    }

    public void bind()
    {
        try
        {
            con = new SqlConnection("");
            con.Open();
            da = new SqlDataAdapter("select * from student_master", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Students Master");

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

    public void CallProcedure(string f)
    {
        con = new SqlConnection("")

        try
        {
            con.Open();


            cmd = new SqlCommand("IU_StudDetails", con);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@flag", SqlDbType.VarChar).Value = @f;
            cmd.Parameters.AddWithValue("@stdid", SqlDbType.Int).Value = lblid.Text;
            cmd.Parameters.AddWithValue("@sname", SqlDbType.VarChar).Value = txtname.Text;
            cmd.Parameters.AddWithValue("@marks", SqlDbType.Int).Value = txtmarks.Text;
            cmd.Parameters.AddWithValue("@address", SqlDbType.VarChar).Value = txtaddress.Text;
            

            GridviewBind();

            da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();    

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblid.Text = GridView1.SelectedRow.Cells[1].Text;
        txtname.Text = GridView1.SelectedRow.Cells[2].Text;
        txtmarks.Text = GridView1.SelectedRow.Cells[3].Text;
        txtaddress.Text = GridView1.SelectedRow.Cells[4].Text;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CallProcedure("U");
        CallProcedure("S");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        CallProcedure("D");
    }

    public void GridviewBind()
    {
         con = new SqlConnection("")

         try
         {
             con.Open();
             cmd = new SqlCommand();
             da = new SqlDataAdapter("IU_StudDetails", con);
             da.SelectCommand.CommandType = CommandType.StoredProcedure;


             da.SelectCommand.Parameters.Add(new SqlParameter("@stdid", SqlDbType.Int));
             da.SelectCommand.Parameters["@stdid"].Value = Convert.ToInt32(lblid.Text);

             da.SelectCommand.Parameters.Add(new SqlParameter("@sname", SqlDbType.VarChar));
             da.SelectCommand.Parameters["@sname"].Value = txtname.Text;

             da.SelectCommand.Parameters.Add(new SqlParameter("@marks", SqlDbType.VarChar));
             da.SelectCommand.Parameters["@marks"].Value = Convert.ToInt32(txtmarks.Text);

             da.SelectCommand.Parameters.Add(new SqlParameter("@address", SqlDbType.VarChar));
             da.SelectCommand.Parameters["@address"].Value = txtaddress.Text;


             DataSet ds = new DataSet();
             da.Fill(ds,"");
             GridView1.DataSource = ds.Tables[0];
             GridView1.DataBind();
             da.Dispose();   
}
         catch (Exception ex)
         {
             Response.Write(ex.Message);
         }
         finally
         {
             con.Close();
         }
    }

}
Posted
Updated 27-Dec-12 18:31pm
v2

for this you should call Bind() Function after Record add or Update..
then it will display.
 
Share this answer
 
Comments
Sachin Shinde11088 28-Dec-12 6:15am    
one more question.
now its working properly.
now i want to display a message on a button click event.
like if i adds new records then on its click event message should be displayed.
like for delete ,update.
for that what should i write.
help me
prashant patil 4987 28-Dec-12 6:20am    
check my solution 2..
try to add this.
C#
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record sucessfully Saved.');", true);


Happy Coding. :) :D
 
Share this answer
 
Comments
Sachin Shinde11088 28-Dec-12 6:27am    
yesss bro its working.
bt will it work after deploying web site on server????
or this will run only on Visual studio..
prashant patil 4987 28-Dec-12 6:28am    
no its working anywhere.. go ahead
...
Sachin Shinde11088 28-Dec-12 6:31am    
Thanks bro..
thanks a lot..
prashant patil 4987 28-Dec-12 6:33am    
hey lways welcome .dude...
all the best.
:)

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