Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }

    protected void BindGridView()
    {
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("Select eid,ename,age from emp", con);
        con.Open();
        da.Fill(dt);
        con.Close();

        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        TextBox txt_name = new TextBox();
        TextBox txt_age = new TextBox();

        TextBox txt_id = new TextBox();
        txt_id = (TextBox)GridView1.FooterRow.FindControl("txt_eid");
        txt_name = (TextBox)GridView1.FooterRow.FindControl("txt_ename");
        txt_age = (TextBox)GridView1.FooterRow.FindControl("txt_eage");

        SqlCommand cmd = new SqlCommand("insert into emp(eid,ename,age)values('" + txt_id.Text + "','" + txt_name.Text + "','" + txt_age.Text + "')");

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        BindGridView();
    }
        
}
Posted
v2
Comments
Member 10458660 18-Dec-14 4:42am    
Error here
SqlCommand cmd = new SqlCommand("insert into emp(eid,ename,age)values('" + txt_id.Text + "','" + txt_name.Text + "','" + txt_age.Text + "')");
Jochen Arndt 18-Dec-14 5:16am    
See this blog on how to get the footer row text: http://geekswithblogs.net/AzamSharp/archive/2005/10/11/56708.aspx

1 solution

First of all, you should use Parameterized query to avoid SQL Injection attacks.

Next, it is giving NullReference Exception, because the TextBoxes are itself null. That means it failed to find the TextBoxes for you.
 
Share this answer
 
Comments
Member 10458660 18-Dec-14 7:05am    
ok.. if you have any tutorial on parameterized query, also give grid view properties each and everything explanation because i searched whole google but no one explain anythig properties clearly ..any thing you find out please share with me ///// @Tadit Dash

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