Jaya Prakash

Jaya Prakash

  • 548
  • 2.3k
  • 63.4k

Editing Table

Feb 17 2023 7:38 AM

This is My db 

create table mytable(id int identity(1,1),fname varchar(55),lname varchar(55),mobno bigint,Email varchar(100),salary money)
i have some data

This is my SP

create procedure sp_UpdateTable
(
@id int,@fname varchar(55),@lname varchar(55),@mobno bigint,@Email varchar(100),@salary money
)
as begin
update mytable set fname=@fname,lname=@lname,mobno=@mobno,salary=@salary where id=@id
end

i can able to do add and delete

but modification is not working properly 

i am doing it in 3 tier 

this is my code

dal layer

public int UpdateToTable(BusinessObjects.ObjectClass objectClass)
{
    SqlCommand cmd = new SqlCommand("sp_UpdateTable", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@id", objectClass.Id);
    cmd.Parameters.AddWithValue("@fname", objectClass.Fname);
    cmd.Parameters.AddWithValue("@lname", objectClass.Lname);
    cmd.Parameters.AddWithValue("@mobno", objectClass.MobNo);
    cmd.Parameters.AddWithValue("@Email", objectClass.Email);
    cmd.Parameters.AddWithValue("@salary", objectClass.Salary);
    con.Open();
    int i = cmd.ExecuteNonQuery();
    return i;
}

logic layer

public int UpdateToTable(BusinessObjects.ObjectClass objectClass)
{
    int i = dal.UpdateToTable(objectClass);
    return i;
}

My row command

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow row = GridView1.Rows[index];
    Label l1 = (Label)row.FindControl("Label1");
    boc.Id = Convert.ToInt32(l1.Text);
    if(e.CommandName== "cmddelete")
    {
        int i = blc.DeleteTable(boc);
        if (i == 1)
        {
            DataSet ds = blc.GetData();
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    else if (e.CommandName == "cmdedit")
    {
        Label l7 = (Label)row.FindControl("Label1");
        boc.Id = Convert.ToInt32(l7.Text);
        Label l2 = (Label)row.FindControl("Label2");
        boc.Fname = l2.Text;
        Label l3 = (Label)row.FindControl("Label3");
        boc.Lname = l3.Text;
        Label l4 = (Label)row.FindControl("Label4");
        boc.MobNo =Convert.ToInt64(l4.Text);
        Label l5 = (Label)row.FindControl("Label5");
        boc.Email = l5.Text;
        Label l6 = (Label)row.FindControl("Label6");
        boc.Salary =Convert.ToDecimal(l6.Text);
        TextBox8.Text = l7.Text;
        TextBox1.Text = l2.Text;
        TextBox2.Text = l3.Text;
        TextBox3.Text = l4.Text;
        TextBox4.Text = l5.Text;
        TextBox5.Text = l6.Text;
        Button1.Text = "Update";
    }
}

and my Button Click

protected void Button1_Click(object sender, EventArgs e)
{
    boc.Fname = TextBox1.Text;
    boc.Lname = TextBox2.Text;
    boc.MobNo =Convert.ToInt64(TextBox3.Text);
    boc.Email = TextBox4.Text;
    boc.Salary = Convert.ToDecimal(TextBox5.Text);
    if (Button1.Text == "Save")
    {
        int i = blc.AddToTable(boc);
        if (i == 1)
        {
            DataSet ds = blc.GetData();
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    else if (Button1.Text == "Update")
    {
        int i = blc.UpdateToTable(boc);
        if (i == 1)
        {
            DataSet ds = blc.GetData();
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }

}

It is not showing any error but the when I try to edit nothing happening same values are displaying pls help me


Answers (6)