0

hi I am trying to make a log in page as well as sign up page in asp.net using 3 tier architecture by using sql server architecture. I am able to fetch data from sql server data base which I have manually inserted during table creation in database and I am able to use it in my log in page.

I have also created a sign up page but I am not able to get the values from sign up webform textbox to sqlserver database I am getting some error kindly help me with this.

I have given the connection string of sql server in web.config

my sql server table creation code

CREATE TABLE LOGINDETAILS
(USERID VARCHAR(50),
PASSWORD VARCHAR (50)
);
 INSERT INTO LOGINDETAILS (USERID,PASSWORD) values( 'sam', 'pass');

web.config connection string code

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionStrings>
    <add name="DBcon" connectionString="Data Source=P3A-B1YH882\SQLSERVER;Initial Catalog=master;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

</configuration>

my business layer /middle layer code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using DataAcess;
using System.Data.SqlClient;
using System.Data.Sql;

namespace middlelayer
{
    public class UserBO
    {
        private string _UserName = " ";

        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }
        private string _Password = " ";

        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        DataA da = new DataA();

        public bool getUser()
        {
            if (da.IsValid(UserName, Password).Tables[0].Rows.Count == 0)
            {
                return false;


            }

            else
            {
                return true;


            }
        }
    }
}

my datAccess layer code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;

namespace DataAcess
{
    public class DataA
    {

        string conString = ConfigurationManager.ConnectionStrings["DBcon"].ToString();
        public DataSet IsValid(string UserName, string Password)
        {
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM LOGINDETAILS WHERE USERID ='" + UserName + "' and PASSWORD= '" + Password + "'", con);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            return ds;

        }
    }
}

MY LOGIN PAGE CODE

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="height: 277px">
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="lbluserid" runat="server" BackColor="#FFFF99" BorderStyle="Ridge" Height="17px" Text="User ID" Width="52px"></asp:Label>
        <asp:TextBox ID="txtuserid" runat="server" BackColor="#99FFCC" style="margin-left: 122px"></asp:TextBox>
&nbsp;&nbsp;
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    </div>
        <p>
            <asp:Label ID="lblpassword" runat="server" BackColor="#FFFF99" BorderStyle="Ridge" Text="Password"></asp:Label>
&nbsp;<asp:TextBox ID="txtpassword" TextMode="Password" runat="server" BackColor="#99FFCC"  style="margin-left: 110px" ></asp:TextBox>
        </p>
        <p>
            &nbsp;</p>
        <asp:Button ID="btnlogin" runat="server" BackColor="#33CCFF" BorderStyle="Ridge" OnClick="btnlogin_Click" style="margin-left: 78px" Text="Login" Width="107px" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <p>
            &nbsp;</p>
        <asp:Label ID="Label1" runat="server" Text="NOT REGISTERED ??"></asp:Label>
      &nbsp;<asp:HyperLink ID="HyperLink1" runat="server" BorderStyle="Outset" NavigateUrl="~/sign_up.aspx">SIGN UP</asp:HyperLink>
    </form>
</body>
</html>

* MY SIGN UP PAGE CODE*

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    &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="lblssignup" runat="server" BackColor="#FF99CC" Text="SIGN UP"></asp:Label>
        <br />
        <br />
        <p>
            <asp:Label ID="lblsuserid" runat="server" Text="ENTER USER ID"></asp:Label>
&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:TextBox ID="TextBox1" runat="server" style="margin-bottom: 0px"></asp:TextBox>

        </p>
        <asp:Label ID="lblspassword" runat="server" Text="ENTER PASSWORD"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <p>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" Width="66px" />
        </p>
    </form>
</body>
</html>

SIGN UP PAGE BUTTON CODE FOR ENTERING DATA INTO SQL SERVER DATABASE ON BUTTON CLICK

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using middlelayer;
namespace WebApplication4
{
    public partial class sign_up : System.Web.UI.Page
    {
        string conString = ConfigurationManager.ConnectionStrings["DBcon"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            string ins= "Insert into [LOGINDETAILS](USERID, PASSWORD) VALUES ('" +TextBox1.Text+ "' , '" +TextBox2.Text+ "')";
                SqlCommand com = new SqlCommand(ins,con);
                  DataSet du = new DataSet();
            SqlDataAdapter sdi = new SqlDataAdapter(com);
            sdi.Fill(du);
            con.Close();

            }


        }
    }

I AM getting error in this last code only of sign up button it is not able to insert values of SIGN UP webform Textbox to sql server databse table and also not reflecting the real values which I want to add in sql server TABLE using sign up webform and also noty saving it. It is sending some error values . kindly help me with this.

BELOW ARE THE IMAGES OF LOG IN AS WELL AS SIGN UP PAGE FOR REFERENCE

LOGIN PAGE WEB FORM

SIGN UP PAGE WEBFORM

KINDLY HELP IN RESOLVING THIS ISSUE

saurabh255
  • 133
  • 2
  • 3
  • 19
  • Instead of saying insert into logindetails, textbox1. fetch the textbox1 data in a variable before the loop and then add it into the logindetails using the variable – Tomm Oct 06 '17 at 06:46
  • You've taken helpful formatting & good question techniques such as adding code, capitalizing, bold, etc and gone overboard. – Keyur PATEL Oct 06 '17 at 06:48
  • can you please elaborate little bit I am not getting you clearly?? Tomm – saurabh255 Oct 06 '17 at 06:48
  • [Bobby Tables](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work) is dancing around you – Steve Oct 06 '17 at 06:48
  • By the way you should insert the content of the two textboxes not the name of their control class. It is TextBox1.Text and TextBox2.Text – Steve Oct 06 '17 at 06:49

1 Answers1

0

Try this:

    SqlConnection con = new SqlConnection(conString);
    con.Open();

    string ins= "Insert into [LOGINDETAILS](USERID, PASSWORD) VALUES (@param1 , @param2)";
    SqlCommand cmd = new SqlCommand(ins,con);
    cmd.Parameters.Add("@param1", SqlDbType.Varchar, 50).value = TextBox1.Text;  
    cmd.Parameters.Add("@param2", SqlDbType.Varchar, 50).value = TextBox2.Text;
    cmd.CommandType = CommandType.Text;
    cmd.ExecuteNonQuery();
    con.Close()
Bungysheep
  • 40
  • 7