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

Error in code! help please..

I am getting an error when it is determining whether the username is is false or not. I am using asp.net in code-behind. It is highlighted below. If someone can tell me what the error is that would be amazing!

Thank you!

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if ((Session["Check"] != null) && (Convert.ToBoolean(Session["Check"]) == true))
{
if (User.Identity.IsAuthenticated)
{
Output.Text = "You are logged in!";
}
}
}

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection("Data Source = ietm-fwb-sql1; Initial Catalog = import_log; Persist Security Info = True; User ID = sa; Password = fwbadmin");

try
{
connection.Open();
UserName.Text = "Connected!";
}

catch (Exception)
{
UserName.Text = "Not connected";
}
}

protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
{
Boolean blnresult;
blnresult = false;

blnresult = Authentication(Login.UserName);

if (blnresult == true)
{
e.Authenticated = true;
Session["Check"] = true;
}
else

e.Authenticated = false;
}

private bool Authentication(TextBox textBox)
{
throw new NotImplementedException();
}

protected static Boolean Authentication(string Username, string Password)
{
string sqlstring;
sqlstring = "SELECT userID FROM import_log.dbo.user_verification WHERE userID =" + Username + "";

System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source = ietm-fwb-sql1; Initial Catalog = import_log; Persist Security Info = True; User ID = sa; Password = fwbadmin");

System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(sqlstring, con);

System.Data.SqlClient.SqlDataReader reader;

con.Open();

reader = comm.ExecuteReader();

if (reader.Read())
return true;
else
return false;
}
}
}

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

What error are you getting?

Akill10
Posting Pro
575 posts since Sep 2010
Reputation Points: 115
Solved Threads: 80
 

Do it this way:

protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
        {
            Boolean blnresult = Authentication("userName", "password"); //provide 2 arguments in here!!!!
            if (blnresult == true)
            {
                e.Authenticated = true;
                Session["Check"] = true;
            }
            else

                e.Authenticated = false;
        }

        protected static Boolean Authentication(string Username, string Password)
        {
            string sqlstring;
            sqlstring = "SELECT userID FROM import_log.dbo.user_verification WHERE userID =" + Username + "";

            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source = ietm-fwb-sql1; Initial Catalog = import_log; Persist Security Info = True; User ID = sa; Password = fwbadmin");

            System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(sqlstring, con);

            System.Data.SqlClient.SqlDataReader reader;

            con.Open();

            reader = comm.ExecuteReader();

            if (reader.Read())
                return true;
            else
                return false;
        }
    }
}
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 
What error are you getting?


I'm getting the error that an object reference for the non-static field, method, or property "System.Web.UI.WebControls.Login.UserName.get"

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Mitja, I am still getting an error (the one that is posted below)

Do it this way:

protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
        {
            Boolean blnresult = Authentication("userName", "password"); //provide 2 arguments in here!!!!
            if (blnresult == true)
            {
                e.Authenticated = true;
                Session["Check"] = true;
            }
            else

                e.Authenticated = false;
        }

        protected static Boolean Authentication(string Username, string Password)
        {
            string sqlstring;
            sqlstring = "SELECT userID FROM import_log.dbo.user_verification WHERE userID =" + Username + "";

            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source = ietm-fwb-sql1; Initial Catalog = import_log; Persist Security Info = True; User ID = sa; Password = fwbadmin");

            System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(sqlstring, con);

            System.Data.SqlClient.SqlDataReader reader;

            con.Open();

            reader = comm.ExecuteReader();

            if (reader.Read())
                return true;
            else
                return false;
        }
    }
}
sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: