locked
Web Form Login error! RRS feed

  • Question

  • User1421620300 posted

    Can someone help me to troubleshoot why i am receiving an error on my website. I have added login to my website and after completing coding the application gives run-time error. I have no known issues with code errors and everything seems okay, but after running website when i try to submit user login the website fails and gives the following error! Please reply, Markus.

    Server Error in '/' Application.


    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 21: 		protected void Submit_Click(object sender, EventArgs e)
    Line 22: 		{
    Line 23: 			SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    Line 24: 
    Line 25: 			con.Open();

    Wednesday, April 8, 2020 6:05 PM

Answers

  • User475983607 posted

    You named the connection strings PropertyEntities and PropertyConnectionString not ConnectionString.

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 8, 2020 6:21 PM

All replies

  • User1421620300 posted

    The following is my code: Markus!

    login.aspx 

    <div class="sec2">
    <div class="container">
    <div class="social">
    <asp:Image ID="Image1" runat="server" />
    <asp:Image ID="Image2" runat="server" />
    </div>

    <div class="content">
    <h2>Sign Up</h2>

    <asp:TextBox ID="TextBox1" placeholder="username" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox2" placeholder="password" runat="server"></asp:TextBox><br />
    <asp:Button ID="Submit" runat="server" OnClick="Submit_Click" Text="Submit" />
    <asp:Label ID="Label1" Style="padding-left:4vw; padding-top:2vw;" font-size="medium" fore-color="red" runat="server" Text=""></asp:Label>
    </div>
    </div>
    </div>

    </form>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PropertyConnectionString %>" SelectCommand="SELECT * FROM [login]"></asp:SqlDataSource>
    </body>
    </html>

    code behind: login.aspx

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

    }

    protected void Submit_Click(object sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

    con.Open();
    SqlCommand cmd = new SqlCommand("select * from login where username = @username and password = @password", con);

    cmd.Parameters.AddWithValue("@username", TextBox1.Text);
    cmd.Parameters.AddWithValue("@password",TextBox2.Text);

    SqlDataReader sdr = cmd.ExecuteReader();

    if (sdr.Read())
    {
    Label1.Text = "Login Sucessfull......!";
    }
    else
    {
    Label1.Text = "Login or Password Incorrect......!";
    }

    con.Close();
    }


    }
    }

    ConnectionStrings:

    <connectionStrings>
    <add name="PropertyEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;initial catalog=Property;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"
    providerName="System.Data.EntityClient" />
    <add name="PropertyConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=Property;Integrated Security=True"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

    Wednesday, April 8, 2020 6:07 PM
  • User475983607 posted

    You named the connection strings PropertyEntities and PropertyConnectionString not ConnectionString.

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 8, 2020 6:21 PM