Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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
Edited 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 at 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 at 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. Smile | :) Big Grin | :-D
  Permalink  
Comments
Sachin Shinde11088 at 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 at 28-Dec-12 6:28am
   
no its working anywhere.. go ahead
...
Sachin Shinde11088 at 28-Dec-12 6:31am
   
Thanks bro..
thanks a lot..
prashant patil 4987 at 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
0 Sergey Alexandrovich Kryukov 861
1 OriginalGriff 410
2 CPallini 255
3 Volynsky Alex 205
4 Richard Deeming 175
0 OriginalGriff 5,540
1 Sergey Alexandrovich Kryukov 4,668
2 CPallini 4,540
3 George Jonsson 3,102
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
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