Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
3 tier architecture

.aspx page


XML
<div align="center">

    <asp:Label ID="lblMessage" runat="Server" ForeColor="red" EnableViewState="False"></asp:Label>

        <table style="border:2px solid black" cellpadding="2" cellspacing="2">
            <tr>
                <td>Name:
                </td>
                <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Age:</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td align="left"><asp:Button ID="Button1" runat="server" Text="Save"
                        onclick="Button1_Click" /></td></tr>
        </table>

        <div><br /><br /><br />
            <asp:GridView ID="GridView1" runat="server" DataKeyNames="Id" AutoGenerateColumns="False"
                AutoGenerateEditButton="True" OnRowEditing="EditRecord"
                OnRowUpdating="UpdateRecord" OnRowDeleting="DeleteRecord">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True"  />
                <asp:TemplateField HeaderText="Name" >
                    <ItemTemplate>
                        <%# Eval("Name") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtname" runat="Server" Text='<%# Eval("Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Age" >
                    <ItemTemplate>
                        <%# Eval("Age") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtage" runat="Server" Text='<%# Eval("Age") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete?">
                    <ItemTemplate>
                        <span onclick="return confirm('Are you sure to Delete?')">
                            <asp:LinkButton ID="lnBD" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
                        </span>
                    </ItemTemplate>
                </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </div>



aspx.cs page



C#
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
        int Id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        int intResult = 0;
        GridViewRow row = GridView1.Rows[e.RowIndex];

        TextBox tFN = (TextBox)row.FindControl("txtname");
        TextBox tAge = (TextBox)row.FindControl("txtage");

        // instantiate BAL
       personbal pbal = new personbal();

        try
        {
            intResult = pbal.Update(Id, tFN.Text, int.Parse(tAge.Text));
            if (intResult > 0)
                lblMessage.Text = "Record Updated Successfully.";
            else
                lblMessage.Text = "Record couldn't updated";
        }
        catch (Exception ee)
        {
            lblMessage.Text = ee.Message.ToString();
        }
        finally
        {
            pbal = null;
        }

        GridView1.EditIndex = -1;
        // Refresh the list
        bind();
    }


Dal.cs



C#
public int Update(int Id, string Name, int Age)
    {
        SqlConnection sc = new SqlConnection(con);
        sc.Open();
        SqlCommand cmdupdate = new SqlCommand("UpdateValue", sc);
        cmdupdate.CommandType = CommandType.StoredProcedure;
        try
        {
            cmdupdate.Parameters.AddWithValue("@Name", Name);
            cmdupdate.Parameters.AddWithValue("@Age", Age);
            cmdupdate.Parameters.AddWithValue("@Id", Id);
            return cmdupdate.ExecuteNonQuery();
        }
        catch
        {
            throw;
        }
        finally
        {
            cmdupdate.Dispose();
            sc.Close();
            sc.Dispose();
        }
    }


Bal.cs




C#
public int Update(int Id, string Name, int Age)
   {
       persondal pdal = new persondal();
       try
       {
           return pdal.Update(Id, Name, Age);
       }
       catch
       {
           throw;
       }
       finally
       {
           pdal = null;
       }
   }


stored procedure

SQL
ALTER PROCEDURE UpdateValue
(
	@Id int,
	@Name varchar(50),
	@Age int
)
AS
	/* SET NOCOUNT ON */ 
	UPDATE Person SET 
		Name = @Name,  
		Age = @Age 
		WHERE Id = @Id
	RETURN


Insert,Load and delete is working fine but update is not working!!! :(

update is working,it gives successful message on compilation and executing BUT IN DATABASE ITS NOT UPDATING. :(

Please Help me out Guys :) I have interview tommo on 3 tier architecture :(

regards
harsha
Posted
Updated 6-Sep-12 5:42am
v2
Comments
AmitGajjar 6-Sep-12 11:54am    
Check if correct ID pass in the DAL ? If yes then next you need to check your stored Procedure by executing using EXEC command. The only problem i can see in your solution is, return @@rowcount; instead of only return in your stored procedure.
Harsha Dev 6-Sep-12 12:43pm    
Thanks man... will check it...
Harsha Dev 6-Sep-12 12:43pm    
ALTER PROCEDURE UpdateValue
(
@Id int,
@Name varchar(50),
@Age int
)
AS
/* SET NOCOUNT ON */
DECLARE @Count int
select @Count=Count(Id) from Person where Name=@Name

IF @Count=0
BEGIN
UPDATE Person SET
Name = @Name,
Age = @Age
WHERE Id = @Id

END
RETURN @Count
RETURN

Is this ok?
Harsha Dev 6-Sep-12 12:51pm    
output -

Procedure or function 'UpdateValue' expects parameter '@Id', which was not supplied.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE =
Finished running [dbo].[UpdateValue].
[no name] 6-Sep-12 15:36pm    
Did you try debugger at int Id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()); to see if it is getting an id ?

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