Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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 _22 : System.Web.UI.Page
{
    SqlConnection conn = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Response.Redirect("MainLogin.aspx");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Button2.Text == "edit")
        {
            UpdateData();
        }
    }

    private void UpdateData()
    {
        // SqlConnection conn = null;
        SqlCommand cmd = null;
        try
        {
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PConnStr"].ConnectionString);
            string sql = "Update ABC set name=@name,age=@age where name=@a";
            conn.Open();
            cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@name", TextBox1.Text);

            cmd.Parameters.AddWithValue("@age", TextBox2.Text);
            cmd.Parameters.AddWithValue("@a", Session["name"]);

            cmd.ExecuteNonQuery();


            Label3.Visible = true;
            Label3.Text = "Updated Successfully";
        }

        catch (Exception ex)
        {

            Label4.Text = ex.ToString();
            Label4.Visible = true;
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataAdapter da = null;
        DataSet dsUser = null;
        SqlDataReader dr;
        try
        {
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PConnStr"].ConnectionString);
            conn.Open();
            string sql = "Select * from ABC,Login  where name=@name and Password=@pswd";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@name", Session["user"]);
            cmd.Parameters.AddWithValue("@pswd", Session["pswd"]);
            //cmd.Parameters.AddWithValue("@lname",T2.Text);
            // cmd.Parameters.AddWithValue("@p_id", cmbP_id.Text);
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {


                TextBox1.Text = dr[2].ToString();
                TextBox2.Text = dr[3].ToString();
                TextBox3.Text = dr[4].ToString();

            }
        }
        catch (Exception ex)
        {
            Label3.Text = ex.ToString();
            Label3.Visible = true;
        }
    }
}
Posted
Updated 20-Apr-12 3:09am
v2

1 solution

Edit/Update/Delete feature work on unique key. Even if your system is designed to have multiple usernames, you need to keep track of userID (assuming this is the unique key - no duplication). Do your logic implementation of edit based on this userID and not the username.

When you authenticated the user, looks like you are keeping track of username. Keep a track of the userID too(which is a unique id). This will help you.
 
Share this answer
 
Comments
Uday P.Singh 20-Apr-12 9:24am    
Agree 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900