Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C#
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();

  }
Posted
Updated 12-Dec-13 19:23pm
v2
Comments
Vishal Pand3y 13-Dec-13 1:33am    
after inserting in DB just bing your grid it's simple
Karthik_Mahalingam 13-Dec-13 1:35am    
ya.. go for it.
raj ch 13-Dec-13 1:35am    
use update panel to refresh ur gridview

1 solution

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
 
Share this answer
 
Comments
Venkat Kumar chigulla 13-Dec-13 1:43am    
It's working but here length of the code is increasing in other words for inserting we open the connection and for Displaying we open the connection. Can we reduce the code..?
Tom Marvolo Riddle 13-Dec-13 1:53am    
@venkat No.You cannot access(insert,select,update...)the database after closing the connection.It is must to open the connection before accessing db.Do one thing declare the connection globally and use it
Animesh Datta 13-Dec-13 1:52am    
Hello ,
After inserting you have to select the data and these data should be bind in gridview . there is no another shortcut .
Venkat Kumar chigulla 13-Dec-13 1:54am    
yeah got it. Thank you.

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