Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
While fetching a record in asp.net from sql server it always return null..here's code- Even if record is available in database, object oid will have null at run time. Please help. thanks.
C#
protected void btnLogin_Click(object sender, EventArgs e)
{
    string cs = ConfigurationManager.ConnectionStrings["DBMS"].ConnectionString;
    SqlConnection con = new SqlConnection(cs);
    SqlCommand com = new SqlCommand("select id from reg where user=@user and password=@pass", con);
    com.Parameters.AddWithValue("@user", txUName.Text);
    com.Parameters.AddWithValue("@pass", txPass.Text);
    con.Open();
    var oid = com.ExecuteScalar();
    con.Close();

    if (oid == null)
    {
        lblEr.Visible = true;
    }
    else
    {
        Session.Add("id", oid.ToString());
        Session.Add("user", txUName.Text);

        com = new SqlCommand("select type from dbo.reg where user=@u", con);
        com.Parameters.AddWithValue("@u", txUName.Text);
        con.Open();
        var otype = com.ExecuteScalar();
        con.Close();

        var vtype = "admin";
        if (vtype == otype.ToString())
        {
            Session.Add("admin", otype.ToString());
            Response.Redirect("admin.aspx");
        }
        Response.Redirect("user.aspx");

    }
}
Posted
Updated 21-Oct-13 7:36am
v2
Comments
Mehdy Moini 21-Oct-13 13:58pm    
Start profiler on the sql server, and see what command exactly does pass to sql server from IIS, it is a handy solution for most of problems like these.

1 solution

It worked just by correcting following syntax-

SqlCommand com = new SqlCommand("select id from reg where [user]=@user and [password]=@pass", con);

Since 'user' is also name of a user in sql server, so when we want to use it at another place like here as column then we have to use it as [user].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900