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?

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.

This article has been dead for over six months. Start a new discussion instead.