I add this code but textbox value show in empty in database ..please give me solution.
On Button Click the GridView TextBox loose all data.
public partial class keyexam2 : System.Web.UI.Page
{
//SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\shubhangi\jayonline\paper\App_Data\QUIZ.MDF;Integrated Security=True;User Instance=True");
DataTable dt = new DataTable();
//DataRow row = dt.NewRow();
SqlCommand cmd;
SqlDataAdapter sda;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
//String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["Data Source=.\SQLEXPRESS;AttachDbFilename=D:\shubhangi\jayonline\paper\App_Data\QUIZ.MDF;Integrated Security=True;User Instance=True"].ConnectionString;
string strQuery = "select * from OneQuestion order by QuestionID";
cmd = new SqlCommand(strQuery, con);
//SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
//GridView1.DataSource = dt;
//GridView1.DataBind();
//Gridview1.DataBound = dt;
//Gridview1.DataBound();
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
sda.Dispose();
//con.Dispose();
}
Gridview1.DataBind();
}
protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
foreach (GridViewRow gr in Gridview1.Rows)
{
string data1 = gr.Cells[0].Text;
string data2 = gr.Cells[1].Text;
TextBox box1 = (TextBox)gr.Cells[2].FindControl("textbox1");
cmd = new SqlCommand("insert into Answertbl(QuestionID, Title,Answer1) values ('" + data1.ToString() + "', '" + data2.ToString() + "', '" + box1.Text + "')", con);
cmd.ExecuteNonQuery();
}
con.Close();
lblshow.Text = "success";
}
}