hi amit,
I think this code will be useful for u!!!!!!!!!!!!!!!
This code is used for Updating a database (MS SQL) from a grid view
This Coantains the code for editing and cancelling
Anyway try this
///////****************CODE
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class GridViewTemplateFieldEdit : System.Web.UI.Page
{
SqlConnection connection;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ConnectAndBound();
}
}
public void ConnectAndBound()
{
// connection = new SqlConnection(" server=192.168.1.2;database=ProjectManagementSyste m;user ID=sa; password=sa");
connection = new SqlConnection(ConfigurationManager.ConnectionStrin gs["TestConnectionString"].ConnectionString);
connection.Open();
cmd = new SqlCommand("dbo.select_StudentDemo", connection);
cmd.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
GV_Student.DataSource = ds;
GV_Student.DataBind();
connection.Close();
}
protected void GV_Student_RowEditing(object sender, GridViewEditEventArgs e)
{
GV_Student.EditIndex = e.NewEditIndex;
ConnectAndBound();
}
protected void GV_Student_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GV_Student_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GV_Student.EditIndex = -1;
ConnectAndBound();
Response.Write("Editing Cancelled");
}
protected void GV_Student_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// connection = new SqlConnection(" server=192.168.1.2;database=ProjectManagementSyste m;user ID=sa; password=sa");
connection = new SqlConnection(ConfigurationManager.ConnectionStrin gs["TestConnectionString"].ConnectionString);
connection.Open();
cmd = new SqlCommand("dbo.Update_StudentDemo", connection);
cmd.CommandType = CommandType.StoredProcedure;
GridViewRow row = GV_Student.Rows[e.RowIndex];
Label lbl_StudentID=(Label)(row.FindControl("lbl_Student ID")as Label);
int studid = int.Parse(lbl_StudentID.Text);
TextBox txt_StudentName=(TextBox)(row.FindControl("txt_Stu dentName")as TextBox);
TextBox txt_email = (TextBox)(row.FindControl("txt_Email") as TextBox);
TextBox txt_phone = (TextBox)(row.FindControl("txt_Phone") as TextBox);
TextBox txt_AccountNo = (TextBox)(row.FindControl("txt_AccountNumber") as TextBox);
cmd.Parameters.Add(new SqlParameter("@studentid", SqlDbType.BigInt, 8));
cmd.Parameters[0].Value = studid;
cmd.Parameters.Add(new SqlParameter("@studentname", SqlDbType.VarChar, 30));
cmd.Parameters[1].Value = txt_StudentName.Text;
cmd.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar, 30));
cmd.Parameters[2].Value = txt_email.Text;
cmd.Parameters.Add(new SqlParameter("@phone", SqlDbType.VarChar, 30));
cmd.Parameters[3].Value = txt_phone.Text;
cmd.Parameters.Add(new SqlParameter("@accountno", SqlDbType.VarChar, 20));
cmd.Parameters[4].Value = txt_AccountNo.Text;
cmd.ExecuteNonQuery();
connection.Close();
Response.Write("*********Data Updated***********");
}
protected void GV_Student_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
}
}
/**************************************NIce C#ing