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.
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
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)
{
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", "Body from app");
Response.Write(tbbody.Text.Trim());
db1.sqlcmd.Parameters.AddWithValue("@Title", "Title from app");
Response.Write(tbtitle.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");
}
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");
}
}
}
}
<%@ 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 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;">
<%----%>
<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>
<%----%>
<%----%>
</div>
<%----%>
<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>
<%----%>
<%----%>
</div>
<%----%>
<%----%>
<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>
<%----%>
</div>
<%----%>
<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>
<%----%>
<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>
<%----%>
<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>