please help me in inserting a textbox in a gridview in asp using C#
when i enter data in the textbox that particular data has to be stored in the database based on the columns below
| name | address | marks |
| chiranjeevi | mdp | textbox1 |
| venu | mdpo | textbox2 |
| naveen | vzm | textbox3 |
| ravi | zzm | textbox4 |
| raju | hyd | textbox5 |
| name | address | marks |
| chiranjeevi | mdp | 20 |
| venu | mdpo | 19 |
| naveen | vzm | 18 |
| ravi | zzm | 17 |
| raju | hyd | 16 |
so the marks entered in textbox1 one has to be stored in corresponding fields in database

Satyapriya NayakPosted Nov 30, 2011, 6:59 AM
Run the attachments.
One alternate solution for your problem.You have to select all the checkbox and enter the values in the gridview and when you click insert button all the records are inserted into the database.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void btnUpdate_Click(object sender, EventArgs e)
{
StringBuilder strSql = new StringBuilder(string.Empty);
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkUpdate = (CheckBox)
GridView1.Rows[i].Cells[0].FindControl("chkSelect");
if (chkUpdate != null)
{
if (chkUpdate.Checked)
{
string strID = GridView1.Rows[i].Cells[1].Text;
string strName = ((TextBox)
GridView1.Rows[i].FindControl("txtname")).Text;
string straddress = ((TextBox)
GridView1.Rows[i].FindControl("txtaddress")).Text;
string strmarks = ((TextBox)
GridView1.Rows[i].FindControl("txtmarks")).Text;
string strUpdate =
"Update Details set name = '" + strName + "',address = '" + straddress + "',marks = '" + strmarks + "' WHERE ID ='" + strID + "'";
strSql.Append(strUpdate);
}
}
}
try
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql.ToString();
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string errorMsg = "Error in Updation";
errorMsg += ex.Message;
throw new Exception(errorMsg);
}
finally
{
con.Close();
}
UncheckAll();
}
private void UncheckAll()
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkUncheck = (CheckBox)
row.FindControl("chkSelect");
TextBox txtname = (TextBox)
row.FindControl("txtname");
TextBox txtaddress = (TextBox)
row.FindControl("txtaddress");
TextBox txtmarks = (TextBox)
row.FindControl("txtmarks");
chkUncheck.Checked = false;
txtname.ReadOnly = true;
txtaddress.ReadOnly = true;
txtname.ForeColor = System.Drawing.Color.Blue;
txtaddress.ForeColor = System.Drawing.Color.Blue;
txtmarks.ForeColor = System.Drawing.Color.Blue;
}
}
protected void chkSelect_CheckedChanged
(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
TextBox txtname = (TextBox)grdRow.FindControl
("txtname");
TextBox txtladdress = (TextBox)grdRow.FindControl
("txtaddress");
TextBox txtlmarks = (TextBox)grdRow.FindControl
("txtmarks");
if (chkTest.Checked)
{
txtname.ReadOnly = false;
txtladdress.ReadOnly = false;
txtlmarks.ReadOnly = false;
txtname.ForeColor = System.Drawing.Color.Black;
txtladdress.ForeColor = System.Drawing.Color.Black;
txtlmarks.ForeColor = System.Drawing.Color.Black;
}
else
{
txtname.ReadOnly = true;
txtladdress.ReadOnly = true;
txtlmarks.ReadOnly = true;
txtname.ForeColor = System.Drawing.Color.Blue;
txtladdress.ForeColor = System.Drawing.Color.Blue;
txtlmarks.ForeColor = System.Drawing.Color.Blue;
}
}
}
Thanks
If this post helps you mark it as answer
Datta KharadPosted Nov 30, 2011, 6:14 AM
You can try this code..
DataTable dtData;
private void Form1_Load(object sender, EventArgs e)
{
dtData = new DataTable();
dtData.Columns.Add("Name");
dtData.Columns.Add("Address");
dtData.Columns.Add("Mark");
}
private void button9_Click(object sender, EventArgs e)
{
if (dtData.Rows.Count > 0)
{
dtData.Rows.Clear();
dataGridView2.DataSource = null;
}
DataRow dr = dtData.NewRow();
dr[0] = "Datta";
dr[1] = "Mumbai";
dr[2] = textBox4.Text;
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "DK";
dr[1] = "Pune";
dr[2] = textBox5.Text;
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "Kharad";
dr[1] = "Hyderbad";
dr[2] = textBox6.Text;
dtData.Rows.Add(dr);
dataGridView2.DataSource = dtData;
}
Mayur GujrathiPosted Nov 30, 2011, 5:35 AM