Click here to Skip to main content
12,945,927 members (37,763 online)
Rate this:
 
Please Sign up or sign in to vote.
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.


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 27-Dec-12 18:27pm
Updated 27-Dec-12 18:31pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

for this you should call Bind() Function after Record add or Update..
then it will display.
  Permalink  
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..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

try to add this.
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record sucessfully Saved.');", true);


Happy Coding. :) :D
  Permalink  
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,180
Maciej Los 2,453
Jochen Arndt 1,910
ppolymorphe 1,785


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100