Asp.NET没有更新新数据 [英] Asp.NET not updating new data

查看:90
本文介绍了Asp.NET没有更新新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我在asp.net网站上工作,但奇妙的是我无法更新数据这里是我的代码:

hi guys
im working on an asp.net web site but wonderfully i cant update data here is my code:

int result = SqlHelper.ExecuteNonQuery(SqlHelper.CreateCommand("UpdateArticle", CommandType.StoredProcedure,
new SqlParameter("@ID", long.Parse(Request.QueryString["ArticleID"])),
new SqlParameter("@CategoryTitle",                                                                           this.Category.SelectedItem.Text),
new SqlParameter("@Title", this.ArticleTitle.Text),
new SqlParameter("@Summary", this.ArticleSummary.Text),
new SqlParameter("@UserName", Context.User.Identity.Name),
new SqlParameter("@IsTmb", this.Tmb.Checked),
new SqlParameter("@IsDailty", this.Daily.Checked),
new SqlParameter("@TmbPhotoID", long.Parse(TmbPhotoID.Text)),
new SqlParameter("@Body", this.Editor.Text)
                                      ));
        this.InfoLabel.Text = result == 1 ? "Update successfully" : "Failed";





我的助手类和我的连接字符串经过严格测试,在这里是我的标记:



My helper class and my connection string is very tested and here is my mark up:

<div class="InsertItemContainer">
             <div>
                 <label for="Category">Category:</label>
                 <asp:DropDownList runat="server" ID="Category"></asp:DropDownList>
             </div>
             <div>
                 <label for="ArticleTitle" style="margin-left: 32px;">Title:</label>
                 <asp:TextBox runat="server" ID="ArticleTitle" Style="width: 60%;" />
                 <asp:RequiredFieldValidator runat="server" ID="TitleValidator" ControlToValidate="ArticleTitle" ErrorMessage="Its Needed!!"></asp:RequiredFieldValidator>
             </div>
             <div>
                 <label for="ArticleSummary" style="margin-left: 32px;">Summary:</label><asp:TextBox runat="server" ID="ArticleSummary" TextMode="MultiLine" Style="vertical-align: middle; width: 60%; resize: none;" />
                 <asp:RequiredFieldValidator runat="server" ID="SummaryValidator" ControlToValidate="ArticleSummary" ErrorMessage="Its Needed!!"></asp:RequiredFieldValidator>
             </div>

         </div>
         <CKEditor:CKEditorControl  runat="server" ID="Editor"></CKEditor:CKEditorControl>
         <asp:RequiredFieldValidator runat="server" ID="EditorValidator" ControlToValidate="Editor" ErrorMessage="Its needed"></asp:RequiredFieldValidator>

         <div style="direction: rtl;">
             <asp:CheckBox runat="server" ID="Daily" Text="DailyITems" />
             <asp:CheckBox runat="server" ID="Tmb" Text="Mosaic" />
         </div>

         <asp:Button runat="server" ID="Update" Text="Update" OnClick="Update_OnClick" CausesValidation="False" />
         <asp:TextBox runat="server" ID="TmbPhotoID" placeHolder="TmbPhoto Id here" TextMode="Number"></asp:TextBox>







更值得一提的是有时当我用绝对字符串替换this.ArticleTitle.Text是工作@ - @时抛出没有异常。除了一些时候在DropDownList中没有选择多个项目。被抛出异常。

,这是我的程序:








and something even more wondefully is some times when i replace the "this.ArticleTitle.Text with an absolute string is works @-@ no exception is thrown. except some times "Cannot have multiple items selected in a DropDownList." is thrown as exception.
and here is my procedure:


USE [WebServices]
GO
/****** Object:  StoredProcedure [dbo].[UpdateArticle]    Script Date: 9/7/2014 5:11:14 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER	Procedure	[dbo].[UpdateArticle]
@ID						BigInt,
@CategoryTitle			nVarChar(300),
@UserName				nVarChar(300),
@Title					nVarChar(600),
@IsTmb					bit,
@IsDaily				bit,
@Summary				nVarChar(Max),
@Body					nVarChar(Max),
@TmbPhotoID				BigInt
As
Declare		@CategoryID	as	BigInt
Declare		@UserID		as	UniqueIdentifier	

Select	@UserID	=	
	(
		Select	UserID	from	aspnet_Users	Where	UserName	=	@UserName
	)
	
	Select	@CategoryID	=	
	(
		Select	ID	From	Category	Where	Category.Title	=	@CategoryTitle
	)
	
	begin try
	Update	Article
		Set		CategoryID = @CategoryID,
				UserID =	@UserID,
				IsTmb = @IsTmb,
				IsDaily = @IsDaily,
				Body = @Body,
				Title = @Title,
				Summary = @Summary,
				TmbPhotoID = @TmbPhotoID

				Where	ID	=	@ID
				return 1
	end try
	begin catch
		return 0
	end catch







感谢任何阅读或帮助的人:)




thanks any one who read or help :)

推荐答案

这是DropDown的DataBinding问题。



如果你绑定了s ame DataSource有多个DropDownLists,或者当同一个ListItem绑定到多个DropDownLists时,可能会出现此错误。





Quick修复



快速解决方法是在读取当前选择的值之前清除DropDown的选择,如下所示...

It is DataBinding problem of the DropDown.

If you are binding the same DataSource with multiple DropDownLists or when the same ListItem is binded to multiple DropDownLists, then this error may come.


Quick Fix

A quick fix is to clear the selection of DropDown before reading its currently selected value like below...
Category.Items.Clear();






Or

Category.clearselection();



参考 ASP .NET Tips:DropDownList.ClearSelection()避免不能在DropDownList中选择多个项目 [ ^ ]





永久解决方案



如果要解决问题的根本原因,请检查DataBind或ListItems是否在多个DropDowns中重复。为此总是创建ListItem的新实例,不要重复使用前面的实例。



参考 - 无法在DropDownList中选择多个项目 [ ^ ]


这篇关于Asp.NET没有更新新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆