Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server , +
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>
 

 

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 Smile | :)
Posted 7-May-13 4:04am
Edited 7-May-13 5:01am
v9
Comments
Pheonyx at 7-May-13 9:05am
   
You said "no exception is thrown. " when you use an absolute string... what exception is thrown when you don't?
RedSakura at 7-May-13 9:13am
   
it just update the title and thats all no exceptions @.@
KM Perumal at 7-May-13 9:07am
   
where is parameter of body????
CHill60 at 7-May-13 9:56am
   
new SqlParameter("UserName", Context.User.Identity.Name), - should that be "@Username" ?
RedSakura at 7-May-13 10:02am
   
that was copy paste failure :D
in my code its @UserName but still dosnt work...
Tadit Dash at 7-May-13 12:42pm
   
Which is a dropdown ? Is "ArticleTitle" a dropdown ?
RedSakura at 7-May-13 13:30pm
   
the category is a dropdown.
ArticleTitle is a TextBox

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
Refer- ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"[^]
 

Permanent Solution
 
If you want to fix the root cause of the problem, then check whether the DataBind or ListItems are duplicated in multiple DropDowns. For that always create new instance of ListItem, don't reuse the previous instance.
 
Refer- Cannot have multiple items selected in a DropDownList[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 DamithSL 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 7 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100