Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Datile
{
    public partial class login : System.Web.UI.Page
    {
     
      public  static string s = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationDbContext"].ToString();
        SqlConnection con = new SqlConnection(s);
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if ((TextUname.Text != "") && (TextPassword.Text != ""))
            {
                con.Open();

                SqlCommand cmd = new SqlCommand("select * from login where username='" + TextUname.Text + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    if (TextUname.Text == dr["username"].ToString())
                    {
                        if (TextPassword.Text == dr["password"].ToString())
                        {

                            Session["name"] = dr["username"].ToString();
                            Session["uid"] = dr["uid"].ToString();
                         

                        }
                        else
                        {

                            // if (TextBox2.Text != dr["pwd"].ToString())
                            Label3.Text = "Incorrect Password";
                        }
                    }
                    else if (TextUname.Text != dr["username"].ToString() && TextPassword.Text != dr["password"].ToString())
                    {
                        Label3.Text = "both UserName and Password are wrong";
                    }
                    else
                    {
                        Label3.Text = "Incorrect UserName ";
                    }
                }
                else
                {
                    Label1.Text = "both UserName and Password are wrong";
                }
                dr.Close();
                con.Close();
            }
        }

    }
}


here im getting this error at declaration of string 's'
Object reference not set to an instance of an object.
Posted
Updated 11-Dec-12 20:15pm
v2
Comments
Sergey Alexandrovich Kryukov 12-Dec-12 2:16am    
In what line?
--SA
choudhary.sumit 12-Dec-12 2:17am    
post your connectionstring here from web.config file

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown, but this is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

—SA
 
Share this answer
 
This means that System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationDbContext"] is returning null. If you call any property or method on a null then NullReferenceException is thrown.

Check your config file for the appropriate connection string name.
 
Share this answer
 
hi,
please check you connection string in web.config or you have not spelled "ApplicationDbContext" correctly.
 
Share this answer
 
The connection string name "ApplicationDbContext" might be spelled mistaken..
Please check the spelling of the connection string in the Web config connections block....
 
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