Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form for user artilces detials, on page load i load the data from database and set into the text field, now i have also delete and update button.
When i perform delete operation it successfully runs and delete the data. but when i enter more text in the text with and press update button, the operation completed successfully, but the underlying data in the database doesn't updates.


SQL
Create Proc uspuserupdatearticle
@success bit out,
@articleid int, @Body varchar(1000),@Title varchar(50),@Categories varchar(30)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY	
UPDATE  TblArticles  SET 	   Title	=  @Title , Body  = @Body , Categories =  @Categories ,UpdatedOn = GETDATE()  WHERE   ArticleID =  @articleid
SET @success = 1
END TRY
BEGIN CATCH
SET @success = 0
END CATCH
END		







C#
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

public partial class User_articledetails : System.Web.UI.Page
{
    DBClass db1 = new DBClass();
    Boolean success;
    protected void Page_Load(object sender, EventArgs e)
    {
        ViewState["articleid"] = Convert.ToInt32(Request.QueryString["ID"]);
        if (Session["UserName"] != null && Session["UserID"] != null)
        {
            //Retrieving UserName from Session
            ViewState["UserName"] = Convert.ToString(Session["UserName"]);
            ViewState["UserID"] = Convert.ToInt32(Session["UserID"]);
            Session["UserName"] = Convert.ToString(ViewState["UserName"]);
            Session["UserID"] = Convert.ToInt32(ViewState["UserID"]);

        }

        LoadArticleData();
    }


    private void LoadArticleData()
    {
        try{
            db1.sqlcmd = new SqlCommand("uspgetspecificarticledetails");
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                db1.sqlcmd.CommandType = CommandType.StoredProcedure;
                db1.sqlcmd.Parameters.AddWithValue("@articleid", Convert.ToInt32(ViewState["articleid"]));
                db1.sqlcmd.Parameters.Add("@Title", SqlDbType.VarChar, 50);
                db1.sqlcmd.Parameters["@Title"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters.Add("@Body", SqlDbType.VarChar, 1000);
                db1.sqlcmd.Parameters["@Body"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters.Add("@Categories", SqlDbType.VarChar, 30);
                db1.sqlcmd.Parameters["@Categories"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters.Add("@PublishedOn", SqlDbType.DateTime);
                db1.sqlcmd.Parameters["@PublishedOn"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters.Add("@UpdatedOn", SqlDbType.DateTime);
                db1.sqlcmd.Parameters["@UpdatedOn"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters.Add("@countlikes", SqlDbType.BigInt);
                db1.sqlcmd.Parameters["@countlikes"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters.Add("@success", SqlDbType.Bit);
                db1.sqlcmd.Parameters["@success"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Connection = db1.sqlcon;
                db1.sqlcon.Open();
                db1.sqlcmd.ExecuteScalar();
                ViewState["Title"] = Convert.ToString(db1.sqlcmd.Parameters["@Title"].Value);
                ViewState["body"] = Convert.ToString(db1.sqlcmd.Parameters["@Body"].Value);
                ViewState["Category"] = Convert.ToString(db1.sqlcmd.Parameters["@Categories"].Value);
                ViewState["PublishedOn"] = Convert.ToString(db1.sqlcmd.Parameters["@PublishedOn"].Value);
                ViewState["UpdatedOn"] = Convert.ToString(db1.sqlcmd.Parameters["@UpdatedOn"].Value);
                ViewState["likes"] = db1.sqlcmd.Parameters["@countlikes"].Value;
                success = Convert.ToBoolean(db1.sqlcmd.Parameters["@success"].Value);

            }}
        catch(Exception ex)
        {
            Response.Write(ex);
        }

       finally{

            if(success == true)
            {
                tbtitle.Text = Convert.ToString(ViewState["Title"]);
                tbbody.Text = Convert.ToString(ViewState["body"]);
                tbcategory.Text = Convert.ToString(ViewState["Category"]);
                lblpublishedon.Text = Convert.ToString(ViewState["PublishedOn"]);
                if (Convert.ToString(ViewState["UpdatedOn"]) == Convert.ToString(System.DateTime.Now))
                {
                    lblupdatedon.Text = "Not Yet Updatedd";

                }
                else
                { lblupdatedon.Text = Convert.ToString(ViewState["UpdatedOn"]); }
                
                if (Convert.ToInt64(ViewState["likes"]) == 1)
                {
                    lbllikes.Text = "0";
                }
                else
                { lbllikes.Text = Convert.ToString(Convert.ToInt64(ViewState["likes"])); }
                
                db1.sqlcon.Close();

            }
            else
            {

            }

        }

        }



    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Response.Write(ViewState["articleid"]);
        try
        {
            db1.sqlcmd = new SqlCommand("uspuserupdatearticle");
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                db1.sqlcmd.CommandType = CommandType.StoredProcedure;
                db1.sqlcmd.Parameters.AddWithValue("@articleid", Convert.ToInt32(ViewState["articleid"]));
                //db1.sqlcmd.Parameters.AddWithValue("@Body", Convert.ToString(tbbody.Text.Trim()));
                db1.sqlcmd.Parameters.AddWithValue("@Body", "Body from app");
                Response.Write(tbbody.Text.Trim());
                //db1.sqlcmd.Parameters.AddWithValue("@Title", Convert.ToString(tbtitle.Text.Trim()));
                db1.sqlcmd.Parameters.AddWithValue("@Title", "Title from app");
                Response.Write(tbtitle.Text.Trim());
                //db1.sqlcmd.Parameters.AddWithValue("@Categories", Convert.ToString(tbcategory.Text.Trim()));
                db1.sqlcmd.Parameters.AddWithValue("@Categories", "category from app");
                Response.Write(tbcategory.Text.Trim());
                db1.sqlcmd.Parameters.Add("@success", SqlDbType.Bit);
                db1.sqlcmd.Parameters["@success"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Connection = db1.sqlcon;
                db1.sqlcon.Open();
                db1.sqlcmd.ExecuteScalar();
                success = Convert.ToBoolean(db1.sqlcmd.Parameters["@success"].Value);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
        finally
        {
            if (success == true)
            {
                Response.Write("Updatation Successfull");
                //Response.Redirect(Request.RawUrl);
            }
            else
            {
                Response.Write("Updation  Un-Successfull");
            }
        }
    }


    protected void btndelete_Click(object sender, EventArgs e)
    {
        try
        {
            db1.sqlcmd = new SqlCommand("uspuserdeletearticles");
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                db1.sqlcmd.CommandType = CommandType.StoredProcedure;
                db1.sqlcmd.Parameters.AddWithValue("@articleid", Convert.ToInt32(ViewState["articleid"]));
                db1.sqlcmd.Parameters.Add("@success", SqlDbType.Bit);
                db1.sqlcmd.Parameters["@success"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Connection = db1.sqlcon;
                db1.sqlcon.Open();
                db1.sqlcmd.ExecuteScalar();
                success = Convert.ToBoolean(db1.sqlcmd.Parameters["@success"].Value);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
        finally
        {
            if (success == true)
            {
                Response.Write("Deleteiton Successfull");
                Response.Redirect("../User/Mynotifications.aspx");
            }
            else
            {
                Response.Write("Deleteiton Un-Successfull");
            }
        }
    }


}



ASP.NET
       <%@ Page Title="" Language="C#" MasterPageFile="~/User/Users.master" AutoEventWireup="true" CodeFile="articledetails.aspx.cs" Inherits="User_articledetails" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div style="width: 99.6%; height: 925px; border: 1px ridge black; text-align: center; vertical-align: middle;">
        <%-- Div Heading--%>
        <div style="width: 100%; height: 30px; line-height: 30px; font-family: Calibri; font-size: x-large; font-weight: lighter; font-style: normal; font-variant: small-caps; text-transform: uppercase; color: #FFFFFF; text-decoration: none; background-color: #000000;">
            <asp:Label ID="Label11" runat="server" Text="Post Article"></asp:Label>
        </div>







        <div style="width: 98%; height: 665px; margin: 0 auto; margin-removed 10px;">

            <%--1st Row--%>
            <div style="width: 12%; height: 25px; line-height: 25px; float: left; margin-removed 20px; text-align: right; font-variant: small-caps; font-style: normal; font-weight: lighter; text-transform: uppercase; color: #000000; font-family: 'Carrois Gothic'; font-size: medium;">
                <asp:Label ID="Label1" runat="server" Text="Title" ForeColor="Black"></asp:Label>
            </div>
            <div style="width: 80%; height: 25px; line-height: 25px; float: left; margin-removed 5%; margin-removed 20px; text-align: left;">
                <asp:TextBox ID="tbtitle" runat="server" CssClass="TextBoxClass" Width="95%" Height="25px" placeholder="Enter Your Article Title with only 20 to 50 chracters"></asp:TextBox>
                <%--<asp:RequiredFieldValidator ID="RequiredFieldValidatortitle" runat="server" Display="None" ControlToValidate="tbtitle" Text="*" ErrorMessage="Please Input Article Title" ForeColor="Red" Font-Names="Calibri"></asp:RequiredFieldValidator>--%>
                <%--<asp:RegularExpressionValidator ID="RegularExpressionValidatortitle" runat="server" Display="None" ControlToValidate="tbtitle" ValidationExpression="^[a-zA-Z''-'\s]{20,50}$" ErrorMessage="Article Title Name Must Contain Only 20-50 Characters" Text="*" Font-Names="Calibri" Font-Size="Small" ForeColor="Red"></asp:RegularExpressionValidator>--%>
            </div>
            <%--2nd Row--%>
            <div style="width: 12%; height: 25px; line-height: 25px; float: left; margin-removed 20px; text-align: right; font-variant: small-caps; font-style: normal; font-weight: lighter; text-transform: uppercase; color: #000000; font-family: 'Carrois Gothic'; font-size: medium;">
                <asp:Label ID="Label2" runat="server" Text="Body" ForeColor="Black"></asp:Label>
            </div>
            <div style="width: 80%; height: 300px; line-height: 20px; float: left; margin-removed 5%; margin-removed 20px; text-align: left;">
                <asp:TextBox ID="tbbody" runat="server" CssClass="TextBoxClass" Width="95%" Height="300px" placeholder="Enter Your Article body with only 50 to 500 chracters" MaxLength="500" Rows="60" TextMode="MultiLine"></asp:TextBox>
                <%--<asp:RequiredFieldValidator ID="RequiredFieldValidatorbody" runat="server" Display="None" ControlToValidate="tbbody" Text="*" ErrorMessage="Please Input Article Title" ForeColor="Red" Font-Names="Calibri"></asp:RequiredFieldValidator>--%>
                <%--<asp:RegularExpressionValidator ID="RegularExpressionValidatorbody" runat="server" Display="None" ControlToValidate="tbbody" ValidationExpression="^.{50,500}$" ErrorMessage="Article Body Name Must Contain Only 50-500 Characters" Text="*" Font-Names="Calibri" Font-Size="Small" ForeColor="Red"></asp:RegularExpressionValidator>--%>
            </div>
            <%--3rd Row--%>
            <%--4th Row--%>
            <div style="width: 12%; height: 25px; line-height: 25px; float: left; margin-removed 20px; text-align: right; font-variant: small-caps; font-style: normal; font-weight: lighter; text-transform: uppercase; color: #000000; font-family: 'Carrois Gothic'; font-size: medium;">
                <asp:Label ID="Label4" runat="server" Text="Category" ForeColor="Black"></asp:Label>
            </div>
            <div style="width: 80%; height: 25px; line-height: 25px; float: left; margin-removed 5%; margin-removed 20px; text-align: left;">
                <asp:TextBox ID="tbcategory" runat="server" CssClass="TextBoxClass" Width="95%" Height="25px" placeholder="Enter Your Article Title with only 5 to 30 chracters"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorcategory" Display="None" runat="server" ControlToValidate="tbcategory" Text="*" ErrorMessage="Please Input Article Category" ForeColor="Red" Font-Names="Calibri"></asp:RequiredFieldValidator>
                <%--<asp:RegularExpressionValidator ID="RegularExpressionValidatorcategory" runat="server" ControlToValidate="tbcategory" ValidationExpression="^[a-zA-Z''-'\s]{5,30}$" ErrorMessage="Article Title Name Must Contain Only 1-50 Characters" Text="*" Font-Names="Calibri" Font-Size="Small" ForeColor="Red"></asp:RegularExpressionValidator>--%>
            </div>

            <%--5th Row--%>
            <div style="width: 12%; height: 25px; line-height: 25px; float: left; margin-removed 20px; text-align: right; font-variant: small-caps; font-style: normal; font-weight: lighter; text-transform: uppercase; color: #000000; font-family: 'Carrois Gothic'; font-size: medium;">
                <asp:Label ID="Label5" runat="server" Text="Published On" ForeColor="Black"></asp:Label>
            </div>
            <div style="width: 30%; height: 25px; line-height: 25px; float: left; margin-removed 5%; margin-removed 20px; text-align: left;">
                <asp:Label ID="lblpublishedon" runat="server" ForeColor="Black"></asp:Label>
            </div>
            <div style="width: 12%; height: 25px; line-height: 25px; float: left; margin-removed 20px; margin-removed 5%; text-align: right; font-variant: small-caps; font-style: normal; font-weight: lighter; text-transform: uppercase; color: #000000; font-family: 'Carrois Gothic'; font-size: medium;">
                <asp:Label ID="Label6" runat="server" Text="Updated On" ForeColor="Black"></asp:Label>
            </div>
            <div style="width: 30%; height: 25px; line-height: 25px; float: left; margin-removed 5%; margin-removed 20px; text-align: left;">
                <asp:Label ID="lblupdatedon" runat="server" ForeColor="Black"></asp:Label>
            </div>
            <%--5th Row--%>
            <div style="width: 12%; height: 25px; line-height: 25px; float: left; margin-removed 20px; text-align: right; font-variant: small-caps; font-style: normal; font-weight: lighter; text-transform: uppercase; color: #000000; font-family: 'Carrois Gothic'; font-size: medium;">
                <asp:Label ID="Label7" runat="server" Text="Likes" ForeColor="Black"></asp:Label>
            </div>
            <div style="width: 30%; height: 25px; line-height: 25px; float: left; margin-removed 5%; margin-removed 20px; text-align: left;">
                <asp:Label ID="lbllikes" runat="server" ForeColor="Black"></asp:Label>
            </div>
            <div style="width: 12%; height: 25px; line-height: 25px; float: left; margin-removed 20px; margin-removed 5%; text-align: right; font-variant: small-caps; font-style: normal; font-weight: lighter; text-transform: uppercase; color: #000000; font-family: 'Carrois Gothic'; font-size: medium;">
            </div>
            <div style="width: 30%; height: 25px; line-height: 25px; float: left; margin-removed 5%; margin-removed 20px; text-align: left;">
            </div>
            <%--7th Row--%>
            <div style="width: 100%; height: 25px; line-height: 25px; float: left; margin-removed 30px; text-align: center; font-variant: small-caps; font-style: normal; font-weight: lighter; text-transform: uppercase; color: #000000; font-family: 'Carrois Gothic'; font-size: medium;">
                <asp:Button ID="btnUpdate" runat="server" CssClass="BtnClass" Text="SAVE" Width="80px" Height="25px" OnClick="btnUpdate_Click" />
                        
                <asp:Button ID="btndelete" runat="server" CssClass="BtnClass" Text="DELETE" Width="80px" Height="25px" OnClick="btndelete_Click" />
            </div>
        </div>

    </div>
</asp:Content>
Posted
Updated 16-Jun-14 23:24pm
v3
Comments
Prasad Avunoori 16-Jun-14 23:54pm    
Make sure ViewState["notifictid"] has the value.
Prasad Avunoori 16-Jun-14 23:59pm    
Alter your stored procedure as follows:

CREATE PROCEDURE uspuserupdatearticle @success BIT OUTPUT
,@articleid INT
,@Body VARCHAR(1000)
,@Title VARCHAR(50)
,@Categories VARCHAR(30)
AS
BEGIN
SET NOCOUNT ON;

BEGIN TRY
SET @success = 0

UPDATE TblArticles
SET Title = @Title
,Body = @Body
,Categories = @Categories
,UpdatedOn = GETDATE()
WHERE ArticleID = @articleid

IF @@ROWCOUNT > 0
SET @success = 1
END TRY

BEGIN CATCH
SET @success = 0
END CATCH
END
Muhammad Taqi Hassan Bukhari 17-Jun-14 0:08am    
ViewState["notifictid"] has the value , the problem is column UpdatedOn is being updated but not Title, Body and Categories. and also the last statement executes Response.Write("Updatation Successfull");
Muhammad Taqi Hassan Bukhari 17-Jun-14 0:17am    
i have tried your sp, but still facing same problem, the problem is column UpdatedOn is being updated but not Title, Body and Categories. and also the last statement executes Response.Write("Updatation Successfull");
Prasad Avunoori 17-Jun-14 0:55am    
Can you please share your table's create script?

1 solution

Dear,

1. Check the size of column in table structure.
2. Increase a size of parameter for text field into the store procedure.



SQL
Create Proc uspuserupdatearticle
@success bit out,
@articleid int, 
@Body varchar(change), -- as per table structure
@Title varchar(change), -- as per table structure
@Categories varchar(change) -- as per table structure
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY	
UPDATE  TblArticles  SET 	   Title	=  @Title , Body  = @Body , Categories =  @Categories ,UpdatedOn = GETDATE()  WHERE   ArticleID =  @articleid
SET @success = 1
END TRY
BEGIN CATCH
SET @success = 0
END CATCH
END	
 
Share this answer
 
v3

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