Hi all,anyone can help me to solve this error.When I build the code it shows the error message below:What I have to do ?


C:\Inetpub\wwwroot\shankar\WebApplication1\login.aspx.cs(83): 'WebApplication1.WebForm1.Custvaluserid_ServerValidate(object, System.Web.UI.WebControls.ServerValidateEventArgs)': not all code paths return a value


This is the code I written:

private bool Custvaluserid_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
SqlConnection conn= new SqlConnection();
conn.ConnectionString="server= 'server name';uid=sa;pwd=;database=";
string query="select ADMINUSERID from ADMIN where ADMINUSERID='admin'";
SqlCommand comm=new SqlCommand(query,conn);
conn.Open();
SqlDataReader dr=comm.ExecuteReader();
args.IsValid = false; 


while(dr.Read())
{
bool msg;
string txtuserid;
txtuserid=(dr [ "ADMINUSERID" ].ToString ( ));

if(txtuserid==args.Value)
{
args.IsValid = true;
msg=true;
return msg;
}
}
}

Here's a hint, what happens if your query returns 0 rows?

Hi Campkev, Before the Custvaluserid_ServerValidate event ,I check the page through the button click event.
private void btnlogin_Click(object sender, System.EventArgs e)
{
if (Page.Isvalid)
{
lblmessage.Text="Userid is correct";
}
else
lblmessage.Text="Userid is wrong";
}

}
In this nothing it has return,when executing, the else part only executed ,i.e Userid is wrong message displayed.What is wrong in the code.. ?
Thanks for your response.

sorry, bub, but that makes no sense because:
1) how is Page.IsValid supposed to know if the user is in your database
2) if you do already know that the user is valid, what is the point of this function.

In any event, here is what you need to do to fix it. I will give you two ways. The first takes the least amount of changes to your code. However, I would recommend the second as I hate multiple return paths.
Fix 1:

privatebool Custvaluserid_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
SqlConnection conn= new SqlConnection();
conn.ConnectionString="server= 'server name';uid=sa;pwd=;database=";
string query="select ADMINUSERID from ADMIN where ADMINUSERID='admin'";
SqlCommand comm=new SqlCommand(query,conn);
conn.Open();
SqlDataReader dr=comm.ExecuteReader();
args.IsValid = false; 


while(dr.Read())
{
bool msg;
string txtuserid;
txtuserid=(dr [ "ADMINUSERID" ].ToString ( ));

if(txtuserid==args.Value)
{
args.IsValid = true;
msg=true;
return msg;
}
}
return false;
}

Fix 2

privatebool Custvaluserid_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
SqlConnection conn= new SqlConnection();
conn.ConnectionString="server= 'server name';uid=sa;pwd=;database=";
string query="select ADMINUSERID from ADMIN where ADMINUSERID='admin'";
SqlCommand comm=new SqlCommand(query,conn);
conn.Open();
SqlDataReader dr=comm.ExecuteReader();
args.IsValid = false; 


while(dr.Read())
{
if(dr[ "ADMINUSERID" ].ToString( )==args.Value)
{
args.IsValid = true;
break;
}
}
return args.IsValid;
}

Hi,

Your problem is with compiler, the above fixes are correct. You create a bool msg but it gets assigned a value only if(txtuserid==args.Value) and also the only return statement is in this block. Even if you are sure that an if statement will always evaluate to true (problem:then why put one, use assertions instead ?) the compiler can't know that and when it detects that at compile time, it won't compile your code unless on all possible branchings, function reaches a return statement with a valid value.

Loren Soth

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.