Not All Code Paths Return A Value Error In Asp.net
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Not all code paths return a value error in C# up vote -4 down vote favorite I am trying to load my charts from the Page_Load event but getting this error "not all code paths return a value" and i am not sure what am I doing wrong here. Can someone help. Here is my code: protected void Page_Load(object sender, EventArgs e) { Literal2.Text = CreateChart_2(); } public string CreateChart_2() { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString); // Initialize the string which would contain the chart data in XML format StringBuilder xmlStr = new StringBuilder(); // Provide the relevant customization attributes to the chart xmlStr.Append("
Tips/Tricks Top Articles Beginner Articles Technical Blogs Posting/Update Guidelines Article Help Forum Article Competition Submit an article or tip Post your Blog quick answersQ&A Ask a Question View Unanswered Questions View All Questions... Linux questions C# questions ASP.NET questions SQL questions fabric questions discussionsforums All Message Boards... Application Lifecycle> Running a Business Sales / Marketing Collaboration / Beta Testing Work Issues Design and Architecture ASP.NET JavaScript C / C++ / MFC> ATL / WTL / STL Managed C++/CLI C# Free Tools Objective-C and Swift Database Hardware & Devices> System http://stackoverflow.com/questions/14946323/not-all-code-paths-return-a-value-error-in-c-sharp Admin Hosting and Servers Java .NET Framework Android iOS Mobile SharePoint Silverlight / WPF Visual Basic Web Development Site Bugs / Suggestions Spam and Abuse Watch features Competitions News The Insider Newsletter The Daily Build Newsletter Newsletter archive Surveys Product Showcase Research Library CodeProject Stuff communitylounge Who's Who Most Valuable Professionals The Lounge The Insider News The Weird & The Wonderful http://www.codeproject.com/Questions/443341/Not-All-Code-Path-Return-A-Value The Soapbox Press Releases Non-English Language > General Indian Topics General Chinese Topics help What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum Site Map Advertise with us About our Advertising Employment Opportunities About Us Ask a Question All Questions All Unanswered FAQ Not All Code Path Return A Value Rate this: Please Sign up or sign in to vote. See more: C# ASP.NET .NET Dear Friends, I have written a code like this to check the access level. But I am getting the error as "Not All Code Path Return A Value" Kindly suggest the reason and solution for the same protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (isAccessLevel() == "YES") { Show_My_Data() } else { Hide_My_Data() } } } public string isAccessLevel() { if (ACC_YN == "YES") { if (STA_YN == "RELEASE") { return "YES"; } else { return "NO"; } } } Posted 20-Aug-12 0:44am Arunprasath Natarajan3.4K Add a Solution 7 solutions Top Rated Most Recent Rate this: Please Sign up or sign in to vote. Solution 1 Accept So
help me. I'm trying to pass the name of the user who logs in from a login form and display his/her name on a https://www.daniweb.com/programming/software-development/threads/365710/c-error-not-all-code-paths-return-a-value Main form. I'm using get{} and set{}. My problem is with the function https://msdn.microsoft.com/en-us/library/87cz4k9t.aspx I'm trying to create to return the name of the user which I'm going to place inside my get{}. Here's my function (I've placed this within a Class): public static string loggedinuser(TextBox uname,TextBox password) { readrecord("Select dbo.loggeduser('" + uname.Text + "','" + password.Text + "')"); if (dr.Read()) { while not all (dr.Read()) { string name =(dr[0].ToString()); return name; } } con.Close(); } It's showing this error "Not all code paths return a value." I don't understand. I already have the return statement. If somebody can help me, I'm most grateful. Thanks so much in advance! c# Chair 17 34 posts since Jan 2011 Community Member 9Contributors 11Replies 89Views 5 YearsDiscussion Span 2 not all code Years Ago Last Post by pritaeas 0 Mitja Bonca 557 5 Years Ago Your code does not return the name string (value) from all over the method. It reutrns only from the while loop. What in case if the code does not go to while loop? It will return what? notihng. But your method return type is a string. That means you have to return string even if the code does not go to while loop. Do it like this: public static string loggedinuser(TextBox uname,TextBox password) { string name = null; readrecord("Select dbo.loggeduser('" + uname.Text + "','" + password.Text + "')"); if (dr.Read()) { while (dr.Read()) { name =(dr[0].ToString()); } } con.Close(); return name; } In case if the code will no go into while loop, it will return a string with null value. So on the other side you can check it: string _name = loggeddinuser(uname, password); if(_uname != null) { //your code if the string returns a not null string! } else MessageBox.Show("Value is null."); //or do some other user notification. 0 skatamatic 371 5 Years Ago The return statement
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Samples Retired content We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. MSDN Library MSDN Library MSDN Library MSDN Library Design Tools Development Tools and Languages Mobile and Embedded Development .NET Development Office development Online Services Open Specifications patterns & practices Servers and Enterprise Development Speech Technologies Web Development Windows Desktop App Development TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Compiler Error CS0161 Other Versions Visual Studio 2008 Visual Studio 2005 Visual Studio .NET 2003 'method': not all code paths return a valueA method that returns a value must have a return statement in all code paths. For more information, see Methods (C# Programming Guide).The following sample generates CS0161: Copy // CS0161.cs public class Test { public static int Main() // CS0161 { int i = 10; if (i < 10) { return i; } else { // uncomment the following line to resolve // return 1; } } } Show: Inherited Protected Print Export (0) Print Export (0) Share IN THIS ARTICLE Is this page helpful? Yes No Additional feedback? 1500 characters remaining Submit Skip this Thank you! We appreciate your feedback. Dev centers Windows Office Visual Studio Microsoft Azure More... Learning resources Microsoft Virtual Academy Channel 9 MSDN Magazine Community Forums Blogs Codeplex Support Self support Programs BizSpark (for startups) Microsoft Imagine (for students) United States (English) Newsletter Privacy & cookies Terms of use Trademarks © 2016 Microsoft © 2016 Microsoft