如何在Gridview中显示数据 [英] How to Display Data in Gridview

查看:69
本文介绍了如何在Gridview中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将值插入到数据库中,但它不会在网格视图中显示插入的值。我的要求是每当插入的值应该显示在Gridview下面是我的代码





I'm inserted values in to database, but it won't the display the inserted values in Grid View . My requirement is whenever the values inserted it should be display on Gridview Below is my Code


protected void btnsave_Click(object sender, EventArgs e)
  {
    int student_id= get_previous_studentID(); //Get student ID
    int counselor_id = get_counselor_id();    // Get Employee ID

    string s = ConfigurationManager.ConnectionStrings["conn"].ToString();
    SqlConnection con = new SqlConnection(s);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "insert into student_info values('venkat','9490069073','hyd');
    cmd.CommandType = CommandType.Text;
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    DataSet ds = new DataSet();
    da.Fill(ds);
    gvstd.DataSource = ds;
    gvstd.DataBind();

  }

推荐答案

你好,



你必须创建一个选择命令来获取数据从数据库中然后显示gridview中的数据。你只是插入到数据库中而没有从中获取数据。所以,你怎么能期望得到这些值。





试试这段代码

Hello ,

you have to create a "Select" command to fetch the data from database and then show that data in gridview .you have only inserted into database and have not fetched from it . so , how could you expect to get those values .


try this code
Void Bind()
{

      string s = ConfigurationManager.ConnectionStrings["conn"].ToString();
      SqlConnection con = new SqlConnection(s);
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = con
      cmd.CommandText = "select * from student_info ";
      cmd.CommandType = CommandType.Text;
      SqlDataAdapter da = new SqlDataAdapter();
      da.SelectCommand = cmd;
      DataSet ds = new DataSet();
      da.Fill(ds);
      gvstd.DataSource = ds;
      gvstd.DataBind();
}





并在插入方法后调用此函数。



谢谢



and call this function after inserting method .

thanks


这篇关于如何在Gridview中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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