This is my Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

{
bool result = false;
result = Test.Test123(User.Text, Password.Text);

if ((result == true))
Response.Redirect("Test.aspx");
else
lblText.Text = "ERROR!";
}

}
}

Im trying to invoke the method inside a seperate class file.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Security.Cryptography;

/// <summary>
/// Summary description for Test
/// </summary>
public class Test
{

private bool Test123(String User, String Password)
{


SqlConnection conn = new SqlConnection(("data source=ITCU-50;integrated security=true;" + "initial catalog=Test"));

string sqlstr = ("SELECT User, Password FROM Test WHERE User = \'"
+ (User + "\'"));

SqlCommand cmd = new SqlCommand(sqlstr, conn);
conn.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();

while (dr.Read()) {
if ((User == dr["User"].ToString()) & (Password == dr["Password"].ToString()))
{
return true;
}

dr.Close();
conn.Close();

}


}
}
Error occurs and the error message is "Test.Test123(string, string)': not all code paths return a value.

Any kind souls out there can provide me with a solution please?

Recommended Answers

All 4 Replies

This is my Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

{
bool result = false;
result = Test.Test123(User.Text, Password.Text);

if ((result == true))
Response.Redirect("Test.aspx");
else
lblText.Text = "ERROR!";
}

}
}



Im trying to invoke the method inside a seperate class file.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Security.Cryptography;

/// <summary>
/// Summary description for Test
/// </summary>
public class Test
{

private bool Test123(String User, String Password)
{


SqlConnection conn = new SqlConnection(("data source=ITCU-50;integrated security=true;" + "initial catalog=Test"));

string sqlstr = ("SELECT User, Password FROM Test WHERE User = \'"
+ (User + "\'"));

SqlCommand cmd = new SqlCommand(sqlstr, conn);
conn.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();

while (dr.Read()) {
if ((User == dr["User"].ToString()) & (Password == dr["Password"].ToString()))
{
return true;
}

dr.Close();
conn.Close();

}


}
}


Error occurs and the error message is "Test.Test123(string, string)': not all code paths return a value.

Any kind souls out there can provide me with a solution please?

In "private bool Test123(String User, String Password)" this method you are not returning any value. so its giving that error.

private bool is a function and all functions must use the return keyword to return a value. if you do not want to return a value--use a procedure

So what should I add to get a return value. I am totally new to C# and Java. Any solutions?

return true;
This should be after -- conn.Close();
}

Just before the closing } of the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.