I have one website development in back end work in the one client grid view option than one command field edit update and cancel.
But in the page in the update not value. so behind in the error of the page
Error
Line 62: protected void gvClientDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
Line 63: {
Line 64: int clientid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
Line 65: GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; Line 66: Label lblID = (Label)row.FindControl("lblID");
and my behind code see now.
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;
public partial class all_user_deatils : System.Web.UI.Page
{
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\HPB Industreis PvtLtd\\App_Data\\HPBData.mdf;Integrated Security=True;User Instance=True");
if (!IsPostBack)
{
gvbind();
}
}
protected void gvbind()
{
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from Planform_Client", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void gvClientDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gvbind();
}
protected void gvClientDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int clientid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");
//TextBox txtname=(TextBox)gr.cell[].control[];
TextBox firstname = (TextBox)row.Cells[0].Controls[0];
TextBox middlename = (TextBox)row.Cells[1].Controls[0];
TextBox lastname = (TextBox)row.Cells[2].Controls[0];
TextBox address = (TextBox)row.Cells[3].Controls[0];
TextBox gender = (TextBox)row.Cells[4].Controls[0];
TextBox day = (TextBox)row.Cells[5].Controls[0];
TextBox month= (TextBox)row.Cells[6].Controls[0];
TextBox year = (TextBox)row.Cells[7].Controls[0];
TextBox mobileno = (TextBox)row.Cells[8].Controls[0];
TextBox emailid = (TextBox)row.Cells[9].Controls[0];
TextBox city = (TextBox)row.Cells[10].Controls[0];
TextBox pincode = (TextBox)row.Cells[11].Controls[0];
TextBox state = (TextBox)row.Cells[12].Controls[0];
TextBox country = (TextBox)row.Cells[13].Controls[0];
TextBox images = (TextBox)row.Cells[14].Controls[0];
TextBox planname = (TextBox)row.Cells[15].Controls[0];
TextBox timeperiod = (TextBox)row.Cells[16].Controls[0];
TextBox amount = (TextBox)row.Cells[17].Controls[0];
TextBox joindate = (TextBox)row.Cells[18].Controls[0];
TextBox cycleperiod = (TextBox)row.Cells[19].Controls[0];
TextBox paydetails = (TextBox)row.Cells[20].Controls[0];
TextBox accountholder = (TextBox)row.Cells[21].Controls[0];
TextBox bankname = (TextBox)row.Cells[22].Controls[0];
TextBox accountnumber = (TextBox)row.Cells[23].Controls[0];
TextBox bankbranch = (TextBox)row.Cells[24].Controls[0];
TextBox ifcscode = (TextBox)row.Cells[25].Controls[0];
TextBox standermember = (TextBox)row.Cells[26].Controls[0];
TextBox standernumber = (TextBox)row.Cells[27].Controls[0];
TextBox uniqid = (TextBox)row.Cells[28].Controls[0];
//TextBox textadd = (TextBox)row.FindControl("txtadd");
//TextBox textc = (TextBox)row.FindControl("txtc");
GridView1.EditIndex = -1;
conn.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
SqlCommand cmd = new SqlCommand("update Planform_Client set firstname='" + firstname.Text + "',middlename='" + middlename.Text + "',lastname='" + lastname.Text + "',address='" + address.Text + "',gender='" + gender.Text + "',day='" + day.Text + "',year='" + year.Text + "', mobileno='" + mobileno.Text + "',emailid='" + emailid.Text + "',city='" + city.Text + "',pincode='" + pincode.Text + "',state='" + state.Text + "',country='" + country.Text + "',images='" + images.Text + "',planname='" + planname.Text + "',timeperiode='" + timeperiod.Text + "',amount='" + amount.Text + "',joindate='" + joindate.Text + "',cycleperiod='" + cycleperiod.Text + "', paydetails='" + paydetails.Text + "',accountholder='" + accountholder.Text + "',bankname='" + bankname.Text + "',accountnumber='" + accountnumber.Text + "',bankbranch='" + bankbranch.Text + "',ifcscode='" + ifcscode.Text + "',standermember='" + standermember.Text + "', standernumber='" + standernumber.Text + "',uniqid='" + uniqid.Text + "' where clientid='" + clientid + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
//GridView1.DataBind();
}
protected void gvClientDetails_RowCanclingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
gvbind();
}
protected void gvClientDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
gvbind();
}
}
So Plz My help me and let me know.
Regards,
Hitesh Dabhi
Hitesh DabhiPosted Dec 20, 2013, 9:27 AM
Thank You So Much this coding is worked successfully in my Project.
Thank You So Much Bro..:D
Regards,
Hitesh Dabhi
Satyapriya NayakPosted Dec 14, 2013, 4:13 AM
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;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter ad = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataTable dataTable;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
Session["sortBy"] = null;
if (!IsPostBack)
{
FillVendorGrid();
}
}
private void FillVendorGrid()
{
SqlConnection conn = new SqlConnection(connStr);
dataTable = new DataTable();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM Vendor";
ad = new SqlDataAdapter(cmd);
ad.Fill(dataTable);
ResultGridView.DataSource = dataTable;
ResultGridView.DataBind();
}
protected void ResultGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
ResultGridView.EditIndex = e.NewEditIndex;
FillVendorGrid();
}
protected void ResultGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ResultGridView.PageIndex = e.NewPageIndex;
FillVendorGrid();
}
protected void ResultGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection(connStr);
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM Vendor WHERE VendorId='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
FillVendorGrid();
}
protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtFName = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtFName");
TextBox txtLName = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtLName");
TextBox txtCity = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtCity");
TextBox txtState = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtState");
TextBox txtCountry = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtCountry");
TextBox txtDescription = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtDescription");
SqlConnection conn = new SqlConnection(connStr);
cmd.Connection = conn;
cmd.CommandText = "UPDATE Vendor SET VendorFName ='" + txtFName.Text + "',VendorLName ='" + txtLName.Text + "',VendorCity ='" + txtCity.Text + "',VendorState ='" + txtState.Text + "',VendorCountry ='" + txtCountry.Text + "',VendorDescription ='" + txtDescription.Text + "' WHERE VendorId='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
conn.Open();
cmd.ExecuteNonQuery();
ResultGridView.EditIndex = -1;
FillVendorGrid();
conn.Close();
}
protected void ResultGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
ResultGridView.EditIndex = -1;
FillVendorGrid();
}
protected void ResultGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtFName = (TextBox)ResultGridView.FooterRow.FindControl("txtFName1");
TextBox txtLName = (TextBox)ResultGridView.FooterRow.FindControl("txtLName1");
TextBox txtCity = (TextBox)ResultGridView.FooterRow.FindControl("txtCity1");
TextBox txtState = (TextBox)ResultGridView.FooterRow.FindControl("txtState1");
TextBox txtCountry = (TextBox)ResultGridView.FooterRow.FindControl("txtCountry1");
TextBox txtDescription = (TextBox)ResultGridView.FooterRow.FindControl("txtDescription1");
SqlConnection conn = new SqlConnection(connStr);
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Vendor(VendorFName, VendorLName,VendorCity,VendorState,VendorCountry,VendorDescription) Values('" + txtFName.Text + "', '" + txtLName.Text + "', '" + txtCity.Text + "', '" + txtState.Text + "', '" + txtCountry.Text + "' , '" + txtDescription.Text + "')";
conn.Open();
cmd.ExecuteNonQuery();
FillVendorGrid();
conn.Close();
}
}
protected void btn_search_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
str = "select * from Vendor where VendorFName like '" + TextBox1.Text + "%'";
cmd = new SqlCommand(str, conn);
sqlda = new SqlDataAdapter(cmd);
ds = new DataSet();
sqlda.Fill(ds, "Vendor");
conn.Close();
ResultGridView.DataSource = ds;
ResultGridView.DataMember = "Vendor";
ResultGridView.DataBind();
}
protected void ResultGridView_Sorting(object sender, GridViewSortEventArgs e)
{
Session["sortBy"] = e.SortExpression;
FillVendorGrid();
}
}
Sanjay KumarPosted Dec 14, 2013, 4:09 AM
http://www.c-sharpcorner.com/UploadFile/9f0ae2/gridview-edit-delete-and-update-in-Asp-Net/
http://www.c-sharpcorner.com/uploadfile/anjudidi/select-update-and-delete-data-in-a-Asp-Net-gridview-control/