Jesper Petersen
Jesper Petersen

Reputation: 89

It will not return a value in class

I'm working right now to make a login system on my website by building it up in class,

I've almost got all the things in place, but it is such that it will not return a value back.

login.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using IndholdName;

namespace LoginClass
{
    public class Login
    {
        public LoginInfo LoginIndhold(string username, string password)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = conn;

            cmd.CommandText = "SELECT Id FROM users WHERE Username = @Username AND Password = @Password";
            cmd.Parameters.AddWithValue("@Username", username);
            cmd.Parameters.AddWithValue("@Password", password);

            conn.Open();
            SqlDataReader readerbruger = cmd.ExecuteReader();

            if (readerbruger.Read())
            {
                HttpContext.Current.Session["id"] = Int32.Parse(readerbruger["id"].ToString());
                //It will not return a value
                //its error are here,
                return username + password;
            }
        }
    }
}

but this is how I've also set it to give value to the set; and get;

name.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IndholdName
{
    public class LoginInfo
    {
        public string username { set; get; }
        public string password { set; get; }
    }
}

My problem is here that it will not give a value back to the user where the only say welcome to Kasper Olsen

if (readerbruger.Read())
        {
            HttpContext.Current.Session["id"] = Int32.Parse(readerbruger["id"].ToString());
            //It will not return a value
            //its error are here,
            return username + password;
        }

Upvotes: 1

Views: 70

Answers (3)

YazX
YazX

Reputation: 521

public LoginInfo LoginIndhold(string username, string password)
{
 LoginInfo returnValue =  null;
        using( SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
   {
        SqlCommand cmd = new SqlCommand();

        cmd.Connection = conn;

        cmd.CommandText = "SELECT Id FROM users WHERE Username = @Username  AND Password = @Password";
        cmd.Parameters.AddWithValue("@Username", username);
        cmd.Parameters.AddWithValue("@Password", password);

        conn.Open();
       using(SqlDataReader readerbruger = cmd.ExecuteReader())
       {
        if (readerbruger.Read())
        {
            HttpContext.Current.Session["id"] =     Int32.Parse(readerbruger["id"].ToString());
            returnValue = new LoginInfo { Username= username, Password=  password};
        }
       }
    }

return returnValue;
}

Upvotes: 0

marsh
marsh

Reputation: 2730

This method:

public LoginInfo LoginIndhold(string username, string password)

indicates that the return value is the class LoginInfo. You are returning a string though. So you need to create the class store the login info inside it and return that instead.

try:

public LoginInfo LoginIndhold(string username, string password)
{
    IndholdName.LoginInfo info;
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    SqlCommand cmd = new SqlCommand();

    cmd.Connection = conn;

    cmd.CommandText = "SELECT Id FROM users WHERE Username = @Username AND Password = @Password";
    cmd.Parameters.AddWithValue("@Username", username);
    cmd.Parameters.AddWithValue("@Password", password);

    conn.Open();
    SqlDataReader readerbruger = cmd.ExecuteReader();

    if (readerbruger.Read())
    {
        HttpContext.Current.Session["id"] = Int32.Parse(readerbruger["id"].ToString());
        info.username = username;
        info.password = password;
    }

    return info;
}

Upvotes: 2

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149558

Your LoginIndhold method has two problems:

  1. You're stating that it should return a LoginInfo, while you're returning a concatenated string.
  2. If readerbruger.Read() returns false, your method doesn't return a value at all.

You'll want to do something like this:

if (readerbruger.Read())
{
      HttpContext.Current.Session["id"] = Int32.Parse(readerbruger["id"].ToString());
 }

 return new LoginInfo { Username = username, Password = password };

Upvotes: 3

Related Questions