Update database column using C# in ASP.Net

indradeo
 
on Oct 06, 2021 07:06 AM
435 Views

how to make edit update function using asp.net c#

i want to 'lst_updated_by' column in table how to do this.

if user updated table value then emp id updated in table.

namespace TS
{
    public partial class Edit1 : System.Web.UI.Page
    {
        int empno = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            empno = Convert.ToInt32(Request.QueryString["Id"].ToString());
            if (!IsPostBack)
            {
                BindTextBoxvalues();
            } 
        }

        private void BindTextBoxvalues()
        {
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
           // SqlCommand cmd = new SqlCommand("select * from ts4",  con);
            SqlCommand cmd = new SqlCommand("select * from ts4 where Id=" + empno, con);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            txtEmpId.Text = dt.Rows[0][0].ToString();
            //txt1.Text = dt.Rows[0][1].ToString();
            Label1.Text = dt.Rows[0][1].ToString();
            Label2.Text = dt.Rows[0][2].ToString();
            Label3.Text = dt.Rows[0][3].ToString();
            Label4.Text = dt.Rows[0][4].ToString();
            Label5.Text = dt.Rows[0][5].ToString();
            Label6.Text = dt.Rows[0][6].ToString();
            txt8.Text = dt.Rows[0][7].ToString();
            txt9.Text = dt.Rows[0][8].ToString();
            txt10.Text = dt.Rows[0][9].ToString();
            txt11.Text = dt.Rows[0][10].ToString();
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            string conn = "";
            conn = ConfigurationManager.ConnectionStrings["constr"].ToString();
            SqlConnection objsqlconn = new SqlConnection(conn);
            objsqlconn.Open();
            SqlCommand cmd = new SqlCommand("Update ts4 set current_status='" + txt8.Text + "' ,actn_rqrd='" + txt9.Text + "',rspncblty='" + txt10.Text + "',rmk='" + txt11.Text + "'where Id='" + txtEmpId.Text + "'", objsqlconn);
            cmd.ExecuteNonQuery(); 
            {
                Response.Redirect("Index.aspx");
            }
        } 
 
        protected void Button2_Click(object sender, EventArgs e)
        { 
            Response.Redirect("Index.aspx"); 
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 09, 2021 06:40 AM

Use below code.

public partial class Edit1 : System.Web.UI.Page
{
    int empno = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        empno = Convert.ToInt32(Request.QueryString["Id"].ToString());
        Session["EmpId"] = empno;
        if (!IsPostBack)
        {
            BindTextBoxvalues();
        }

    }

    private void BindTextBoxvalues()
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        // SqlCommand cmd = new SqlCommand("select * from ts4",  con);
        SqlCommand cmd = new SqlCommand("select * from ts4 where Id=" + empno, con);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        txtEmpId.Text = dt.Rows[0][0].ToString();
        //txt1.Text = dt.Rows[0][1].ToString();
        Label1.Text = dt.Rows[0][1].ToString();
        Label2.Text = dt.Rows[0][2].ToString();
        Label3.Text = dt.Rows[0][3].ToString();
        Label4.Text = dt.Rows[0][4].ToString();
        Label5.Text = dt.Rows[0][5].ToString();
        Label6.Text = dt.Rows[0][6].ToString();
        txt8.Text = dt.Rows[0][7].ToString();
        txt9.Text = dt.Rows[0][8].ToString();
        txt10.Text = dt.Rows[0][9].ToString();
        txt11.Text = dt.Rows[0][10].ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string conn = "";
        conn = ConfigurationManager.ConnectionStrings["constr"].ToString();
        SqlConnection objsqlconn = new SqlConnection(conn);
        objsqlconn.Open();
        SqlCommand cmd = new SqlCommand("Update ts4 set current_status='" + txt8.Text + "' ,actn_rqrd='" + txt9.Text + "',rspncblty='" + txt10.Text + "',lst_updated_by='" + Session["EmpId"].ToString() + "',rmk='" + txt11.Text + "' where Id='" + txtEmpId.Text + "'", objsqlconn);
        cmd.ExecuteNonQuery();

        {
            Response.Redirect("Index.aspx");
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Index.aspx");
    }
}