Hi, can someone pls help me.

I'm trying to pass the name of the user who logs in from a login form and display his/her name on a Main form. I'm using get{} and set{}. My problem is with the function I'm trying to create to return the name of the user which I'm going to place inside my get{}. Here's my function (I've placed this within a Class):

public static string loggedinuser(TextBox uname,TextBox password)
        {
            
            readrecord("Select dbo.loggeduser('" + uname.Text + "','" + password.Text + "')");
            if (dr.Read())
            {
                
                while (dr.Read())
                {                   
                    string name =(dr[0].ToString());
                    return name;
                    
                }
                
            }
            con.Close();
                       
        }

It's showing this error "Not all code paths return a value." I don't understand. I already have the return statement.

If somebody can help me, I'm most grateful. Thanks so much in advance!

Your code does not return the name string (value) from all over the method. It reutrns only from the while loop. What in case if the code does not go to while loop? It will return what? notihng. But your method return type is a string. That means you have to return string even if the code does not go to while loop.
Do it like this:

public static string loggedinuser(TextBox uname,TextBox password)
        {   
            string name = null;         
            readrecord("Select dbo.loggeduser('" + uname.Text + "','" + password.Text + "')");
            if (dr.Read())
            {                
                while (dr.Read())
                {                   
                    name =(dr[0].ToString());     
                }                
            }
            con.Close();
            return name;               
        }

In case if the code will no go into while loop, it will return a string with null value.
So on the other side you can check it:

string _name = loggeddinuser(uname, password);
if(_uname != null)
{
     //your code if the string returns a not null string!
}
else
    MessageBox.Show("Value is null."); //or do some other user notification.

The return statement will not be called if the first if() statement fails (ie the Read() returns false)

One way to solve this is to put return ""; at the end (after con.close())

Note that con.close() is not being called if the name is read, since return is called which jumps out of the function.

Here's what I would do:

public static string loggedinuser(TextBox uname,TextBox password)
        {
            
            readrecord("Select dbo.loggeduser('" + uname.Text + "','" + password.Text + "')");
            string tempString = "";
            if (dr.Read())
            {
                
                while (dr.Read())
                {                   
                    string name =(dr[0].ToString());
                    tempString = name;
                    break;
                }
                
            }
            if (tempString == "")
                throw new Exception("Login info not found");
            con.Close();
            return tempString;
        }

Adding one return statement at the end will solve a problem.... Trying to figure out the problem yourself will increase your figure try not to rely on others but look for the answer yourself

commented: good suggestion +3

Hi all,

Thanks so much for your feedback. Unfortunately, "Adding one return statement at the end will solve a problem" did not do it. I've tried using Mitja's and skamatic's code, but it always returned a null or "" value. I still haven't figured out WHY. BUT I've figured out a different way:

public string loggedinuser()
        {
           
            Class1.readrecord("Select dbo.loggeduser('" + txtusername.Text + "','" + txtpassword.Text + "')");
            Class1.dr.Read();
            string name = (Class1.dr[0].ToString());
            Class1.con.Close();
            return name;

        }

(I transferred my function to the login form). And I have other lines of code that take care of catching if login info is not found. Here are the rest of my code in my login form:

public string getloggeduser
        {
            get { return loggedinuser(); }

        }
        
        
        private void btnlogin_Click(object sender, EventArgs e)
        {
            if ((txtusername.Text == String.Empty) || (txtpassword.Text == String.Empty))
            {
                lblwarning.Visible = true;

            }
            else
            {

                Class1.readrecord("Select * from dbo.logininfo('" + txtusername.Text + "','" + txtpassword.Text + "')");
                if (Class1.dr.Read())
                {
                    Class1.con.Close();
                    lblwarning.Visible = false;
                    FormMain formmain = new FormMain();                   
                    formmain.setloggeduser = getloggeduser;
                    this.Hide();
                    formmain.Show();

                }
                else
                {
                    lblwarning.Visible = true;
                    lblwarning.Text = "Invalid username/password.";
                    txtpassword.Clear();

                }
                Class1.con.Close();
                
            }

It works fine :=) You may have some thoughts to tweak the code a little more. I'd be grateful still.

To abelLazm: You can try to be a little nice, and just say "this might work" and stop there. Saying what you did does not help at all. I see you're a "virtuoso" now, but when you were starting out, weren't you "relying on others" too?

To Mitjza and skamatic, thanks so much for your time :=)

commented: Well done +11

:) It was not to hurt you it was just an advice if you take that advice seriously it will help you .... and BTW I always tried not to rely on others :) Best of luck

OK, thanks for the advice then!

Just came across this post by Chair

I've logged in via Facebook where it says :-

"We're a community of IT Pros here for help, advice, solutions, professional growth and fun."

so maybe abelLazm (Abel Lazm) would like to take note and buy himself some humility
for having a go at Chair for "daring to ask a question"

to quote from Abel whats his name -:-

"Trying to figure out the problem yourself will increase your figure try not to rely on others but look for the answer yourself"

well that really nice isn't it , trying to make a human with a question feel thick as you have a MPHIL in computer science ?

Bill Gates ( you may have geard of him ? - if not try something called the web ) multi-billionaire and way more successful than you will ever be , doesnt know everything but apparently you do

tell you what , why don't you set up your own forum where you can talk down to the ordinary people and boost your ego that way ?

we'd all appreciate it

and Chair - if your stuck , ask questions and experiment and you'll learn loads

great site by the way

SqlConnection con = new SqlConnection("Data Source=6;Initial Catalog=kalyan;User ID=sa;Password=***********");

Line 18: public string insertuserdetails(BEL objuserdetails)
Line 19: {

error
'DAL.insertuserdetails(BEL)': not all code paths return a value

Please make a new thread for your issue instead of hijacking a two year old one.

The issue will be that you do not return a value in every possible outcome of the method you are in.

For example if an if else statement you must return values in both sections of it

public bool DemoMethod(string Input)
{
    if (Input == "Hello")
    {
        return true;
    }
    else
    {
        return false;
    }
}

or another common one would be

public bool DemoMethod(string Input)
{
    if (Input == "Hello")
    {
        return true;
    }
    // Not having a return call here will give the error you see as you do not provide a return path if the code does not enter the if loop
}

Would need to see the full code block to identify the missing line though

public bool Assert_DisconnectButton_NoTooltip1()
yes

         #region Variable Declarations
         WinButton uIDisconnectButton = this.UIPcProxConfigpcProxanWindow.UIItemWindow.UIDisconnectButton;
         #endregion

         if(ToolTipService.IsOpenProperty.Name==null)
         {
             return true;
         }

     }

hello,
in above method there is an error "not all code path returns a value".
how can i get rid of this.

Missing return on line 9.

Next time start a new discussion thread, instead of replying to an existing one.

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.