954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Resolve my problem

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;
}
}
}
Gowrishankar
Newbie Poster
13 posts since Jun 2006
Reputation Points: 10
Solved Threads: 1
 

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

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

Hi Campkev, Before the Custvaluserid_ServerValidate event ,I check the page through the button click event.
privatevoid 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.

Gowrishankar
Newbie Poster
13 posts since Jun 2006
Reputation Points: 10
Solved Threads: 1
 

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;
}
campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

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

Lord Soth
Posting Whiz in Training
233 posts since Mar 2006
Reputation Points: 28
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You