i am in problems to insert data from gridview textbox. i am trying to general rule. but not success. i show my trial.
Work order no | |
Width="470px"> | |
| Work order no |
| ||||||||||||||||
database
WorkOrderNo varchar(50)primary key,
PRSL int ,
Qty int,
price money,
unit varchar(50)
code---
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('PR no can not be blank');", true);
}
else
{
string wono = ""; int prsl = 0, qty = 0; string unit = ""; decimal price = 0;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Chbox = (CheckBox)row.FindControl("chb1");
if (Chbox.Checked == true)
{
select++;
}
}
if (select == 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('select checkbox for insert)');", true);
return;
}
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Chbox = (CheckBox)row.FindControl("chb1");
if (Chbox.Checked == true)
{
for (int j = 1; j < GridView1.Rows[i].Cells.Count; j++)
{
wono = TextBox1.Text;
//accsl = int.Parse(Label2.Text);
if (j == 1) prsl = Convert.ToInt32(GridView1.Rows[i].Cells[j].Text);
//if (j == 2) qty = Convert.ToInt32(GridView1.Rows[i].Cells[j].Text);
// my problem is that i can not insert data from gridview textbox.
TextBox txt = (TextBox)row.FindControl("txtQty");
//int ll = int.Parse(txt.Text);
int k = int.Parse(txt.Text);
if (j == 2) qty =Convert.ToInt32(GridView1.Rows[i].Cells[k].Text); if (j ==3 ) price = Convert.ToDecimal(GridView1.Rows[i].Cells[j].Text);
if (j ==4) unit = GridView1.Rows[i].Cells[j].Text;
}
InsertData(wono,prsl, qty, price, unit);
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Success fully added (PR...)');", true);
}
}
TextBox1.Enabled = true;
Button2.Visible = false;
}
}
please any body helpm me best regurds. thanks

Satyapriya NayakPosted Mar 9, 2013, 12:37 PM
using System;
using System.Collections;
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.Text;
namespace Editable_GridView_multiple_records
{
public partial class WebForm1 : 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 strPRSL = ((TextBox)
GridView1.Rows[i].FindControl("txtPRSL")).Text;
string strQuantity = ((TextBox)
GridView1.Rows[i].FindControl("txtQuantity")).Text;
string strPrice = ((TextBox)
GridView1.Rows[i].FindControl("txtPrice")).Text;
string strUnit = ((TextBox)
GridView1.Rows[i].FindControl("txtUnit")).Text;
string strUpdate =
"Update product set PRSL = '" + strPRSL + "',Quantity = '" + strQuantity + "',Price = '" + strPrice + "',Unit = '" + strUnit + "' 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();
}
}
protected void chkSelect_CheckedChanged
(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
TextBox txtPRSL = (TextBox)grdRow.FindControl
("txtPRSL");
TextBox txtQuantity = (TextBox)grdRow.FindControl
("txtQuantity");
TextBox txtPrice = (TextBox)grdRow.FindControl
("txtPrice");
TextBox txtUnit = (TextBox)grdRow.FindControl
("txtUnit");
if (chkTest.Checked)
{
txtQuantity.ReadOnly = false;
txtQuantity.ForeColor = System.Drawing.Color.Red;
}
else
{
txtQuantity.ReadOnly = true;
txtQuantity.ForeColor = System.Drawing.Color.Green;
}
}
}
}