Error Sqldatareader Not All Code Paths Return A Value
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you not all code paths return a value c# try catch finally might have Meta Discuss the workings and policies of this site not all code paths return a value in c# About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or
Not All Code Paths Return A Value In Asp.net C#
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
Try Catch C#
4.7 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("
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 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C# not all code paths return a value try catch up http://stackoverflow.com/questions/14946323/not-all-code-paths-return-a-value-error-in-c-sharp vote 6 down vote favorite 2 I'm having a hard time figuring out how to get around the error with the below code. In this case below I want to return the datatable inside the catch as null. public static DataTable DTTable(string mysqlQuery, out DataTable DTTableTable) { try { MySqlDataAdapter DataDTTables = new MySqlDataAdapter(mysqlQuery, Connection); DataDTTables.SelectCommand.CommandTimeout = 240000; DataTable DataDTTablesDT = new DataTable(); DataDTTables.Fill(DataDTTablesDT); DTTableTable = DataDTTablesDT; EventLog.WriteEntry(StaticStringClass.crawlerID, http://stackoverflow.com/questions/6810090/c-sharp-not-all-code-paths-return-a-value-try-catch "Returning Sucessful datatable query: "+mysqlQuery); return DTTableTable; } catch (Exception ex) { string messageString = "Could not fill database for query: " + mysqlQuery + " because of error: " + ex.Message.ToString(); LoggingClass.GenericLogging(messageString); } } c# share|improve this question asked Jul 24 '11 at 22:08 user222427 Not a big deal, but I recommend changing your local variables to always start with lower-case letters, even if you intend to return them from the function. It makes it easier to tell that they are local variables. You've already doing that with messageString, for example. –Merlyn Morgan-Graham Jul 24 '11 at 22:14 3 Returning out parameter is quite silly, no offence meant. Why such design? Either have it as return value, or as out parameter. –Shadow Wizard Jul 24 '11 at 22:16 add a comment| 7 Answers 7 active oldest votes up vote 20 down vote accepted In your catch block, add a return: catch (Exception ex) { // your code return null; } share|improve this answer answered Jul 24 '11 at 22:10 Tim 21.7k33748 add a comment| up vote 6 down vote You don't return anything after your catch block finishes execution. You need to return some DataTable for that case. public stati
i have creted this code for login control: using https://www.daniweb.com/programming/software-development/threads/69254/solution-for-not-all-code-paths-return-a-value-for-bool System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; http://www.dreamincode.net/forums/topic/121053-error-not-all-code-path-return-a-value/ using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { bool Autheticated = false; not all Autheticated = SiteLevelCustomAutheticationMethod(Login1.UserName, Login1.Password); e.Authenticated = Autheticated; if (Autheticated == true) { Response.Write("Home.aspx"); } } private bool SiteLevelCustomAutheticationMethod (string UserName, string Password) { bool boolReturnValue = false; string scon = "SERVER=.; INITIAL CATALOG=Villaplus; UID=sa; PWD=ash; "; SqlConnection con = new SqlConnection(scon); String strSQL = "Select not all code * From Login"; SqlCommand com = new SqlCommand(strSQL, con); SqlDataReader Dr; con.Open(); Dr = com.ExecuteReader(); while (Dr.Read()) { if ((UserName == Dr["name"].ToString()) & (Password == Dr["Password"].ToString())) { boolReturnValue = true; } Dr.Close(); return boolReturnValue; } } } but it gives error: '_Default.SiteLevelCustomAutheticationMethod(string, string)': not all code paths return a value . please give solution for this. c# msamir 7 posts since Feb 2007 Community Member 7Contributors 9Replies 25Views 9 YearsDiscussion Span 3 Years Ago Last Post by de Source 0 hollystyles 113 9 Years Ago you need to move the return statement outside of the while loop. 0 RwCC 56 9 Years Ago while (Dr.Read()) { if ((UserName == Dr["name"].ToString()) & (Password == Dr["Password"].ToString())) { boolReturnValue = true; } Dr.Close(); } return boolReturnValue; Have the code that way and it should work. 0 reklawnnel
1 New Topic/Question Reply 3 Replies - 4507 Views - Last Post: 19 August 2009 - 06:13 AM Rate Topic: #1 iqbalkmk New D.I.C Head Reputation: 0 Posts: 6 Joined: 18-August 09 Error not all code path return a value Posted 19 August 2009 - 01:40 AM I need a solution ...am new in programing 'PaymentDAL.getyear(int, string)': not all code paths return a value public string getyear(int year,string month) { string result=""; string getingyear="select * from where Year="+year; SqlConnection conn=new SqlConnection(constr); SqlCommand cmd=new SqlCommand("Getingyear",conn); SqlDataReader dr; try { dr=cmd.ExecuteReader(); while(dr.Read()) { if (dr["Month"].ToString() == month) { result = "Already Paid"; return result; } else { result = ""; return result; } } } catch { throw; } finally { cmd.Dispose(); conn.Close(); conn.Dispose(); } Is This A Good Question/Topic? 0 Back to top MultiQuote Quote + Reply Replies To: Error not all code path return a value #2 Renagado D.I.C Regular Reputation: 118 Posts: 388 Joined: 14-June 09 Re: Error not all code path return a value Posted 19 August 2009 - 02:48 AM That's because the code in your catch block doesnt return anything. Simplest way to solve it is this: public string getyear(int year,string month) { string result=""; string getingyear="select * from where Year="+year; SqlConnection conn=new SqlConnection(constr); SqlCommand cmd=new SqlCommand("Getingyear",conn); SqlDataReader dr; try { dr=cmd.ExecuteReader(); while(dr.Read()) { if (dr["Month"].ToString() == month) { result = "Already Paid"; return result; } else { result = ""; return result; } } } catch { return result; } finally { cmd.Dispose(); conn.Close(); conn.Dispose(); } Was This Post Helpful? 0 Back to top MultiQuote Quote + Reply #3 baavgai Dr