Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8972369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:12:11+00:00 2026-06-15T18:12:11+00:00

I am trying to get a return value and it keeps giving me an

  • 0

I am trying to get a return value and it keeps giving me an error.
I am trying to grab the “roleid” after the username has been validated by sending it the username– I can’t figure out what I am doing wrong?

public string ValidateRole(string sUsername)
{
    string matchstring = "SELECT  roleid FROM tblUserRoles WHERE UserName='" +      sUsername +"'";
    SqlCommand cmd = new SqlCommand(matchstring);
    cmd.Connection = new SqlConnection("Data Source=(local);Initial Catalog=samplename;Integrated Security=True");
    cmd.Connection.Open();
    cmd.CommandType = CommandType.Text;

    SqlDataAdapter sda = new SqlDataAdapter();
    DataTable dt = new DataTable();
    sda.SelectCommand = cmd;
    sda.Fill(dt);

    string match;
    if (dt.Rows.Count > 0)
    {
        foreach (DataRow row in dt.Rows)
        {
            match = row["roleid"].ToString();
            return match;
        }
    }  
    else
    {  
        match = "fail";
        return match;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:12:12+00:00Added an answer on June 15, 2026 at 6:12 pm

    The “Not all code paths return a value” error you are seeing is a compiler error, not a runtime error, so the problem is with your C# code not being correct.

    In this case it is because you have a return statement in a foreach loop and the compiler is not quite smart enough to see that your code would go down the ‘else’ path if there were no rows in the datatable. I.e. the compiler cannot see that the ‘If (true)’ branch will always return a value.

    Best practice is to always have a return statement at the end of the function, and to initialize you variables (‘match’ is not initialized). If you return part way through, your code is less readable too.

    The simplest fix is:

     public string ValidateRole(string sUsername)
       {
    
           string matchstring = "SELECT  roleid FROM tblUserRoles WHERE UserName='" +      sUsername +"'";
           SqlCommand cmd = new SqlCommand(matchstring);
           cmd.Connection = new SqlConnection("Data Source=(local);Initial Catalog=samplename;Integrated Security=True");
           cmd.Connection.Open();
           cmd.CommandType = CommandType.Text;
    
           SqlDataAdapter sda = new SqlDataAdapter();
           DataTable dt = new DataTable();
           sda.SelectCommand = cmd;
           sda.Fill(dt);
    
           string match = "fail";
        if (dt.Rows.Count > 0)
        {
           foreach (DataRow row in dt.Rows)
           {
               match = row["roleid"].ToString();
            return match;
           }              
    
        }  
    
        return "fail";
    
       }
    

    However, there are several other issues with the code you may not be aware of:

    1. You have a SQL Injection vulnerability that makes your
      application completely insecure. This is because you have
      concatenated SQL strings to make a query rather than writing a
      parametized query.

    2. You should get in the habit of using a ADO.NET DataReader over a DataAdapters and DataTables. Or better, avoid DataTables altogether as
      they are legacy. Use Linq2Sql or Entity Framework for your data
      access layer and you will write far less code.

    3. You should seriously consider using ASP.NET Membersip API for your authorization and roles etc. If you did that, your function would not even be required – you’d just write: Roles.IsUserInRole(sUserName, “User”) to check whether a user is in a certain role.

    4. When you use a resource that implements IDisposable, like SQLConnection, you should wrap its use in a using() {} block so that you always release the resource as soon as possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do 2 things: get a return value from a C
I'm trying to get the IsAdmin Value with SQL query(This query return 1 line
I'm trying to get a return value from an ajax call but I keep
I'm trying to get the returned value from a java method, but it returns
I'm trying to get my function to return the data it got into another
I'm trying to get sphinx search to return excerpts of matched documents. I'm using
I'm trying to get an Excel function to return more arguments than are passed
I am trying to get a boolean method to return true or false on
I am trying to get the below linq query to return -1 if there
I'm trying to use $.post within a function, get the results, then return the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.