TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sagar Adhe
NA
6
602
Search result gridview is editing wrong row
Jan 7 2016 5:40 AM
public partial class _Default : System.Web.UI.Page
{
static DataTable dt;
private string SearchString = "";
SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=bobrecharge;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
DataSet ds = new DataSet();
DataTable FromTable = new DataTable();
conn.Open();
string cmdstr = "Select * from Signup";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
cmd.ExecuteNonQuery();
FromTable = ds.Tables[0];
if (FromTable.Rows.Count > 0)
{
gvEmployeeDetails.DataSource = FromTable;
gvEmployeeDetails.DataBind();
}
else
{
FromTable.Rows.Add(FromTable.NewRow());
gvEmployeeDetails.DataSource = FromTable;
gvEmployeeDetails.DataBind();
int TotalColumns = gvEmployeeDetails.Rows[0].Cells.Count;
gvEmployeeDetails.Rows[0].Cells.Clear();
gvEmployeeDetails.Rows[0].Cells.Add(new TableCell());
gvEmployeeDetails.Rows[0].Cells[0].ColumnSpan = TotalColumns;
gvEmployeeDetails.Rows[0].Cells[0].Text = "No records Found";
}
ds.Dispose();
conn.Close();
}
protected void BindData2()
{
DataSet ds = new DataSet();
DataTable FromTable = new DataTable();
conn.Open();
string cmdstr = "Select * from Signup";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
cmd.ExecuteNonQuery();
FromTable = ds.Tables[0];
if (FromTable.Rows.Count > 0)
{
gvEmployeeDetails2.DataSource = FromTable;
gvEmployeeDetails2.DataBind();
}
else
{
FromTable.Rows.Add(FromTable.NewRow());
gvEmployeeDetails2.DataSource = FromTable;
gvEmployeeDetails2.DataBind();
int TotalColumns = gvEmployeeDetails2.Rows[0].Cells.Count;
gvEmployeeDetails2.Rows[0].Cells.Clear();
gvEmployeeDetails2.Rows[0].Cells.Add(new TableCell());
gvEmployeeDetails2.Rows[0].Cells[0].ColumnSpan = TotalColumns;
gvEmployeeDetails2.Rows[0].Cells[0].Text = "No records Found";
}
ds.Dispose();
conn.Close();
}
protected void gvEmployeeDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblEmpID = (Label)gvEmployeeDetails.Rows[e.RowIndex].FindControl("lblUserID");
conn.Open();
string cmdstr = "delete from Signup where UserID=@UserID";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.Parameters.AddWithValue("@UserID", lblEmpID.Text);
cmd.ExecuteNonQuery();
conn.Close();
BindData();
}
protected void gvEmployeeDetails2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblEmpID = (Label)gvEmployeeDetails2.Rows[e.RowIndex].FindControl("lblUserID");
conn.Open();
string cmdstr = "delete from Signup where UserID=@UserID";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.Parameters.AddWithValue("@UserID", lblEmpID.Text);
cmd.ExecuteNonQuery();
conn.Close();
BindData2();
}
protected void gvEmployeeDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("ADD"))
{
//TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddUserID");
TextBox txtAddName = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddUserName");
TextBox txtAddDesignation = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmailID");
TextBox txtAddCity = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddContact");
TextBox txtAddCountry = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddPassword");
TextBox txtAddCountry1 = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddCPassword");
conn.Open();
string cmdstr = "insert into Signup(UserName,EmailID,Contact,Password,CPassword) values(@name,@designation,@city,@country,@country1)";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
// cmd.Parameters.AddWithValue("@empid", txtAddEmpID.Text);
cmd.Parameters.AddWithValue("@name", txtAddName.Text);
cmd.Parameters.AddWithValue("@designation", txtAddDesignation.Text);
cmd.Parameters.AddWithValue("@city", txtAddCity.Text);
cmd.Parameters.AddWithValue("@country", txtAddCountry.Text);
cmd.Parameters.AddWithValue("@country1", txtAddCountry1.Text);
cmd.ExecuteNonQuery();
conn.Close();
BindData();
lblResult.Text = "Record Inserted";
}
}
protected void gvEmployeeDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblEditEmpID = (Label)gvEmployeeDetails.Rows[e.RowIndex].FindControl("lblEditUserID");
TextBox txtEditName = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditUserName");
TextBox txtEditDesignation = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditEmailID");
TextBox txtEditCity = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditContact");
TextBox txtEditCountry = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditPassword");
TextBox txtEditCountry1 = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditCPassword");
conn.Open();
string cmdstr = "update Signup set UserName=@name,EmailID=@designation,Contact=@city,Password=@country,CPassword=@country1 where UserID=@empid";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.Parameters.AddWithValue("@empid", lblEditEmpID.Text);
cmd.Parameters.AddWithValue("@name", txtEditName.Text);
cmd.Parameters.AddWithValue("@designation", txtEditDesignation.Text);
cmd.Parameters.AddWithValue("@city", txtEditCity.Text);
cmd.Parameters.AddWithValue("@country", txtEditCountry.Text);
cmd.Parameters.AddWithValue("@country1", txtEditCountry1.Text);
cmd.ExecuteNonQuery();
conn.Close();
gvEmployeeDetails.EditIndex = -1;
BindData();
lblResult.Text = "Record Updated";
}
protected void gvEmployeeDetails2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
gvEmployeeDetails.Visible = false;
gvEmployeeDetails2.Visible = true;
Label lblEditEmpID = (Label)gvEmployeeDetails2.Rows[e.RowIndex].FindControl("lblEditUserID");
TextBox txtEditName = (TextBox)gvEmployeeDetails2.Rows[e.RowIndex].FindControl("txtEditUserName");
TextBox txtEditDesignation = (TextBox)gvEmployeeDetails2.Rows[e.RowIndex].FindControl("txtEditEmailID");
TextBox txtEditCity = (TextBox)gvEmployeeDetails2.Rows[e.RowIndex].FindControl("txtEditContact");
TextBox txtEditCountry = (TextBox)gvEmployeeDetails2.Rows[e.RowIndex].FindControl("txtEditPassword");
TextBox txtEditCountry1 = (TextBox)gvEmployeeDetails2.Rows[e.RowIndex].FindControl("txtEditCPassword");
conn.Open();
string cmdstr = "update Signup set UserName=@name,EmailID=@designation,Contact=@city,Password=@country,CPassword=@country1 where UserID=@empid";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.Parameters.AddWithValue("@empid", lblEditEmpID.Text);
cmd.Parameters.AddWithValue("@name", txtEditName.Text);
cmd.Parameters.AddWithValue("@designation", txtEditDesignation.Text);
cmd.Parameters.AddWithValue("@city", txtEditCity.Text);
cmd.Parameters.AddWithValue("@country", txtEditCountry.Text);
cmd.Parameters.AddWithValue("@country1", txtEditCountry1.Text);
cmd.ExecuteNonQuery();
conn.Close();
gvEmployeeDetails2.EditIndex = -1;
BindData2();
lblResult.Text = "Record Updated";
}
protected void gvEmployeeDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvEmployeeDetails.EditIndex = -1;
BindData();
}
protected void gvEmployeeDetails2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvEmployeeDetails.Visible = false;
gvEmployeeDetails2.Visible = true;
gvEmployeeDetails2.EditIndex = -1;
BindData2();
}
protected void gvEmployeeDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvEmployeeDetails.EditIndex = e.NewEditIndex;
BindData();
}
protected void gvEmployeeDetails2_RowEditing(object sender, GridViewEditEventArgs e)
{
gvEmployeeDetails.Visible = false;
gvEmployeeDetails2.Visible = true;
GridViewRow row = gvEmployeeDetails2.Rows[e.NewEditIndex];
if (row.RowState != (DataControlRowState.Edit | DataControlRowState.Selected))
{
gvEmployeeDetails2.SelectedIndex = e.NewEditIndex;
}
//gvEmployeeDetails2.EditIndex = e.NewEditIndex;
BindData2();
}
protected void gvEmployeeDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvEmployeeDetails.PageIndex = e.NewPageIndex;
gvEmployeeDetails.DataSource = dt;
gvEmployeeDetails.DataBind();
BindData();
}
protected void gvEmployeeDetails2_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvEmployeeDetails.Visible = false;
gvEmployeeDetails2.Visible = true;
gvEmployeeDetails2.PageIndex = e.NewPageIndex;
gvEmployeeDetails2.DataSource = dt;
gvEmployeeDetails2.DataBind();
BindData2();
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtSearch.Text = "";
SearchString = "";
lblResult.Text = "";
gvEmployeeDetails.DataSource = dt;
gvEmployeeDetails.DataBind();
BindData();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = GetDataFromDataBase(txtSearch.Text);
if (dt.Rows.Count > 0)
{
gvEmployeeDetails.Visible = false;
gvEmployeeDetails2.Visible = true;
gvEmployeeDetails2.DataSource = dt;
gvEmployeeDetails2.DataBind();
lblResult.Text = "Match Result ..'" + txtSearch.Text.Trim() + "'... ";
}
else
{
lblResult.Text = "Record not Found for..'" + txtSearch.Text.Trim() + "'... ";
gvEmployeeDetails2.DataSource = null;
gvEmployeeDetails2.DataBind();
}
}
public DataTable GetDataFromDataBase(string searchtext)
{
DataTable dt = new DataTable();
string querystring = "select * from Signup where UserName like '%" + txtSearch.Text + "%'; ";
SqlDataAdapter da = new SqlDataAdapter(querystring, conn);
conn.Open();
da.Fill(dt);
return dt;
}
protected void gvEmployeeDetails2_SelectedIndexChanged(object sender, EventArgs e){
GridViewRow row = gvEmployeeDetails2.SelectedRow;
}
}
I'm searching for sag its show me below records==
UserID UserName EmailID Contact Password CPassword Action
30 sagar 888 9096889834 9096 9096 Edit Delete
19 sag43 asd 345 567 567 Edit Delete
20 sag1 sag@gmail 34544 234 234 Edit Delete
22 sag saga2 2434 342 32424 Edit Delete
24 sag3 sasa 8983651289 33333 33333 Edit Delete
Reply
Answers (
1
)
E-commers project
sorry I cant download the codes