Reputation: 89
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
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
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
Reputation: 149558
Your LoginIndhold
method has two problems:
LoginInfo
, while you're returning a concatenated string.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