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
Naimish MakwanaPosted Feb 17, 2023, 9:41 AM
Try below
Create new instance of BusinessObjects class in grid.
Tuhin PaulPosted Feb 17, 2023, 8:15 PM
Hi Jaya,
Based on the code you provided, it looks like you are correctly calling the stored procedure
sp_UpdateTablewith the updated values when the "Update" button is clicked. However, you are not retrieving the updated data from the database and refreshing the GridView control.To fix this, you need to retrieve the updated data from the database after the update operation is completed and bind it to the GridView control. You can do this by modifying the logic layer's
UpdateToTablemethod to return a DataSet containing the updated data, and then using this DataSet to refresh the GridView control. Here's how you can modify your code:In the DAL layer, modify the
UpdateToTablemethod to retrieve the updated data and return it as a DataSet:In the logic layer, modify the
UpdateToTablemethod to call the updatedUpdateToTablemethod in the DAL layer and return the DataSet:In the
GridView1_RowCommandevent handler, modify the "cmdedit" case to call theUpdateToTablemethod and refresh the GridView with the updated data:With these modifications, when the "Update" button is clicked, the updated data will be retrieved from the database and the GridView control will be refreshed with the updated data.
Jignesh KumarPosted Feb 17, 2023, 12:34 PM
Hi Jaya prakash,
You are passing Id in to your update store procedure, Put debugger in below section and check Id is going proper in SP,
else if (e.CommandName == "cmdedit")
Jaya PrakashPosted Feb 17, 2023, 9:21 AM
not working sir
pls give me another solution
Naimish MakwanaPosted Feb 17, 2023, 8:07 AM
Hello Jaya,
By looking at your code it seems you are not passing Id in UpdateToTable method.
check : protected void Button1_Click(object sender, EventArgs e) method and pass record Id. then id should work.
Thanks
Naimish Makwana
Naimish MakwanaPosted Feb 17, 2023, 8:02 AM
Hello Jaya,
After updating the record, did you check the data in Database? Is database value is updated?
Thanks
Naimish