1

I am new to postgres and asp.net. I am trying to create a simple login page in VS 2015 using Npgsql. I don't know how to make a connection with c# and postgres database. If any one can please help me out of where I am going wrong. Thanks in advance.

My Login.aspx code looks like this:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="LoginPortal.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="css/bootstrap.css" rel="stylesheet" />
    <link href="css/my-styles.css" rel="stylesheet" media="screen" />
    <link href="dist/css/bootstrap.min.css" rel="stylesheet" media="screen" />
    <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome,firefox=1' /> 
     <link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css" /> 
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <link href="client/fine-uploader-new.css" rel="stylesheet" />
    <script src="client/jquery.fine-uploader.js"></script>    
    <link rel="stylesheet" href="http://bootstrapformhelpers.com/assets/css/bootstrap-formhelpers.min.css" media="screen" />
    <script src="http://bootstrapformhelpers.com/assets/js/bootstrap-formhelpers.min.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="http://bootstrapformhelpers.com/assets/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/dropzone/3.8.4/css/dropzone.css"></script>
  <script type="text/javascript" src="js/bootstrap-filestyle.min.js"> </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div class="container" style="margin-left: 40px">
        <div class="panel panel-primary panel-transparent">
        <div class="panel-heading">Login Page</div>
        <div class="panel-body">
        <asp:Label ID="lblUsername" runat="server" Text="Username"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>&nbsp;&nbsp;
        <asp:RequiredFieldValidator id="userValidator" runat="server"
            controltovalidate="txtUserName" errormessage="Please enter a valid Username!"></asp:RequiredFieldValidator>
        <br />
        <br/>
        <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>&nbsp;&nbsp;<asp:RequiredFieldValidator id="passwordValidator"
            runat="server" controltovalidate="txtPassword" errormessage="Please enter your Password!"></asp:RequiredFieldValidator>
        <br /><br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnLogin" runat="server" Text="Login" onclick="btnlogin_Click" />
        &nbsp;&nbsp;&nbsp;
            <asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" Text="Cancel" />
        <br /><br /><br />

     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="lblMsg" runat="server"></asp:Label>

     </div>
    </div>

    </div>
    </form>
</body>
</html>

And the code behind which I tried is:

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.Data;
using Npgsql; 

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

        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            string ConString = String.Format("Server=localhost;Port=5432;User Id=postgres;password=Att1234$$;Database=LoginCredits");
            try
            {
                NpgsqlConnection con = new NpgsqlConnection(ConString);
                con.Open();
                string sql = "Select * from logintab where username='" + txtUserName.Text + "' and password ='" + txtPassword.Text + "'";
                NpgsqlCommand cmd = new NpgsqlCommand(sql,con);
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                //NpgsqlDataReader rd = cmd.ExecuteReader();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    Session["id"] = txtUserName.Text;
                    Response.Redirect("Home.aspx");
                    Response.Write("Login Suucessfull!");
                    //Session.RemoveAll();
                }
                else
                {
                    lblMsg.Visible = true;
                    lblMsg.Text = "Please enter a Valid Username and Password";
                }
            }
            catch(NpgsqlException ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            lblMsg.Visible = false;
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtUserName.Focus();
        }
    }
}

I haven't completed code behind yet. Please suggest me to how make connection to the database.

3
  • You can find answer on stack overflow: stackoverflow.com/questions/1245554/… Commented May 5, 2016 at 7:51
  • Hi everybody. I have updated my code and done with the login task using a bit of sessions. what if I want to be automatically get redirected to login page after a few minutes of inactivity? Commented May 9, 2016 at 6:30
  • In the Home.aspx there is just a Logout button which simply redirects me back to the Login.aspx.. Commented May 9, 2016 at 6:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.