Problem with login page connecting to SQL database in asp.net please help

  • using System;

    using System.Configuration;

    using System.Data;

    //using System.Linq;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.HtmlControls;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Xml.Linq;

    //using System.Data.OleDb;

    using System.Data.SqlClient;

    using System.Collections;

    using System.ComponentModel;

    using System.Drawing;

    using System.Web.SessionState;

    public partial class _Default : System.Web.UI.Page

    {

    public string holduser=null;

    public string holdpass=null;

    public string usernm="default name";

    public string userpa="default pass";

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    private bool DbConnection(string txtUser, string txtPass)

    {

    //Connection

    SqlConnection a;

    a = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2008\\WebSite2\\WebSite2\\App_Data\\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

    a.Open();

    //Command

    SqlCommand b;

    b = new SqlCommand("sp_ValidateUser", a);

    b.CommandType = CommandType.StoredProcedure;

    SqlParameter objParam1;

    SqlParameter objParam2;

    SqlParameter returnPar;

    objParam1 = b.Parameters.Add("@UserName", SqlDbType.VarChar);

    objParam2 = b.Parameters.Add("Password", SqlDbType.VarChar);

    returnPar = b.Parameters.Add("@NO_OF_USERS", SqlDbType.Int);

    objParam1.Direction = ParameterDirection.Input;

    objParam2.Direction=ParameterDirection.Input;

    returnPar.Direction=ParameterDirection.ReturnValue;

    objParam1.Value=txtUser;

    objParam2.Value=txtPass;

    try

    {

    if(a.State.Equals(ConnectionState.Closed))

    {

    a.Open();

    b.ExecuteNonQuery();

    }

    if((int)returnPar.Value<1)

    {

    // status.Text="Invalid Login";

    return false;

    }

    else

    {

    Response.Redirect("C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2008\\WebSite2\\WebSite2\\Registration_page.aspx");

    return true;

    }

    }

    catch(Exception ex)

    {

    //

    //status.Text=ex+"Error Conneccting to DB";

    return false;

    }

    }

    /*

    protected void LoginButton_Click(object sender, EventArgs e)

    {

    // status.Text = Login1.UserName + " " + Login1.Password;

    if(Page.IsValid)

    {

    if(DbConnection(Login1.UserName.Trim(),Login1.Password.Trim()))

    {

    FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);

    }

    else

    {

    stat.Text="Invalid Login, Please Try Again";

    }

    }

    }

    OleDbDataReader r;

    r = b.ExecuteReader();

    while (r.Read())

    {

    string retrieveduser = Convert(r.GetValue(1));

    if (retrieveduser==holduser)

    {

    }

    else

    {

    Console.WriteLine("Invalid User");

    }

    }

    }

    private string Convert(object p)

    {

    throw new NotImplementedException();

    }

    protected string UserName_TextChanged(object sender, EventArgs e)

    {

    status.Text = Login1.UserName;

    Login1.UserName = holduser;

    //Connection

    OleDbConnection a1;

    a1 = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2008\\WebSites\\WebSite2\\App_Data\\Database.mdf");

    a1.Open();

    //Command

    OleDbCommand b;

    b = new OleDbCommand("select username from user where username = " + holduser);

    b.ExecuteNonQuery();

    //ExecuteReader

    OleDbDataReader c;

    c = b.ExecuteReader();

    try

    {

    if (c.Read())

    {

    usernm = Convert(c.GetValue(1));

    }

    else

    {

    usernm = "no such user";

    }

    }

    catch (Exception e1)

    {

    Console.WriteLine(e1);

    }

    status.Text = usernm;

    return usernm;

    }

    protected string Password_TextChanged(object sender, EventArgs e)

    {

    status.Text = Login1.PasswordLabelText;

    holdpass = Login1.PasswordLabelText;

    //Connection

    OleDbConnection a1;

    a1 = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2008\\WebSites\\WebSite2\\App_Data\\Database.mdf");

    a1.Open();

    //Command

    OleDbCommand b;

    b = new OleDbCommand("select password from user where password = " + holdpass);

    b.ExecuteNonQuery();

    //ExecuteReader

    OleDbDataReader c;

    c = b.ExecuteReader();

    try

    {

    if(c.Read())

    {

    userpa=Convert(c.GetValue(2));

    }

    else

    {

    userpa= "wrong password";

    }

    }

    catch (Exception e1)

    {

    Console.WriteLine(e1);

    }

    status.Text = userpa;

    return userpa;

    }*/

    }

    [/code]

    What i am trying to is in the database there are 2

    userid password

    Karan lucknow018

    Ratan pun01e

    The Database path is

    Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\WebSite2\WebSite2\App_Data\Database.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True

    I am trying to go to registration page if the login is successful but i am not able to

    Please any suggetsions.

  • Your connection string is going to use the active directory user you've logged in with to the client.

    Are the 2 users you refer to SQL Logins?

    If so, they need to be using windows authentication. If they are using sql notification you will either have to change them or change your connection string to specify the user/pswd.

    Not sure what the intended security model is for your application...


    ,

    Mike Thien

  • Yes thanks for the help, I managed to resolve the matter, the error was with the connection string having to be properly setup in web.config.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply