简单的登录错误。会议

时间:2014-09-14 20:38:17

标签: c# asp.net

我是初学者。我有这个简单的登录,真的无法理解为什么它不工作,当我输入正确的登录信息我得到这个错误:

'/'应用程序中的

服务器错误

bruger_id

Linje 67:session [“login_id”] = reader [“bruger_id”];

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


using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    if (Session["login_id"] != null)
    {



        PlaceHoldermenu.Visible = true;

        Button_Login.Text = "Log ud";

        Label_Fejl.Visible = false;
        TextBox1.Visible = false;
        TextBox2.Visible = false;



    }
    else
    {

        Label_Fejl.Visible = true;
        TextBox1.Visible = true;
        TextBox2.Visible = true;
        Button_Login.Text = "Log ind";

    }
}


     private void logind (string username, string password)
{





    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "SELECT bruger_email, bruger_kode FROM brugere WHERE bruger_email = @login AND bruger_kode = @pass";
    cmd.Parameters.Add("@login", SqlDbType.VarChar).Value = TextBox1.Text;
    cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = TextBox2.Text;

    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    if (reader.Read())
    {
        Session["login_id"] = reader["bruger_id"];
        Session["login_navn"] = reader["bruger_email"];



        Response.Redirect("profilNormal.aspx");
    }
    else
    {
        Label_Fejl.Text = "Forkert brugernavn eller kodeord.";
    }
    conn.Close();
}


protected void Button_Login_Click(object sender, EventArgs e)
{

    if (Button_Login.Text == "Log ind")
    {

        logind(TextBox1.Text, TextBox2.Text);
    }
    else
    {

        Session.Remove("login_id");
        Response.Redirect("Default.aspx");

    }
}

}

任何想法?顺便说一句,我仍然是asp.net c#的新手。

1 个答案:

答案 0 :(得分:3)

您忘了选择bruger_id

  

SELECT bruger_id ,bruger_email,bruger_kode FROM brugere WHERE   bruger_email = @login AND bruger_kode = @pass

因此读者会在这一行上抛出异常:

Session["login_id"] = reader["bruger_id"];