带存储过程的GridView数据绑定 [英] GridView DataBinding With Stored Procedure

查看:88
本文介绍了带存储过程的GridView数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新或添加记录后GridView没有出现在屏幕上。我应该编写什么代码。这就是我现在所写的内容。





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();
         }
    }

}

推荐答案

为此你应该打电话给Bind( )记录添加或更新后的功能..

然后它会显示。
for this you should call Bind() Function after Record add or Update..
then it will display.


尝试添加它。

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





快乐编码。 :):D



Happy Coding. :) :D


这篇关于带存储过程的GridView数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆