简体   繁体   English

通过Webform TEXTBOX在SQL Server DATABASE表中插入值的注册页面出错

[英]GETTING ERROR IN SIGN UP PAGE for inserting values in SQL server DATABASE table through webform TEXTBOX

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. 嗨,我正在尝试通过使用SQL Server体系结构使用3层体系结构在asp.net中创建登录页面以及注册页面。 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. 我可以从在数据库表创建期间手动插入的sql server数据库中获取数据,并且可以在登录页面中使用它。

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. 我还创建了一个注册页面,但是我无法从将Webform文本框注册到sqlserver数据库中获取值。我遇到一些错误,请帮我解决这个问题。

I have given the connection string of sql server in web.config 我在web.config中给出了sql server的连接字符串

my sql server table creation code 我的sql server表创建代码

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

web.config connection string code web.config连接字符串代码

<?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 我的datAccess层代码

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 注册页面按钮代码,用于通过单击按钮将数据输入到SQL Server数据库中

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. 我在仅注册按钮的最后一个代码中遇到错误,它无法将SIGN UP Webform Textbox的值插入到sql server databse表中,也无法反映我想使用signing webform添加到sql server TABLE中的真实值并保存它。 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 请协助解决此问题

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM